Skip to content

Commit 3c3372a

Browse files
committed
#374 Renamed redundant dbName property in DB to name
1 parent 07be740 commit 3c3372a

File tree

1 file changed

+56
-57
lines changed
  • activejdbc/src/main/java/org/javalite/activejdbc

1 file changed

+56
-57
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/DB.java

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public class DB {
4646

4747
public static final String DEFAULT_NAME = "default";
4848

49-
private final String dbName;
49+
private final String name;
5050

5151
/**
5252
* Creates a new DB object representing a connection to a DB.
5353
*
54-
* @param dbName logical name for a database.
54+
* @param name logical name for a database.
5555
*/
56-
public DB(String dbName){
57-
this.dbName = dbName;
56+
public DB(String name) {
57+
this.name = name;
5858
}
5959

6060
/**
@@ -66,29 +66,29 @@ public DB(String dbName){
6666
* @param password password.
6767
*/
6868
public void open(String driver, String url, String user, String password) {
69-
checkExistingConnection(dbName);
69+
checkExistingConnection(name);
7070
try {
7171
Class.forName(driver);
7272
Connection connection = DriverManager.getConnection(url, user, password);
73-
ConnectionsAccess.attach(dbName, connection, url);
73+
ConnectionsAccess.attach(name, connection, url);
7474
} catch (Exception e) {
7575
throw new InitException("Failed to connect to JDBC URL: " + url, e);
7676
}
7777
}
7878

7979
/**
8080
* Opens a new connection in case additional driver-specific parameters need to be passed in.
81-
*
81+
*
8282
* @param driver driver class name
8383
* @param url JDBC URL
8484
* @param props connection properties
8585
*/
8686
public void open(String driver, String url, Properties props) {
87-
checkExistingConnection(dbName);
87+
checkExistingConnection(name);
8888
try {
8989
Class.forName(driver);
9090
Connection connection = DriverManager.getConnection(url, props);
91-
ConnectionsAccess.attach(dbName, connection, url);
91+
ConnectionsAccess.attach(name, connection, url);
9292
} catch (Exception e) {
9393
throw new InitException("Failed to connect to JDBC URL: " + url, e);
9494
}
@@ -98,15 +98,15 @@ public void open(String driver, String url, Properties props) {
9898
* Opens a connection from JNDI based on a registered name. This assumes that there is a <code>jndi.properties</code>
9999
* file with proper JNDI configuration in it.
100100
*
101-
* @param jndiName name of a configured data source.
101+
* @param jndiName name of a configured data source.
102102
*/
103103
public void open(String jndiName) {
104-
checkExistingConnection(dbName);
104+
checkExistingConnection(name);
105105
try {
106106
Context ctx = new InitialContext();
107107
DataSource ds = (DataSource) ctx.lookup(jndiName);
108108
Connection connection = ds.getConnection();
109-
ConnectionsAccess.attach(dbName, connection, jndiName);
109+
ConnectionsAccess.attach(name, connection, jndiName);
110110
} catch (Exception e) {
111111
throw new InitException("Failed to connect to JNDI name: " + jndiName, e);
112112
}
@@ -117,8 +117,8 @@ public void open(String jndiName) {
117117
*
118118
* @param connection instance of connection to attach to current thread.
119119
*/
120-
public void attach(Connection connection){
121-
ConnectionsAccess.attach(dbName, connection, "");
120+
public void attach(Connection connection) {
121+
ConnectionsAccess.attach(name, connection, "");
122122
}
123123

124124
/**
@@ -128,13 +128,12 @@ public void attach(Connection connection){
128128
* @return instance of a connection detached from current thread by name passed to constructor.
129129
*/
130130
public Connection detach() {
131-
132-
Connection connection = ConnectionsAccess.getConnection(dbName);
131+
Connection connection = ConnectionsAccess.getConnection(name);
133132
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");
136135
}
137-
ConnectionsAccess.detach(dbName);// lets free the thread from connection
136+
ConnectionsAccess.detach(name); // let's free the thread from connection
138137
StatementCache.instance().cleanStatementCache(connection);
139138
} catch (DBException e) {
140139
logger.warn("Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!", e);
@@ -147,11 +146,11 @@ public Connection detach() {
147146
*
148147
* @param datasource datasource will be used to acquire a connection.
149148
*/
150-
public void open(DataSource datasource){
151-
checkExistingConnection(dbName);
149+
public void open(DataSource datasource) {
150+
checkExistingConnection(name);
152151
try {
153152
Connection connection = datasource.getConnection();
154-
ConnectionsAccess.attach(dbName, connection, datasource.toString());
153+
ConnectionsAccess.attach(name, connection, datasource.toString());
155154
} catch (SQLException e) {
156155
throw new InitException(e);
157156
}
@@ -162,16 +161,16 @@ public void open(DataSource datasource){
162161
* Opens a new connection from JNDI data source by name using explicit JNDI properties. This method can be used in cases
163162
* when file <code>jndi.properties</code> cannot be easily updated.
164163
*
165-
* @param jndiName name of JNDI data source.
164+
* @param jndiName name of JNDI data source.
166165
* @param jndiProperties JNDI properties
167166
*/
168167
public void open(String jndiName, Properties jndiProperties) {
169-
checkExistingConnection(dbName);
168+
checkExistingConnection(name);
170169
try {
171170
Context ctx = new InitialContext(jndiProperties);
172171
DataSource ds = (DataSource) ctx.lookup(jndiName);
173172
Connection connection = ds.getConnection();
174-
ConnectionsAccess.attach(dbName, connection,
173+
ConnectionsAccess.attach(name, connection,
175174
jndiProperties.contains("url") ? jndiProperties.getProperty("url") : jndiName);
176175
} catch (Exception e) {
177176
throw new InitException("Failed to connect to JNDI name: " + jndiName, e);
@@ -181,23 +180,23 @@ public void open(String jndiName, Properties jndiProperties) {
181180

182181
/**
183182
* This method is used internally by framework.
184-
*
183+
*
185184
* @param spec specification for a JDBC connection.
186185
*/
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 {
194193
throw new IllegalArgumentException("this spec not supported: " + spec.getClass());
195194
}
196195
}
197196

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()
201200
+ ". This might indicate a logical error in your application.");
202201
}
203202
}
@@ -233,13 +232,13 @@ private void openJndi(ConnectionJndiSpec spec) {
233232
* This method is used internally by framework.
234233
*
235234
* @param context context.
236-
* @param jndiName JNDI name.
235+
* @param jndiName JNDI name.
237236
*/
238237
private void openContext(InitialContext context, String jndiName) {
239238
try {
240239
DataSource ds = (DataSource) context.lookup(jndiName);
241240
Connection connection = ds.getConnection();
242-
ConnectionsAccess.attach(dbName, connection, jndiName);
241+
ConnectionsAccess.attach(name, connection, jndiName);
243242
} catch (Exception e) {
244243
throw new InitException("Failed to connect to JNDI name: " + jndiName, e);
245244
}
@@ -259,9 +258,9 @@ public void close() {
259258
*/
260259
public void close(boolean suppressWarning) {
261260
try {
262-
Connection connection = ConnectionsAccess.getConnection(dbName);
261+
Connection connection = ConnectionsAccess.getConnection(name);
263262
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");
265264
}
266265
StatementCache.instance().cleanStatementCache(connection);
267266
connection.close();
@@ -270,8 +269,8 @@ public void close(boolean suppressWarning) {
270269
if (!suppressWarning) {
271270
logger.warn("Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!", e);
272271
}
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
275274
}
276275
}
277276

@@ -708,9 +707,9 @@ private void logException(String message, Exception e) {
708707
*/
709708
public void openTransaction() {
710709
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");
714713
}
715714
c.setAutoCommit(false);
716715
LogFilter.log(logger, "Transaction opened");
@@ -725,9 +724,9 @@ public void openTransaction() {
725724
*/
726725
public void commitTransaction() {
727726
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");
731730
}
732731
c.commit();
733732
LogFilter.log(logger, "Transaction committed");
@@ -741,9 +740,9 @@ public void commitTransaction() {
741740
*/
742741
public void rollbackTransaction() {
743742
try {
744-
Connection c = ConnectionsAccess.getConnection(dbName);
743+
Connection c = ConnectionsAccess.getConnection(name);
745744
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");
747746
}
748747
c.rollback();
749748
LogFilter.log(logger, "Transaction rolled back");
@@ -755,13 +754,13 @@ public void rollbackTransaction() {
755754
/**
756755
* Provides connection from current thread.
757756
*
758-
* @return connection from current thread.
757+
* @return connection from current thread.
759758
*/
760759
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+
}
765764
return connection;
766765
}
767766

@@ -770,8 +769,8 @@ public Connection connection() {
770769
*
771770
* @return true if finds connection on current thread, false if not.
772771
*/
773-
public boolean hasConnection(){
774-
return null != ConnectionsAccess.getConnection(dbName);
772+
public boolean hasConnection() {
773+
return null != ConnectionsAccess.getConnection(name);
775774
}
776775

777776
/**

0 commit comments

Comments
 (0)