KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > ReplicationConnection


1 /*
2  Copyright (C) 2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22 package com.mysql.jdbc;
23
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.SQLWarning JavaDoc;
29 import java.sql.Savepoint JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * Connection that opens two connections, one two a replication master, and
36  * another to one or more slaves, and decides to use master when the connection
37  * is not read-only, and use slave(s) when the connection is read-only.
38  *
39  * @version $Id: ReplicationConnection.java,v 1.1.2.1 2005/05/13 18:58:38
40  * mmatthews Exp $
41  */

42 public class ReplicationConnection implements java.sql.Connection JavaDoc {
43     private Connection currentConnection;
44
45     private Connection masterConnection;
46
47     private Connection slavesConnection;
48
49     public ReplicationConnection(Properties JavaDoc masterProperties,
50             Properties JavaDoc slaveProperties) throws SQLException JavaDoc {
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     /*
61      * (non-Javadoc)
62      *
63      * @see java.sql.Connection#clearWarnings()
64      */

65     public synchronized void clearWarnings() throws SQLException JavaDoc {
66         this.currentConnection.clearWarnings();
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see java.sql.Connection#close()
73      */

74     public synchronized void close() throws SQLException JavaDoc {
75         this.masterConnection.close();
76         this.slavesConnection.close();
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see java.sql.Connection#commit()
83      */

84     public synchronized void commit() throws SQLException JavaDoc {
85         this.currentConnection.commit();
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see java.sql.Connection#createStatement()
92      */

93     public Statement createStatement() throws SQLException JavaDoc {
94         return this.currentConnection.createStatement();
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see java.sql.Connection#createStatement(int, int)
101      */

102     public synchronized Statement createStatement(int resultSetType,
103             int resultSetConcurrency) throws SQLException JavaDoc {
104         return this.currentConnection.createStatement(resultSetType,
105                 resultSetConcurrency);
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see java.sql.Connection#createStatement(int, int, int)
112      */

113     public synchronized Statement createStatement(int resultSetType,
114             int resultSetConcurrency, int resultSetHoldability)
115             throws SQLException JavaDoc {
116         return this.currentConnection.createStatement(resultSetType,
117                 resultSetConcurrency, resultSetHoldability);
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see java.sql.Connection#getAutoCommit()
124      */

125     public synchronized boolean getAutoCommit() throws SQLException JavaDoc {
126         return this.currentConnection.getAutoCommit();
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see java.sql.Connection#getCatalog()
133      */

134     public synchronized String JavaDoc getCatalog() throws SQLException JavaDoc {
135         return this.currentConnection.getCatalog();
136     }
137
138     public synchronized Connection getCurrentConnection() {
139         return this.currentConnection;
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see java.sql.Connection#getHoldability()
146      */

147     public synchronized int getHoldability() throws SQLException JavaDoc {
148         return this.currentConnection.getHoldability();
149     }
150
151     public synchronized Connection getMasterConnection() {
152         return this.masterConnection;
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see java.sql.Connection#getMetaData()
159      */

160     public synchronized DatabaseMetaData getMetaData() throws SQLException JavaDoc {
161         return this.currentConnection.getMetaData();
162     }
163
164     public synchronized Connection getSlavesConnection() {
165         return this.slavesConnection;
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see java.sql.Connection#getTransactionIsolation()
172      */

173     public synchronized int getTransactionIsolation() throws SQLException JavaDoc {
174         return this.currentConnection.getTransactionIsolation();
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see java.sql.Connection#getTypeMap()
181      */

182     public synchronized Map JavaDoc getTypeMap() throws SQLException JavaDoc {
183         return this.currentConnection.getTypeMap();
184     }
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see java.sql.Connection#getWarnings()
190      */

191     public synchronized SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
192         return this.currentConnection.getWarnings();
193     }
194
195     /*
196      * (non-Javadoc)
197      *
198      * @see java.sql.Connection#isClosed()
199      */

200     public synchronized boolean isClosed() throws SQLException JavaDoc {
201         return this.currentConnection.isClosed();
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see java.sql.Connection#isReadOnly()
208      */

209     public synchronized boolean isReadOnly() throws SQLException JavaDoc {
210         return this.currentConnection == this.slavesConnection;
211     }
212
213     /*
214      * (non-Javadoc)
215      *
216      * @see java.sql.Connection#nativeSQL(java.lang.String)
217      */

218     public synchronized String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
219         return this.currentConnection.nativeSQL(sql);
220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see java.sql.Connection#prepareCall(java.lang.String)
226      */

227     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
228         return this.currentConnection.prepareCall(sql);
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
235      */

236     public synchronized CallableStatement prepareCall(String JavaDoc sql,
237             int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
238         return this.currentConnection.prepareCall(sql, resultSetType,
239                 resultSetConcurrency);
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
246      */

247     public synchronized CallableStatement prepareCall(String JavaDoc sql,
248             int resultSetType, int resultSetConcurrency,
249             int resultSetHoldability) throws SQLException JavaDoc {
250         return this.currentConnection.prepareCall(sql, resultSetType,
251                 resultSetConcurrency, resultSetHoldability);
252     }
253
254     /*
255      * (non-Javadoc)
256      *
257      * @see java.sql.Connection#prepareStatement(java.lang.String)
258      */

259     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
260         return this.currentConnection.prepareStatement(sql);
261     }
262
263     /*
264      * (non-Javadoc)
265      *
266      * @see java.sql.Connection#prepareStatement(java.lang.String, int)
267      */

268     public synchronized PreparedStatement prepareStatement(String JavaDoc sql,
269             int autoGeneratedKeys) throws SQLException JavaDoc {
270         return this.currentConnection.prepareStatement(sql, autoGeneratedKeys);
271     }
272
273     /*
274      * (non-Javadoc)
275      *
276      * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
277      */

278     public synchronized PreparedStatement prepareStatement(String JavaDoc sql,
279             int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
280         return this.currentConnection.prepareStatement(sql, resultSetType,
281                 resultSetConcurrency);
282     }
283
284     /*
285      * (non-Javadoc)
286      *
287      * @see java.sql.Connection#prepareStatement(java.lang.String, int, int,
288      * int)
289      */

290     public synchronized PreparedStatement prepareStatement(String JavaDoc sql,
291             int resultSetType, int resultSetConcurrency,
292             int resultSetHoldability) throws SQLException JavaDoc {
293         return this.currentConnection.prepareStatement(sql, resultSetType,
294                 resultSetConcurrency, resultSetHoldability);
295     }
296
297     /*
298      * (non-Javadoc)
299      *
300      * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
301      */

302     public synchronized PreparedStatement prepareStatement(String JavaDoc sql,
303             int[] columnIndexes) throws SQLException JavaDoc {
304         return this.currentConnection.prepareStatement(sql, columnIndexes);
305     }
306
307     /*
308      * (non-Javadoc)
309      *
310      * @see java.sql.Connection#prepareStatement(java.lang.String,
311      * java.lang.String[])
312      */

313     public synchronized PreparedStatement prepareStatement(String JavaDoc sql,
314             String JavaDoc[] columnNames) throws SQLException JavaDoc {
315         return this.currentConnection.prepareStatement(sql, columnNames);
316     }
317
318     /*
319      * (non-Javadoc)
320      *
321      * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
322      */

323     public synchronized void releaseSavepoint(Savepoint JavaDoc savepoint)
324             throws SQLException JavaDoc {
325         this.currentConnection.releaseSavepoint(savepoint);
326     }
327
328     /*
329      * (non-Javadoc)
330      *
331      * @see java.sql.Connection#rollback()
332      */

333     public synchronized void rollback() throws SQLException JavaDoc {
334         this.currentConnection.rollback();
335     }
336
337     /*
338      * (non-Javadoc)
339      *
340      * @see java.sql.Connection#rollback(java.sql.Savepoint)
341      */

342     public synchronized void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
343         this.currentConnection.rollback(savepoint);
344     }
345
346     /*
347      * (non-Javadoc)
348      *
349      * @see java.sql.Connection#setAutoCommit(boolean)
350      */

351     public synchronized void setAutoCommit(boolean autoCommit)
352             throws SQLException JavaDoc {
353         this.currentConnection.setAutoCommit(autoCommit);
354     }
355
356     /*
357      * (non-Javadoc)
358      *
359      * @see java.sql.Connection#setCatalog(java.lang.String)
360      */

361     public synchronized void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
362         this.currentConnection.setCatalog(catalog);
363     }
364
365     /*
366      * (non-Javadoc)
367      *
368      * @see java.sql.Connection#setHoldability(int)
369      */

370     public synchronized void setHoldability(int holdability)
371             throws SQLException JavaDoc {
372         this.currentConnection.setHoldability(holdability);
373     }
374
375     /*
376      * (non-Javadoc)
377      *
378      * @see java.sql.Connection#setReadOnly(boolean)
379      */

380     public synchronized void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
381         if (readOnly) {
382             switchToSlavesConnection();
383         } else {
384             switchToMasterConnection();
385         }
386     }
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see java.sql.Connection#setSavepoint()
392      */

393     public synchronized Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
394         return this.currentConnection.setSavepoint();
395     }
396
397     /*
398      * (non-Javadoc)
399      *
400      * @see java.sql.Connection#setSavepoint(java.lang.String)
401      */

402     public synchronized Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
403         return this.currentConnection.setSavepoint(name);
404     }
405
406     /*
407      * (non-Javadoc)
408      *
409      * @see java.sql.Connection#setTransactionIsolation(int)
410      */

411     public synchronized void setTransactionIsolation(int level)
412             throws SQLException JavaDoc {
413         this.currentConnection.setTransactionIsolation(level);
414     }
415
416     // For testing
417

418     /*
419      * (non-Javadoc)
420      *
421      * @see java.sql.Connection#setTypeMap(java.util.Map)
422      */

423     public synchronized void setTypeMap(Map JavaDoc arg0) throws SQLException JavaDoc {
424         this.currentConnection.setTypeMap(arg0);
425     }
426
427     private synchronized void switchToMasterConnection() throws SQLException JavaDoc {
428         String JavaDoc slaveCatalog = this.slavesConnection.getCatalog();
429         String JavaDoc 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 JavaDoc {
455         String JavaDoc slaveCatalog = this.slavesConnection.getCatalog();
456         String JavaDoc 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