Commit d26e09dc authored by guus's avatar guus

Throw exception if no database can be retrieved from the pool (instead of...

Throw exception if no database can be retrieved from the pool (instead of returning null), and retry up to the configured amount of times (JM-1336, JM-1337). Reviewed by Daniel.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@10268 b35dd754-fafc-0310-a699-88a17e54d16e
parent c478545b
......@@ -95,10 +95,18 @@ public class DbConnectionManager {
Integer retryCnt = 0;
Integer retryMax = 10;
Integer retryWait = 250; // milliseconds
Connection con;
Connection con = null;
SQLException lastException = null;
do {
retryCnt++;
con = connectionProvider.getConnection();
try {
con = connectionProvider.getConnection();
} catch (SQLException e) {
// TODO distinguish recoverable from non-recoverable exceptions.
lastException = e;
Log.info("Unable to get a connection from the database pool " +
"(attempt "+retryCnt+" out of "+retryMax+").", e);
}
if (con != null) {
// Got one, lets hand it off.
break;
......@@ -112,8 +120,12 @@ public class DbConnectionManager {
} while (retryCnt <= retryMax);
if (con == null) {
Log.warn("ConnectionManager.getConnection() " +
"failed to obtain a connection.");
final SQLException ex = new SQLException("ConnectionManager.getConnection() " +
"failed to obtain a connection after " + retryCnt +" retries. " +
"The exception from the last attempt is attached to this exception.",
lastException);
Log.error("Unable to obtain a connection from the database pool.", ex);
throw ex;
}
// See if profiling is enabled. If yes, wrap the connection with a
......
......@@ -70,20 +70,13 @@ public class DefaultConnectionProvider implements ConnectionProvider {
}
public Connection getConnection() throws SQLException {
Connection connection = null;
try {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
try {
connection = DriverManager.getConnection(proxoolURL, settings);
}
catch (SQLException e) {
Log.error("DbConnectionProvider: Error while getting connection: ", e);
}
return DriverManager.getConnection(proxoolURL, settings);
}
catch (ClassNotFoundException e) {
Log.error("DbConnectionProvider: Unable to find driver: ", e);
throw new SQLException("DbConnectionProvider: Unable to find driver", e);
}
return connection;
}
public void start() {
......
......@@ -47,20 +47,13 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
}
public Connection getConnection() throws SQLException {
Connection connection = null;
try {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
try {
connection = DriverManager.getConnection(proxoolURL, settings);
}
catch (SQLException e) {
Log.error("EmbeddedConnectionProvider: Error while getting connection: ", e);
}
return DriverManager.getConnection(proxoolURL, settings);
}
catch (ClassNotFoundException e) {
Log.error("EmbeddedConnectionProvider: Unable to find driver: ", e);
throw new SQLException("EmbeddedConnectionProvider: Unable to find driver: ", e);
}
return connection;
}
public void start() {
......
......@@ -107,17 +107,10 @@ public class JNDIDataSourceProvider implements ConnectionProvider {
}
public Connection getConnection() {
public Connection getConnection() throws SQLException {
if (dataSource == null) {
Log.error("DataSource has not been initialized.", null);
return null;
}
try {
return dataSource.getConnection();
}
catch (SQLException e) {
Log.error("Could not retrieve Connection from DataSource", e);
return null;
throw new SQLException("DataSource has not been initialized.");
}
return dataSource.getConnection();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment