13.2 PointBase UniSync PointBase provides a pure Java-based, platform-independent synchronization engine called UniSync. UniSync (v4.5) can synchronize enterprise databases (Oracle, DB2, Sybase, and MS SQL Server) and workgroup databases (PointBase Embedded) with PointBase Micro mobile databases. The key concepts in UniSync are hubs and spokes. The synchronization process is as follows: Create corresponding databases and tables on both the backend server and mobile devices.Create a hub on the synchronization server. The hub contains publications that specify the backend tables (or partial tables) available for synchronization (publish).Use the hub object to create spokes. Spokes are objects on the synchronization server representing mobile devices. Each spoke has an ID. It can subscribe to the publications in the same hub through subscription objects. Using a spoke ID, the mobile device connects to the matching spoke and synchronizes to the subscribed backend tables.Start the synchronization server. This basically involves executing the main() method of class com.pointbase.me.sync.Server. The server class is available in PointBase distribution package. There are several ways to run the server in different environments. Please refer to PointBase documentation for more details and example scripts. By default, the server listens at port 8124.Initiate the synchronization process using a spoke ID and spoke stub classes residing on the mobile devices. Figure 13.3 illustrates the architectural concepts of hubs and spokes.

13.2.1 Serverside Code Walk Through Listing 13.1 uses example code from UniSync's basicMicro_to_Oracle sample application to illustrate the synchronization process between a PointBase Micro database and an Oracle backend database. On the synchronization server side, we first create a hub and a publication to expose the tables to be synchronized. Then, we create a spoke that subscribes to that publication. Finally, we run the server at a specific port.Listing 13.1. PointBase UniSync server application
import com.pointbase.unisync.api.*; // A PB Embedded DB is used by the sync // engine to store metadata. String PB_DRIVER = "com.pointbase.jdbc.jdbcUniversalDriver"; String PB_EMBEDDED_URL = "jdbc:pointbase:embedded:HUBDB,new"; String PB_USER = "PBPUBLIC"; String PB_PASSWORD = "PBPUBLIC"; // Parameters to access the Oracle back end String DB_DRIVER = "oracle.jdbc.driver.OracleDriver"; String DB_URL = "jdbc:oracle:<database>"; String DB_USER = "PBPUBLIC"; String DB_PASSWORD = "PBPUBLIC"; // Those point to the same Oracle database String UNISYNC_URL = "jdbc:pointbase:oracle:<database>"; String UNISYNC_DRIVER = "com.pointbase.driver.jdbc.oracle.OracleDriver"; // Configuration constants String DATA_SOURCE = "Data"; String HUB_NAME = "Hub1"; String PUB = "Pub1"; String SUB = "Sub1"; String SPOKE_NAME = "Spoke1"; String SPOKE_PASSWORD = "password"; // Tables to be synchronized String[] tableNames = new String[] {DB_USER + ".NAMECARD"}; // The Sync manager uses a PB Embedded DB for metadata SyncManager manager = SyncManager.getInstance(PB_EMBEDDED_URL, PB_DRIVER, PB_USER, PB_PASSWORD); // Get or create a hub Hub hub=manager.getHub(HUB_NAME); if (hub == null) hub = manager.createHub(HUB_NAME); // Get or create a backend data source SyncDataSource dataSource = manager.getSyncDataSource(DATA_SOURCE); if (dataSource == null) dataSource = manager.createSyncDataSource(DATA_SOURCE, UNISYNC_URL, UNISYNC_DRIVER, DB_USER, DB_PASSWORD); // Get or create a publication of // the specified tables Publication pub = hub.getPublication(PUB); if (pub == null) { pub = hub.newPublication(PUB, DATA_SOURCE, tableNames); // Make the Publication available to Spokes hub.publish(pub); } // Get or create a spoke that // subscribes to the publication SpokeConfig spoke = hub.getSpokeConfig(SPOKE_NAME); if (spoke == null) { spoke = hub.createSpokeConfig(SPOKE_NAME); spoke.savePassword(SPOKE_PASSWORD); // Subscribe to the Publication Subscription sub = spoke.newSubscription(SUB, SyncDataSource.DEFAULT,PUB); spoke.subscribe(sub); } // Start the server at the default port 8124 // or the port specified by command line parameter // -Dtcp.listenerPort = <Numeric Value> hub.startServer();
13.2.2 Clientside Code Walk Through On the client side, a mobile device contacts the server to obtain a spoke and synchronizes tables that are subscribed to that spoke (Listing 13.2). The client API works on both the CDC and CLDC.Listing 13.2. PointBase UniSync client side application
import com.pointbase.me.jdbc.*; // User database parameters String PB_MICRO_URL = "jdbc:pointbase:micro:SPOKEDB"; String PB_DRIVER = "com.pointbase.me.jdbc.jdbcDriver"; String PB_USER = "PBPUBLIC"; String PB_PASSWORD = "PBPUBLIC"; // How to connect the sync server HUB_URL = "http://sync.server:8124"; // Constants to define the Spoke name // and the password String SPOKE_NAME = "Spoke1"; String SPOKE_PASSWORD = "password"; // First establish a DB connection to PB_MICRO_URL Class.forName(PB_DRIVER); Connection conn = DriverManager.getConnection(PB_MICRO_URL, PB_USER, PB_PASSWORD); // SyncManager stores temp info in local DB SyncManager manager=SyncManager.getInstance(conn); // Get or create a spoke Spoke spoke = manager.getSpoke(SPOKE_NAME); if (spoke == null) { spoke=manager.createSpoke(SPOKE_NAME); // Save the URL of the Hub, so that the // Spoke can communicate with the Hub spoke.saveHubURL(HUB_URL); // Save the Spoke's password. This should be // same as the one provided in the Hub side. spoke.savePassword(SPOKE_PASSWORD); // Load the configuration from the Hub // This will also create the NameCard table spoke.loadConfig(); // This creates the NameCard table on the device spoke.getSnapshot(); } // Call sync to synchronize the databases spoke.sync();
|