EMMA Coverage Report (generated Fri May 26 15:35:26 CDT 2006)
[all classes][com.mysql.jdbc]

COVERAGE SUMMARY FOR SOURCE FILE [ReplicationConnection.java]

nameclass, %method, %block, %line, %
ReplicationConnection.java100% (1/1)26%  (11/43)41%  (122/295)46%  (37/80)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ReplicationConnection100% (1/1)26%  (11/43)41%  (122/295)46%  (37/80)
clearWarnings (): void 0%   (0/1)0%   (0/4)0%   (0/2)
commit (): void 0%   (0/1)0%   (0/4)0%   (0/2)
createStatement (int, int): Statement 0%   (0/1)0%   (0/6)0%   (0/1)
createStatement (int, int, int): Statement 0%   (0/1)0%   (0/7)0%   (0/1)
getAutoCommit (): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
getCurrentConnection (): Connection 0%   (0/1)0%   (0/3)0%   (0/1)
getHoldability (): int 0%   (0/1)0%   (0/4)0%   (0/1)
getMetaData (): DatabaseMetaData 0%   (0/1)0%   (0/4)0%   (0/1)
getTransactionIsolation (): int 0%   (0/1)0%   (0/4)0%   (0/1)
getTypeMap (): Map 0%   (0/1)0%   (0/4)0%   (0/1)
getWarnings (): SQLWarning 0%   (0/1)0%   (0/4)0%   (0/1)
isClosed (): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
isReadOnly (): boolean 0%   (0/1)0%   (0/9)0%   (0/1)
nativeSQL (String): String 0%   (0/1)0%   (0/5)0%   (0/1)
prepareCall (String): CallableStatement 0%   (0/1)0%   (0/5)0%   (0/1)
prepareCall (String, int, int): CallableStatement 0%   (0/1)0%   (0/7)0%   (0/1)
prepareCall (String, int, int, int): CallableStatement 0%   (0/1)0%   (0/8)0%   (0/1)
prepareStatement (String): PreparedStatement 0%   (0/1)0%   (0/5)0%   (0/1)
prepareStatement (String, String []): PreparedStatement 0%   (0/1)0%   (0/6)0%   (0/1)
prepareStatement (String, int []): PreparedStatement 0%   (0/1)0%   (0/6)0%   (0/1)
prepareStatement (String, int): PreparedStatement 0%   (0/1)0%   (0/6)0%   (0/1)
prepareStatement (String, int, int): PreparedStatement 0%   (0/1)0%   (0/7)0%   (0/1)
prepareStatement (String, int, int, int): PreparedStatement 0%   (0/1)0%   (0/8)0%   (0/1)
releaseSavepoint (Savepoint): void 0%   (0/1)0%   (0/5)0%   (0/2)
rollback (): void 0%   (0/1)0%   (0/4)0%   (0/2)
rollback (Savepoint): void 0%   (0/1)0%   (0/5)0%   (0/2)
setAutoCommit (boolean): void 0%   (0/1)0%   (0/5)0%   (0/2)
setHoldability (int): void 0%   (0/1)0%   (0/5)0%   (0/2)
setSavepoint (): Savepoint 0%   (0/1)0%   (0/4)0%   (0/1)
setSavepoint (String): Savepoint 0%   (0/1)0%   (0/5)0%   (0/1)
setTransactionIsolation (int): void 0%   (0/1)0%   (0/5)0%   (0/2)
setTypeMap (Map): void 0%   (0/1)0%   (0/5)0%   (0/2)
swapConnections (Connection, Connection): void 100% (1/1)87%  (39/45)86%  (12/14)
ReplicationConnection (Properties, Properties): void 100% (1/1)100% (25/25)100% (6/6)
close (): void 100% (1/1)100% (7/7)100% (3/3)
createStatement (): Statement 100% (1/1)100% (4/4)100% (1/1)
getCatalog (): String 100% (1/1)100% (4/4)100% (1/1)
getMasterConnection (): Connection 100% (1/1)100% (3/3)100% (1/1)
getSlavesConnection (): Connection 100% (1/1)100% (3/3)100% (1/1)
setCatalog (String): void 100% (1/1)100% (5/5)100% (2/2)
setReadOnly (boolean): void 100% (1/1)100% (18/18)100% (6/6)
switchToMasterConnection (): void 100% (1/1)100% (7/7)100% (2/2)
switchToSlavesConnection (): void 100% (1/1)100% (7/7)100% (2/2)

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 */
22package com.mysql.jdbc;
23 
24import java.sql.CallableStatement;
25import java.sql.DatabaseMetaData;
26import java.sql.PreparedStatement;
27import java.sql.SQLException;
28import java.sql.SQLWarning;
29import java.sql.Savepoint;
30import java.sql.Statement;
31import java.util.Map;
32import java.util.Properties;
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 */
42public 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        /*
61         * (non-Javadoc)
62         * 
63         * @see java.sql.Connection#clearWarnings()
64         */
65        public synchronized void clearWarnings() throws SQLException {
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 {
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 {
85                this.currentConnection.commit();
86        }
87 
88        /*
89         * (non-Javadoc)
90         * 
91         * @see java.sql.Connection#createStatement()
92         */
93        public Statement createStatement() throws SQLException {
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 {
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 {
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 {
126                return this.currentConnection.getAutoCommit();
127        }
128 
129        /*
130         * (non-Javadoc)
131         * 
132         * @see java.sql.Connection#getCatalog()
133         */
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        /*
143         * (non-Javadoc)
144         * 
145         * @see java.sql.Connection#getHoldability()
146         */
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        /*
156         * (non-Javadoc)
157         * 
158         * @see java.sql.Connection#getMetaData()
159         */
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        /*
169         * (non-Javadoc)
170         * 
171         * @see java.sql.Connection#getTransactionIsolation()
172         */
173        public synchronized int getTransactionIsolation() throws SQLException {
174                return this.currentConnection.getTransactionIsolation();
175        }
176 
177        /*
178         * (non-Javadoc)
179         * 
180         * @see java.sql.Connection#getTypeMap()
181         */
182        public synchronized Map getTypeMap() throws SQLException {
183                return this.currentConnection.getTypeMap();
184        }
185 
186        /*
187         * (non-Javadoc)
188         * 
189         * @see java.sql.Connection#getWarnings()
190         */
191        public synchronized SQLWarning getWarnings() throws SQLException {
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 {
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 {
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 nativeSQL(String sql) throws SQLException {
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 sql) throws SQLException {
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 sql,
237                        int resultSetType, int resultSetConcurrency) throws SQLException {
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 sql,
248                        int resultSetType, int resultSetConcurrency,
249                        int resultSetHoldability) throws SQLException {
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 sql) throws SQLException {
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 sql,
269                        int autoGeneratedKeys) throws SQLException {
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 sql,
279                        int resultSetType, int resultSetConcurrency) throws SQLException {
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 sql,
291                        int resultSetType, int resultSetConcurrency,
292                        int resultSetHoldability) throws SQLException {
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 sql,
303                        int[] columnIndexes) throws SQLException {
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 sql,
314                        String[] columnNames) throws SQLException {
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 savepoint)
324                        throws SQLException {
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 {
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 savepoint) throws SQLException {
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 {
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 catalog) throws SQLException {
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 {
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 {
381                if (readOnly) {
382                        if (currentConnection != slavesConnection) {
383                                switchToSlavesConnection();
384                        }
385                } else {
386                        if (currentConnection != masterConnection) {
387                                switchToMasterConnection();
388                        }
389                }
390        }
391 
392        /*
393         * (non-Javadoc)
394         * 
395         * @see java.sql.Connection#setSavepoint()
396         */
397        public synchronized Savepoint setSavepoint() throws SQLException {
398                return this.currentConnection.setSavepoint();
399        }
400 
401        /*
402         * (non-Javadoc)
403         * 
404         * @see java.sql.Connection#setSavepoint(java.lang.String)
405         */
406        public synchronized Savepoint setSavepoint(String name) throws SQLException {
407                return this.currentConnection.setSavepoint(name);
408        }
409 
410        /*
411         * (non-Javadoc)
412         * 
413         * @see java.sql.Connection#setTransactionIsolation(int)
414         */
415        public synchronized void setTransactionIsolation(int level)
416                        throws SQLException {
417                this.currentConnection.setTransactionIsolation(level);
418        }
419 
420        // For testing
421 
422        /*
423         * (non-Javadoc)
424         * 
425         * @see java.sql.Connection#setTypeMap(java.util.Map)
426         */
427        public synchronized void setTypeMap(Map arg0) throws SQLException {
428                this.currentConnection.setTypeMap(arg0);
429        }
430 
431        private synchronized void switchToMasterConnection() throws SQLException {
432                swapConnections(this.masterConnection, this.slavesConnection);
433        }
434 
435        private synchronized void switchToSlavesConnection() throws SQLException {
436                swapConnections(this.slavesConnection, this.masterConnection);
437        }
438        
439        /**
440         * Swaps current context (catalog, autocommit and txn_isolation) from
441         * sourceConnection to targetConnection, and makes targetConnection
442         * the "current" connection that will be used for queries.
443         * 
444         * @param sourceConnection the connection to swap from
445         * @param targetConnection the connection to swap to
446         * 
447         * @throws SQLException if an error occurs
448         */
449        private synchronized void swapConnections(Connection sourceConnection, 
450                        Connection targetConnection) throws SQLException {
451                String targetCatalog = targetConnection.getCatalog();
452                String sourceCatalog = sourceConnection.getCatalog();
453 
454                if (sourceCatalog != null && !sourceCatalog.equals(targetCatalog)) {
455                        targetConnection.setCatalog(targetCatalog);
456                } else if (targetCatalog != null) {
457                        targetConnection.setCatalog(targetCatalog);
458                }
459 
460                boolean sourceAutoCommit = sourceConnection.getAutoCommit();
461 
462                if (targetConnection.getAutoCommit() != sourceAutoCommit) {
463                        targetConnection.setAutoCommit(sourceAutoCommit);
464                }
465 
466                int sourceTransactionIsolation = sourceConnection
467                                .getTransactionIsolation();
468 
469                if (targetConnection.getTransactionIsolation() != sourceTransactionIsolation) {
470                        targetConnection
471                                        .setTransactionIsolation(sourceTransactionIsolation);
472                }
473                
474                this.currentConnection = targetConnection;
475        }
476}

[all classes][com.mysql.jdbc]
EMMA 2.0.4217 (C) Vladimir Roubtsov