@@ -46,15 +46,15 @@ public class DB {
46
46
47
47
public static final String DEFAULT_NAME = "default" ;
48
48
49
- private final String dbName ;
49
+ private final String name ;
50
50
51
51
/**
52
52
* Creates a new DB object representing a connection to a DB.
53
53
*
54
- * @param dbName logical name for a database.
54
+ * @param name logical name for a database.
55
55
*/
56
- public DB (String dbName ) {
57
- this .dbName = dbName ;
56
+ public DB (String name ) {
57
+ this .name = name ;
58
58
}
59
59
60
60
/**
@@ -66,29 +66,29 @@ public DB(String dbName){
66
66
* @param password password.
67
67
*/
68
68
public void open (String driver , String url , String user , String password ) {
69
- checkExistingConnection (dbName );
69
+ checkExistingConnection (name );
70
70
try {
71
71
Class .forName (driver );
72
72
Connection connection = DriverManager .getConnection (url , user , password );
73
- ConnectionsAccess .attach (dbName , connection , url );
73
+ ConnectionsAccess .attach (name , connection , url );
74
74
} catch (Exception e ) {
75
75
throw new InitException ("Failed to connect to JDBC URL: " + url , e );
76
76
}
77
77
}
78
78
79
79
/**
80
80
* Opens a new connection in case additional driver-specific parameters need to be passed in.
81
- *
81
+ *
82
82
* @param driver driver class name
83
83
* @param url JDBC URL
84
84
* @param props connection properties
85
85
*/
86
86
public void open (String driver , String url , Properties props ) {
87
- checkExistingConnection (dbName );
87
+ checkExistingConnection (name );
88
88
try {
89
89
Class .forName (driver );
90
90
Connection connection = DriverManager .getConnection (url , props );
91
- ConnectionsAccess .attach (dbName , connection , url );
91
+ ConnectionsAccess .attach (name , connection , url );
92
92
} catch (Exception e ) {
93
93
throw new InitException ("Failed to connect to JDBC URL: " + url , e );
94
94
}
@@ -98,15 +98,15 @@ public void open(String driver, String url, Properties props) {
98
98
* Opens a connection from JNDI based on a registered name. This assumes that there is a <code>jndi.properties</code>
99
99
* file with proper JNDI configuration in it.
100
100
*
101
- * @param jndiName name of a configured data source.
101
+ * @param jndiName name of a configured data source.
102
102
*/
103
103
public void open (String jndiName ) {
104
- checkExistingConnection (dbName );
104
+ checkExistingConnection (name );
105
105
try {
106
106
Context ctx = new InitialContext ();
107
107
DataSource ds = (DataSource ) ctx .lookup (jndiName );
108
108
Connection connection = ds .getConnection ();
109
- ConnectionsAccess .attach (dbName , connection , jndiName );
109
+ ConnectionsAccess .attach (name , connection , jndiName );
110
110
} catch (Exception e ) {
111
111
throw new InitException ("Failed to connect to JNDI name: " + jndiName , e );
112
112
}
@@ -117,8 +117,8 @@ public void open(String jndiName) {
117
117
*
118
118
* @param connection instance of connection to attach to current thread.
119
119
*/
120
- public void attach (Connection connection ){
121
- ConnectionsAccess .attach (dbName , connection , "" );
120
+ public void attach (Connection connection ) {
121
+ ConnectionsAccess .attach (name , connection , "" );
122
122
}
123
123
124
124
/**
@@ -128,13 +128,12 @@ public void attach(Connection connection){
128
128
* @return instance of a connection detached from current thread by name passed to constructor.
129
129
*/
130
130
public Connection detach () {
131
-
132
- Connection connection = ConnectionsAccess .getConnection (dbName );
131
+ Connection connection = ConnectionsAccess .getConnection (name );
133
132
try {
134
- if (connection == null ){
135
- throw new DBException ("cannot detach connection '" + dbName + "' because it is not available" );
133
+ if (connection == null ) {
134
+ throw new DBException ("cannot detach connection '" + name + "' because it is not available" );
136
135
}
137
- ConnectionsAccess .detach (dbName ); // lets free the thread from connection
136
+ ConnectionsAccess .detach (name ); // let's free the thread from connection
138
137
StatementCache .instance ().cleanStatementCache (connection );
139
138
} catch (DBException e ) {
140
139
logger .warn ("Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!" , e );
@@ -147,11 +146,11 @@ public Connection detach() {
147
146
*
148
147
* @param datasource datasource will be used to acquire a connection.
149
148
*/
150
- public void open (DataSource datasource ){
151
- checkExistingConnection (dbName );
149
+ public void open (DataSource datasource ) {
150
+ checkExistingConnection (name );
152
151
try {
153
152
Connection connection = datasource .getConnection ();
154
- ConnectionsAccess .attach (dbName , connection , datasource .toString ());
153
+ ConnectionsAccess .attach (name , connection , datasource .toString ());
155
154
} catch (SQLException e ) {
156
155
throw new InitException (e );
157
156
}
@@ -162,16 +161,16 @@ public void open(DataSource datasource){
162
161
* Opens a new connection from JNDI data source by name using explicit JNDI properties. This method can be used in cases
163
162
* when file <code>jndi.properties</code> cannot be easily updated.
164
163
*
165
- * @param jndiName name of JNDI data source.
164
+ * @param jndiName name of JNDI data source.
166
165
* @param jndiProperties JNDI properties
167
166
*/
168
167
public void open (String jndiName , Properties jndiProperties ) {
169
- checkExistingConnection (dbName );
168
+ checkExistingConnection (name );
170
169
try {
171
170
Context ctx = new InitialContext (jndiProperties );
172
171
DataSource ds = (DataSource ) ctx .lookup (jndiName );
173
172
Connection connection = ds .getConnection ();
174
- ConnectionsAccess .attach (dbName , connection ,
173
+ ConnectionsAccess .attach (name , connection ,
175
174
jndiProperties .contains ("url" ) ? jndiProperties .getProperty ("url" ) : jndiName );
176
175
} catch (Exception e ) {
177
176
throw new InitException ("Failed to connect to JNDI name: " + jndiName , e );
@@ -181,23 +180,23 @@ public void open(String jndiName, Properties jndiProperties) {
181
180
182
181
/**
183
182
* This method is used internally by framework.
184
- *
183
+ *
185
184
* @param spec specification for a JDBC connection.
186
185
*/
187
- public void open (ConnectionSpec spec ){
188
- checkExistingConnection (dbName );
189
- if (spec instanceof ConnectionJdbcSpec ){
190
- openJdbc ((ConnectionJdbcSpec )spec );
191
- }else if (spec instanceof ConnectionJndiSpec ){
192
- openJndi ((ConnectionJndiSpec )spec );
193
- }else {
186
+ public void open (ConnectionSpec spec ) {
187
+ checkExistingConnection (name );
188
+ if (spec instanceof ConnectionJdbcSpec ) {
189
+ openJdbc ((ConnectionJdbcSpec ) spec );
190
+ } else if (spec instanceof ConnectionJndiSpec ) {
191
+ openJndi ((ConnectionJndiSpec ) spec );
192
+ } else {
194
193
throw new IllegalArgumentException ("this spec not supported: " + spec .getClass ());
195
194
}
196
195
}
197
196
198
- private void checkExistingConnection (String dbName ) {
199
- if ( null != ConnectionsAccess .getConnection (dbName )) {
200
- throw new DBException ("Cannot open a new connection because existing connection is still on current thread, dbName : " + dbName + ", connection instance: " + connection ()
197
+ private void checkExistingConnection (String name ) {
198
+ if ( null != ConnectionsAccess .getConnection (name )) {
199
+ throw new DBException ("Cannot open a new connection because existing connection is still on current thread, name : " + name + ", connection instance: " + connection ()
201
200
+ ". This might indicate a logical error in your application." );
202
201
}
203
202
}
@@ -233,13 +232,13 @@ private void openJndi(ConnectionJndiSpec spec) {
233
232
* This method is used internally by framework.
234
233
*
235
234
* @param context context.
236
- * @param jndiName JNDI name.
235
+ * @param jndiName JNDI name.
237
236
*/
238
237
private void openContext (InitialContext context , String jndiName ) {
239
238
try {
240
239
DataSource ds = (DataSource ) context .lookup (jndiName );
241
240
Connection connection = ds .getConnection ();
242
- ConnectionsAccess .attach (dbName , connection , jndiName );
241
+ ConnectionsAccess .attach (name , connection , jndiName );
243
242
} catch (Exception e ) {
244
243
throw new InitException ("Failed to connect to JNDI name: " + jndiName , e );
245
244
}
@@ -259,9 +258,9 @@ public void close() {
259
258
*/
260
259
public void close (boolean suppressWarning ) {
261
260
try {
262
- Connection connection = ConnectionsAccess .getConnection (dbName );
261
+ Connection connection = ConnectionsAccess .getConnection (name );
263
262
if (connection == null ){
264
- throw new DBException ("cannot close connection '" + dbName + "' because it is not available" );
263
+ throw new DBException ("cannot close connection '" + name + "' because it is not available" );
265
264
}
266
265
StatementCache .instance ().cleanStatementCache (connection );
267
266
connection .close ();
@@ -270,8 +269,8 @@ public void close(boolean suppressWarning) {
270
269
if (!suppressWarning ) {
271
270
logger .warn ("Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!" , e );
272
271
}
273
- }finally {
274
- ConnectionsAccess .detach (dbName ); // lets free the thread from connection
272
+ } finally {
273
+ ConnectionsAccess .detach (name ); // let's free the thread from connection
275
274
}
276
275
}
277
276
@@ -708,9 +707,9 @@ private void logException(String message, Exception e) {
708
707
*/
709
708
public void openTransaction () {
710
709
try {
711
- Connection c = ConnectionsAccess .getConnection (dbName );
712
- if (c == null ){
713
- throw new DBException ("Cannot open transaction, connection '" + dbName + "' not available" );
710
+ Connection c = ConnectionsAccess .getConnection (name );
711
+ if (c == null ) {
712
+ throw new DBException ("Cannot open transaction, connection '" + name + "' not available" );
714
713
}
715
714
c .setAutoCommit (false );
716
715
LogFilter .log (logger , "Transaction opened" );
@@ -725,9 +724,9 @@ public void openTransaction() {
725
724
*/
726
725
public void commitTransaction () {
727
726
try {
728
- Connection c = ConnectionsAccess .getConnection (dbName );
729
- if (c == null ){
730
- throw new DBException ("Cannot commit transaction, connection '" + dbName + "' not available" );
727
+ Connection c = ConnectionsAccess .getConnection (name );
728
+ if (c == null ) {
729
+ throw new DBException ("Cannot commit transaction, connection '" + name + "' not available" );
731
730
}
732
731
c .commit ();
733
732
LogFilter .log (logger , "Transaction committed" );
@@ -741,9 +740,9 @@ public void commitTransaction() {
741
740
*/
742
741
public void rollbackTransaction () {
743
742
try {
744
- Connection c = ConnectionsAccess .getConnection (dbName );
743
+ Connection c = ConnectionsAccess .getConnection (name );
745
744
if (c == null ) {
746
- throw new DBException ("Cannot rollback transaction, connection '" + dbName + "' not available" );
745
+ throw new DBException ("Cannot rollback transaction, connection '" + name + "' not available" );
747
746
}
748
747
c .rollback ();
749
748
LogFilter .log (logger , "Transaction rolled back" );
@@ -755,13 +754,13 @@ public void rollbackTransaction() {
755
754
/**
756
755
* Provides connection from current thread.
757
756
*
758
- * @return connection from current thread.
757
+ * @return connection from current thread.
759
758
*/
760
759
public Connection connection () {
761
- Connection connection = ConnectionsAccess .getConnection (dbName );
762
- if (connection == null )
763
- throw new DBException ("there is no connection '" + dbName + "' on this thread, are you sure you opened it?" );
764
-
760
+ Connection connection = ConnectionsAccess .getConnection (name );
761
+ if (connection == null ) {
762
+ throw new DBException ("there is no connection '" + name + "' on this thread, are you sure you opened it?" );
763
+ }
765
764
return connection ;
766
765
}
767
766
@@ -770,8 +769,8 @@ public Connection connection() {
770
769
*
771
770
* @return true if finds connection on current thread, false if not.
772
771
*/
773
- public boolean hasConnection (){
774
- return null != ConnectionsAccess .getConnection (dbName );
772
+ public boolean hasConnection () {
773
+ return null != ConnectionsAccess .getConnection (name );
775
774
}
776
775
777
776
/**
0 commit comments