MySQL Attribute Persistence
Overview
org.moock.unity.opt.sqlap.SQLAttributePersistence is an AttributePersistence Object for MySQL
database. It is a simple but effective implementation and can be used as a guide for more complex implementations.
Tables are created automatically by the Server when the server starts up if they don't
already exist. Only String Attributes can be persisted. All Attributes set via UPCRoom are String so this
is only a concern for Attributes set by other Rooms which may store different Objects.
Client persistence is done by a clientID and clientPassword combination.
Deploying to the Server
Step 1
Install MySQL. You can download MySQL at www.mysql.com.
Step 2
Create a database. I recommend using MySQL Control Center which provides a graphical interface.
Alternatively, use the command:
CREATE DATABASE db_name
For help with MySQL consult the excellent documentation available the site.
Step 3
Edit uconfig.xml by adding the following under the SERVER tag:
<ATTRIBUTE_PERSISTENCE>
<CLASS>org.moock.unity.opt.sqlap.SQLAttributePersistence</CLASS>
<ATTRIBUTES>
<DRIVER>org.gjt.mm.mysql.Driver</DRIVER>
<URL>jdbc:mysql://db_ip:3306/db_name</URL>
<USERNAME>db_username</USERNAME>
<PASSWORD>db_password</PASSWORD>
</ATTRIBUTES>
</ATTRIBUTE_PERSISTENCE>
DRIVER is the name of the jdcb driver to use to make the database connection.
URL is the url to your mysql database not including username and password.
USERNAME/PASSWORD are a username and password with permission to create and write
to tables in your database.
Step 4
Add the jar file containing the driver class and unity_optional.jar to the CLASSPATH in the startup script you use to start Unity 2 Multiuser Server
(startserver.bat or startserver.sh).
Example: Windows assuming the mysql-jdbc.jar (containing the mysql driver) has been copied to the lib directory:
java -cp lib\mysql-jdbc.jar;lib\unity_optional.jar;lib\xerces.jar;lib\xml-apis.jar;lib\unity_core.jar;lib\log4j.jar;lib\jdom.jar -Dlog4j.configuration=file:ss.lcf org.moock.unity.core.Unity start
Creating and Logging In
Server and Room persistence is handled automatically. Client persistence must be initiated by a Room using
the ClientManagementServices Object.
All arguments passed in args for are Strings.
createPersistentClient(String clientID, ArrayList args)
args 0 identifies the Client across sessions (eg. username).
args 1 the password for the Client which must be matched for loginClient
removePersistentClient(String clientID, ArrayList args)
args 0 identifies the Client across sessions (eg. username).
loginClient(String clientID, ArrayList args)
args 0 identifies the Client across sessions (eg. username).
args 1 the password GIVEN which must match the existing password to login successfully
So to create a new client in the database for the client with clientID 45 (obtained via ClientServices getClientID()) you would use something like the following code:
ArrayList args = new ArrayList();
args.add("james");
args.add("7hth3n3");
Services.getClientManagementServices().createPersistentClient("45", args);
Of course generally you will not hardcode the username and password but will instead retrieve
it from Messages sent by the Client. See the code in UPCRoom for a demonstration of how
UPCRoom uses this AttributePersistence.