1 22 package com.mysql.jdbc; 23 24 import java.sql.CallableStatement ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.sql.SQLWarning ; 29 import java.sql.Savepoint ; 30 import java.sql.Statement ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 34 42 public class ReplicationConnection implements java.sql.Connection { 43 private Connection currentConnection; 44 45 private Connection masterConnection; 46 47 private Connection slavesConnection; 48 49 public ReplicationConnection(Properties masterProperties, 50 Properties slaveProperties) throws SQLException { 51 Driver driver = new Driver(); 52 53 this.masterConnection = (com.mysql.jdbc.Connection) driver.connect( 54 "jdbc:mysql:///", masterProperties); 55 this.slavesConnection = (com.mysql.jdbc.Connection) driver.connect( 56 "jdbc:mysql:///", slaveProperties); 57 this.currentConnection = this.masterConnection; 58 } 59 60 65 public synchronized void clearWarnings() throws SQLException { 66 this.currentConnection.clearWarnings(); 67 } 68 69 74 public synchronized void close() throws SQLException { 75 this.masterConnection.close(); 76 this.slavesConnection.close(); 77 } 78 79 84 public synchronized void commit() throws SQLException { 85 this.currentConnection.commit(); 86 } 87 88 93 public Statement createStatement() throws SQLException { 94 return this.currentConnection.createStatement(); 95 } 96 97 102 public synchronized Statement createStatement(int resultSetType, 103 int resultSetConcurrency) throws SQLException { 104 return this.currentConnection.createStatement(resultSetType, 105 resultSetConcurrency); 106 } 107 108 113 public synchronized Statement createStatement(int resultSetType, 114 int resultSetConcurrency, int resultSetHoldability) 115 throws SQLException { 116 return this.currentConnection.createStatement(resultSetType, 117 resultSetConcurrency, resultSetHoldability); 118 } 119 120 125 public synchronized boolean getAutoCommit() throws SQLException { 126 return this.currentConnection.getAutoCommit(); 127 } 128 129 134 public synchronized String getCatalog() throws SQLException { 135 return this.currentConnection.getCatalog(); 136 } 137 138 public synchronized Connection getCurrentConnection() { 139 return this.currentConnection; 140 } 141 142 147 public synchronized int getHoldability() throws SQLException { 148 return this.currentConnection.getHoldability(); 149 } 150 151 public synchronized Connection getMasterConnection() { 152 return this.masterConnection; 153 } 154 155 160 public synchronized DatabaseMetaData getMetaData() throws SQLException { 161 return this.currentConnection.getMetaData(); 162 } 163 164 public synchronized Connection getSlavesConnection() { 165 return this.slavesConnection; 166 } 167 168 173 public synchronized int getTransactionIsolation() throws SQLException { 174 return this.currentConnection.getTransactionIsolation(); 175 } 176 177 182 public synchronized Map getTypeMap() throws SQLException { 183 return this.currentConnection.getTypeMap(); 184 } 185 186 191 public synchronized SQLWarning getWarnings() throws SQLException { 192 return this.currentConnection.getWarnings(); 193 } 194 195 200 public synchronized boolean isClosed() throws SQLException { 201 return this.currentConnection.isClosed(); 202 } 203 204 209 public synchronized boolean isReadOnly() throws SQLException { 210 return this.currentConnection == this.slavesConnection; 211 } 212 213 218 public synchronized String nativeSQL(String sql) throws SQLException { 219 return this.currentConnection.nativeSQL(sql); 220 } 221 222 227 public CallableStatement prepareCall(String sql) throws SQLException { 228 return this.currentConnection.prepareCall(sql); 229 } 230 231 236 public synchronized CallableStatement prepareCall(String sql, 237 int resultSetType, int resultSetConcurrency) throws SQLException { 238 return this.currentConnection.prepareCall(sql, resultSetType, 239 resultSetConcurrency); 240 } 241 242 247 public synchronized CallableStatement prepareCall(String sql, 248 int resultSetType, int resultSetConcurrency, 249 int resultSetHoldability) throws SQLException { 250 return this.currentConnection.prepareCall(sql, resultSetType, 251 resultSetConcurrency, resultSetHoldability); 252 } 253 254 259 public PreparedStatement prepareStatement(String sql) throws SQLException { 260 return this.currentConnection.prepareStatement(sql); 261 } 262 263 268 public synchronized PreparedStatement prepareStatement(String sql, 269 int autoGeneratedKeys) throws SQLException { 270 return this.currentConnection.prepareStatement(sql, autoGeneratedKeys); 271 } 272 273 278 public synchronized PreparedStatement prepareStatement(String sql, 279 int resultSetType, int resultSetConcurrency) throws SQLException { 280 return this.currentConnection.prepareStatement(sql, resultSetType, 281 resultSetConcurrency); 282 } 283 284 290 public synchronized PreparedStatement prepareStatement(String sql, 291 int resultSetType, int resultSetConcurrency, 292 int resultSetHoldability) throws SQLException { 293 return this.currentConnection.prepareStatement(sql, resultSetType, 294 resultSetConcurrency, resultSetHoldability); 295 } 296 297 302 public synchronized PreparedStatement prepareStatement(String sql, 303 int[] columnIndexes) throws SQLException { 304 return this.currentConnection.prepareStatement(sql, columnIndexes); 305 } 306 307 313 public synchronized PreparedStatement prepareStatement(String sql, 314 String [] columnNames) throws SQLException { 315 return this.currentConnection.prepareStatement(sql, columnNames); 316 } 317 318 323 public synchronized void releaseSavepoint(Savepoint savepoint) 324 throws SQLException { 325 this.currentConnection.releaseSavepoint(savepoint); 326 } 327 328 333 public synchronized void rollback() throws SQLException { 334 this.currentConnection.rollback(); 335 } 336 337 342 public synchronized void rollback(Savepoint savepoint) throws SQLException { 343 this.currentConnection.rollback(savepoint); 344 } 345 346 351 public synchronized void setAutoCommit(boolean autoCommit) 352 throws SQLException { 353 this.currentConnection.setAutoCommit(autoCommit); 354 } 355 356 361 public synchronized void setCatalog(String catalog) throws SQLException { 362 this.currentConnection.setCatalog(catalog); 363 } 364 365 370 public synchronized void setHoldability(int holdability) 371 throws SQLException { 372 this.currentConnection.setHoldability(holdability); 373 } 374 375 380 public synchronized void setReadOnly(boolean readOnly) throws SQLException { 381 if (readOnly) { 382 switchToSlavesConnection(); 383 } else { 384 switchToMasterConnection(); 385 } 386 } 387 388 393 public synchronized Savepoint setSavepoint() throws SQLException { 394 return this.currentConnection.setSavepoint(); 395 } 396 397 402 public synchronized Savepoint setSavepoint(String name) throws SQLException { 403 return this.currentConnection.setSavepoint(name); 404 } 405 406 411 public synchronized void setTransactionIsolation(int level) 412 throws SQLException { 413 this.currentConnection.setTransactionIsolation(level); 414 } 415 416 418 423 public synchronized void setTypeMap(Map arg0) throws SQLException { 424 this.currentConnection.setTypeMap(arg0); 425 } 426 427 private synchronized void switchToMasterConnection() throws SQLException { 428 String slaveCatalog = this.slavesConnection.getCatalog(); 429 String masterCatalog = this.masterConnection.getCatalog(); 430 431 if (slaveCatalog != null && !slaveCatalog.equals(masterCatalog)) { 432 this.masterConnection.setCatalog(slaveCatalog); 433 } else if (masterCatalog != null) { 434 this.masterConnection.setCatalog(null); 435 } 436 437 boolean slavesAutoCommit = this.slavesConnection.getAutoCommit(); 438 439 if (this.masterConnection.getAutoCommit() != slavesAutoCommit) { 440 this.masterConnection.setAutoCommit(slavesAutoCommit); 441 } 442 443 int slavesTransactionIsolation = this.slavesConnection 444 .getTransactionIsolation(); 445 446 if (this.masterConnection.getTransactionIsolation() != slavesTransactionIsolation) { 447 this.masterConnection 448 .setTransactionIsolation(slavesTransactionIsolation); 449 } 450 451 this.currentConnection = this.masterConnection; 452 } 453 454 private synchronized void switchToSlavesConnection() throws SQLException { 455 String slaveCatalog = this.slavesConnection.getCatalog(); 456 String masterCatalog = this.masterConnection.getCatalog(); 457 458 if (masterCatalog != null && !masterCatalog.equals(slaveCatalog)) { 459 this.slavesConnection.setCatalog(masterCatalog); 460 } else if (slaveCatalog != null) { 461 this.slavesConnection.setCatalog(null); 462 } 463 464 boolean masterAutoCommit = this.masterConnection.getAutoCommit(); 465 466 if (this.slavesConnection.getAutoCommit() != masterAutoCommit) { 467 this.slavesConnection.setAutoCommit(masterAutoCommit); 468 } 469 470 int masterTransactionIsolation = this.masterConnection 471 .getTransactionIsolation(); 472 473 if (this.slavesConnection.getTransactionIsolation() != masterTransactionIsolation) { 474 this.slavesConnection 475 .setTransactionIsolation(masterTransactionIsolation); 476 } 477 this.currentConnection = this.slavesConnection; 478 479 this.slavesConnection.setAutoCommit(this.masterConnection 480 .getAutoCommit()); 481 this.slavesConnection.setTransactionIsolation(this.masterConnection 482 .getTransactionIsolation()); 483 } 484 } 485 | Popular Tags |