Skip to content

Commit 453d27f

Browse files
authored
Add files via upload
1 parent ba5bea5 commit 453d27f

File tree

5 files changed

+223
-23
lines changed

5 files changed

+223
-23
lines changed

AttributeType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
package sync.db.mysql;
2626

2727
/**
28-
*
28+
* Enum class that defines the type of attribute being mapped between two tables.
29+
* <p>
30+
* <b>STRING</b> type refers to all attributes that require them to be
31+
* surrounded with a '' or "" in a MySQL insert statement.
32+
* <b>NUMERICAL</b> type refers to all attributes that do not require them to
33+
* be surrounded with a '' or "" in a MySQL insert statement.
2934
* @author Arvind Sasikumar
3035
*/
3136
public enum AttributeType {

DBSyncAgent.java

Lines changed: 187 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,36 @@
2727
import java.sql.*;
2828

2929
/**
30-
*
30+
* An object of this class serves as the agent for the synchronization process.
31+
* All synchronization tasks are handed over to an object of this class, which
32+
* then takes the required steps to perform the actual synchronization depending
33+
* on its set properties. It requires all details for establishing a connection
34+
* to the server and client MySQL servers and some additional synchronization
35+
* specific properties.<p>
36+
* An object of this class is created using the Builder pattern as opposed to
37+
* the usual constructor based initialization.<p>
38+
* An example on using the Builder pattern to create a new DBSyncObject:<p>
39+
* <pre>
40+
* {@code
41+
* DBSyncAgent dbSyncAgent = new DBSyncAgent.Builder()
42+
.setServerDatabaseAddress("localhost")
43+
.setServerDatabaseName("test1")
44+
.setServerDatabaseUsername("root")
45+
.setServerDatabasePassword("root")
46+
.setServerDatabasePort(3306)
47+
.setServerDatabaseConnectionOptions("?useSSL=false")
48+
.setClientDatabaseAddress("localhost")
49+
.setClientDatabaseName("test2")
50+
.setClientDatabaseUsername("root")
51+
.setClientDatabasePassword("root")
52+
.setClientDatabasePort(3306)
53+
.setClientDatabaseConnectionOptions("?useSSL=false")
54+
.setDBMap(dbMap)
55+
.setSyncInterval(12)
56+
.build();
57+
* }
58+
* </pre>
59+
* @see #DBSyncAgent(sync.db.mysql.DBSyncAgent.Builder)
3160
* @author Arvind Sasikumar
3261
*/
3362
public class DBSyncAgent {
@@ -58,6 +87,11 @@ public class DBSyncAgent {
5887

5988
private DBSynchronizer dbSynchronizer;
6089

90+
/**
91+
* Build the DBSyncAgent class using the Builder pattern.
92+
* @author Arvind Sasikumar
93+
* @see <a href="http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern">Builder Pattern</a>
94+
*/
6195
public static class Builder{
6296

6397
private String serverDatabaseAddress;
@@ -78,96 +112,194 @@ public static class Builder{
78112

79113
private int syncInterval;
80114

115+
/**
116+
* Set the address of the server database.
117+
* @param serverDatabaseAddress address of the server database,
118+
* e.g. "localhost" or "59.23.54.22"
119+
* @return Builder object as per the Builder pattern
120+
*/
81121
public Builder setServerDatabaseAddress(String serverDatabaseAddress){
82122

83123
this.serverDatabaseAddress = serverDatabaseAddress;
84124
return this;
85125
}
86126

127+
/**
128+
* Set the name of the server database.
129+
* @param serverDatabaseName name of the server database, e.g. "testdb"
130+
* @return Builder object as per the Builder pattern
131+
*/
87132
public Builder setServerDatabaseName(String serverDatabaseName){
88133

89134
this.serverDatabaseName = serverDatabaseName;
90135
return this;
91136
}
92137

138+
/**
139+
* Set the username to access the server database.
140+
* @param serverDatabaseUsername username to access the server database
141+
* @return Builder object as per the Builder pattern
142+
*/
93143
public Builder setServerDatabaseUsername(String serverDatabaseUsername){
94144

95145
this.serverDatabaseUsername = serverDatabaseUsername;
96146
return this;
97147
}
98148

149+
/**
150+
* Set the password to access the server database.
151+
* @param serverDatabasePassword password to access the server database
152+
* @return Builder object as per the Builder pattern
153+
*/
99154
public Builder setServerDatabasePassword(String serverDatabasePassword){
100155

101156
this.serverDatabasePassword = serverDatabasePassword;
102157
return this;
103158
}
104159

105-
public Builder setServerDatabaseConnectionOptions(String serverDatabaseConnectionOptions){
160+
/**
161+
* Set the optional connection string to be used for the server
162+
* connection.
163+
* All options in the MySQL connection string as per the Connector/J
164+
* appear here. e.g. "?useSSL=false"
165+
* @param serverDatabaseConnectionOptions optional connection string
166+
* @return Builder object as per the Builder pattern
167+
*/
168+
public Builder setServerDatabaseConnectionOptions(String
169+
serverDatabaseConnectionOptions){
106170

107-
this.serverDatabaseConnectionOptions = serverDatabaseConnectionOptions;
171+
this.serverDatabaseConnectionOptions
172+
= serverDatabaseConnectionOptions;
108173
return this;
109174
}
110175

176+
/**
177+
* Set the port for the server connection.
178+
* @param serverDatabasePort port for the server connection, e.g. 3306
179+
* @return Builder object as per the Builder pattern
180+
*/
111181
public Builder setServerDatabasePort(int serverDatabasePort){
112182

113183
this.serverDatabasePort = serverDatabasePort;
114184
return this;
115185
}
116186

187+
/**
188+
* Set the address of the client database.
189+
* @param clientDatabaseAddress address of the client database,
190+
* e.g. "localhost" or "59.23.54.22"
191+
* @return Builder object as per the Builder pattern
192+
*/
117193
public Builder setClientDatabaseAddress(String clientDatabaseAddress){
118194

119195
this.clientDatabaseAddress = clientDatabaseAddress;
120196
return this;
121197
}
122198

199+
/**
200+
* Set the name of the client database.
201+
* @param clientDatabaseName name of the client database, e.g. "testdb"
202+
* @return Builder object as per the Builder pattern
203+
*/
123204
public Builder setClientDatabaseName(String clientDatabaseName){
124205

125206
this.clientDatabaseName = clientDatabaseName;
126207
return this;
127208
}
128209

210+
/**
211+
* Set the username to access the client database.
212+
* @param clientDatabaseUsername username to access the client database
213+
* @return Builder object as per the Builder pattern
214+
*/
129215
public Builder setClientDatabaseUsername(String clientDatabaseUsername){
130216

131217
this.clientDatabaseUsername = clientDatabaseUsername;
132218
return this;
133219
}
134220

221+
/**
222+
* Set the password to access the client database.
223+
* @param clientDatabasePassword password to access the client database
224+
* @return Builder object as per the Builder pattern
225+
*/
135226
public Builder setClientDatabasePassword(String clientDatabasePassword){
136227

137228
this.clientDatabasePassword = clientDatabasePassword;
138229
return this;
139230
}
140231

141-
public Builder setClientDatabaseConnectionOptions(String clientDatabaseConnectionOptions){
232+
/**
233+
* Set the optional connection string to be used for the client
234+
* connection.
235+
* All options in the MySQL connection string as per the Connector/J
236+
* appear here. e.g. "?useSSL=false"
237+
* @param clientDatabaseConnectionOptions optional connection string
238+
* @return Builder object as per the Builder pattern
239+
*/
240+
public Builder setClientDatabaseConnectionOptions(String
241+
clientDatabaseConnectionOptions){
142242

143-
this.clientDatabaseConnectionOptions = clientDatabaseConnectionOptions;
243+
this.clientDatabaseConnectionOptions
244+
= clientDatabaseConnectionOptions;
144245
return this;
145246
}
146247

248+
/**
249+
* Set the port for the client connection.
250+
* @param clientDatabasePort port for the client connection, e.g. 3306
251+
* @return Builder object as per the Builder pattern
252+
*/
147253
public Builder setClientDatabasePort(int clientDatabasePort){
148254

149255
this.clientDatabasePort = clientDatabasePort;
150256
return this;
151257
}
152258

259+
/**
260+
* Set the database map.
261+
* @param dbMap database map
262+
* @return Builder object as per the Builder pattern
263+
*/
153264
public Builder setDBMap(DBMap dbMap){
154265

155266
this.dbMap = dbMap;
156267
return this;
157268
}
158269

270+
/**
271+
* Set the synchronization interval.
272+
* This specifies the time interval between two successive
273+
* synchronization attempts during a live sync. Specified in seconds.
274+
* @param syncInterval synchronization interval in seconds
275+
* @return Builder object as per the Builder pattern
276+
*/
159277
public Builder setSyncInterval(int syncInterval){
160278

161279
this.syncInterval = syncInterval;
162280
return this;
163281
}
164282

283+
/**
284+
* Build the DBSyncAgent object using the Builder pattern after all
285+
* properties have been set using the Builder class.
286+
* @return a new DBSyncAgent object with all properties set
287+
* @see <a href="http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern">Builder Pattern</a>
288+
*/
165289
public DBSyncAgent build(){
166290

167291
return new DBSyncAgent(this);
168292
}
169293
}
170294

295+
/**
296+
* Create a new DBSyncAgent object using the Builder Pattern.
297+
* This constructor is declared as private and is hence not accessible from
298+
* outside. To use this constructor, one must use the Builder pattern.
299+
* @param builder the builder object used to build the new DBSyncAgent
300+
* object using the Builder pattern
301+
* @see <a href="http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern">Builder Pattern</a>
302+
*/
171303
private DBSyncAgent(Builder builder){
172304

173305
serverDatabaseAddress = builder.serverDatabaseAddress;
@@ -189,11 +321,20 @@ private DBSyncAgent(Builder builder){
189321
syncInterval = builder.syncInterval;
190322
}
191323

324+
/**
325+
* Set the synchronization interval.
326+
* This specifies the time interval between two successive
327+
* synchronization attempts during a live sync. Specified in seconds.
328+
* @param syncInterval synchronization interval in seconds
329+
*/
192330
public void setSyncInterval(int syncInterval){
193331

194332
this.syncInterval = syncInterval;
195333
}
196334

335+
/**
336+
* Connects to the client and server databases as per the set properties.
337+
*/
197338
public void connect(){
198339

199340
try{
@@ -203,13 +344,15 @@ public void connect(){
203344
String connectionString = "jdbc:mysql://" + serverDatabaseAddress + ":" +
204345
serverDatabasePort + "/" + serverDatabaseName +
205346
serverDatabaseConnectionOptions;
206-
serverConnection = DriverManager.getConnection(connectionString,serverDatabaseUsername,serverDatabasePassword);
347+
serverConnection = DriverManager.getConnection(connectionString,
348+
serverDatabaseUsername,serverDatabasePassword);
207349
serverStatement = serverConnection.createStatement();
208350

209351
connectionString = "jdbc:mysql://" + clientDatabaseAddress + ":" +
210352
clientDatabasePort + "/" + clientDatabaseName +
211353
clientDatabaseConnectionOptions;
212-
clientConnection = DriverManager.getConnection(connectionString,clientDatabaseUsername,clientDatabasePassword);
354+
clientConnection = DriverManager.getConnection(connectionString,
355+
clientDatabaseUsername,clientDatabasePassword);
213356
clientStatement = clientConnection.createStatement();
214357
}
215358

@@ -219,22 +362,58 @@ public void connect(){
219362
}
220363
}
221364

365+
/**
366+
* Performs initial synchronization between the client and the server
367+
* databases.
368+
* When synchronization is done for the first time or after a reasonable
369+
* interval of time, call this function before calling the {@link #liveSync()}.
370+
*/
222371
public void sync(){
223372

224373
dbSynchronizer = new DBSynchronizer(serverStatement, clientStatement, dbMap);
225374
Thread dbSynchronizerThread = new Thread(dbSynchronizer);
226375
dbSynchronizerThread.start();
227376
}
228377

378+
/**
379+
* Synchronizes the client and the server databases periodically.
380+
* This synchronization is done periodically as specified using
381+
* {@link #setSyncInterval(int)} or initialized using the Builder pattern.
382+
*/
229383
public void liveSync(){
230384

231-
dbSynchronizer = new DBSynchronizer(serverStatement, clientStatement, dbMap, syncInterval);
385+
dbSynchronizer = new DBSynchronizer(serverStatement, clientStatement,
386+
dbMap, syncInterval);
232387
Thread dbSynchronizerThread = new Thread(dbSynchronizer);
233388
dbSynchronizerThread.start();
234389
}
235390

391+
/**
392+
* Stops the synchronization process.
393+
* The current transaction will be finished before the stop takes places
394+
* to make sure the database is in proper state.
395+
*/
236396
public void stopSync(){
237397

238398
dbSynchronizer.stopSync();
239399
}
400+
401+
/**
402+
* Disconnects the existing client and server connections safely.
403+
* Call this after calling {@link #stopSync()} if a {@link #liveSync()} is
404+
* in progress.
405+
*/
406+
public void disconnect(){
407+
408+
try{
409+
410+
clientStatement.close();
411+
serverStatement.close();
412+
}
413+
414+
catch(Exception e){
415+
416+
e.printStackTrace();
417+
}
418+
}
240419
}

0 commit comments

Comments
 (0)