Commit 62653123 authored by God Ly's avatar God Ly Committed by it2000

Replaced (con|pstmt|rs).close() with DBConnectionManager.close...(...).

Replaced Statement with PreparedStatement.
Nearly all X.close() are now in DBConnectionManager.
Also improved if{..}else{..} logic (human/compiler hint, usually if{..} should/will be executed).

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@11691 b35dd754-fafc-0310-a699-88a17e54d16e
parent 94b2a133
...@@ -117,41 +117,37 @@ public class DbConnectionManager { ...@@ -117,41 +117,37 @@ public class DbConnectionManager {
Connection con = null; Connection con = null;
SQLException lastException = null; SQLException lastException = null;
do { do {
retryCnt++;
try { try {
con = connectionProvider.getConnection(); con = connectionProvider.getConnection();
if (con != null) {
// Got one, lets hand it off.
// Usually profiling is not enabled. So we return a normal
// connection unless profiling is enabled. If yes, wrap the
// connection with a profiled connection.
if (!profilingEnabled) {
return con;
}
else {
return new ProfiledConnection(con);
}
}
} catch (SQLException e) { } catch (SQLException e) {
// TODO distinguish recoverable from non-recoverable exceptions. // TODO distinguish recoverable from non-recoverable exceptions.
lastException = e; lastException = e;
Log.info("Unable to get a connection from the database pool " + Log.info("Unable to get a connection from the database pool " +
"(attempt "+retryCnt+" out of "+retryMax+").", e); "(attempt "+retryCnt+" out of "+retryMax+").", e);
} }
if (con != null) {
// Got one, lets hand it off.
break;
}
try { try {
Thread.sleep(retryWait); Thread.sleep(retryWait);
} }
catch (Exception e) { catch (Exception e) {
// Ignored // Ignored
} }
retryCnt++;
} while (retryCnt <= retryMax); } while (retryCnt <= retryMax);
throw new SQLException("ConnectionManager.getConnection() " +
if (con == null) { "failed to obtain a connection after " + retryCnt +" retries. " +
throw new SQLException("ConnectionManager.getConnection() " + "The exception from the last attempt is as follows: "+lastException);
"failed to obtain a connection after " + retryCnt +" retries. " +
"The exception from the last attempt is as follows: "+lastException);
}
// See if profiling is enabled. If yes, wrap the connection with a
// profiled connection.
if (profilingEnabled) {
return new ProfiledConnection(con);
}
else {
return con;
}
} }
/** /**
...@@ -266,7 +262,7 @@ public class DbConnectionManager { ...@@ -266,7 +262,7 @@ public class DbConnectionManager {
* Log.error(sqle.getMessage(), sqle); * Log.error(sqle.getMessage(), sqle);
* } * }
* finally { * finally {
* ConnectionManager.closePreparedStatement(pstmt); * ConnectionManager.closeStatement(pstmt);
* } * }
* } </pre> * } </pre>
* *
...@@ -282,6 +278,90 @@ public class DbConnectionManager { ...@@ -282,6 +278,90 @@ public class DbConnectionManager {
} }
} }
} }
/**
* Closes a statement and a result set. This method should be called within the finally section of
* your database logic, as in the following example:
*
* <pre>
* public void doSomething(Connection con) {
* PreparedStatement pstmt = null;
* ResultSet rs = null;
* try {
* pstmt = con.prepareStatement("select * from blah");
* rs = ...
* ....
* }
* catch (SQLException sqle) {
* Log.error(sqle.getMessage(), sqle);
* }
* finally {
* ConnectionManager.closeStatement(rs, pstmt);
* }
* } </pre>
*
* @param stmt the statement.
*/
public static void closeStatement(ResultSet rs, Statement stmt) {
closeResultSet(rs);
closeStatement(stmt);
}
/**
* Closes a statement. This method should be called within the try section of
* your database logic when you reuse a statement. It may throws an exception,
* so don't place it in the finally section.<br>
* Example:
*
* <pre>
* public void doSomething(Connection con) {
* PreparedStatement pstmt = null;
* try {
* pstmt = con.prepareStatement("select * from dual");
* pstmt.executeUpdate();
* ...
* <b>ConnectionManager.fastcloseStmt(pstmt);</b>
* pstmt = con.prepareStatement("select * from blah");
* ...
* }
* ...
* } </pre>
*
* @param rs the result set to close.
* @param stmt the statement to close.
*/
public static void fastcloseStmt(PreparedStatement pstmt) throws SQLException
{
pstmt.close();
}
/**
* Closes a statement and a result set. This method should be called within the try section of
* your database logic when you reuse a statement. It may throw an exception,
* so don't place it in the finally section.<br>
* Example:
*
* <pre>
* public void doSomething(Connection con) {
* PreparedStatement pstmt = null;
* try {
* pstmt = con.prepareStatement("select * from blah");
* rs = pstmt.executeQuery();
* ...
* ConnectionManager.fastcloseStmt(rs, pstmt);
* pstmt = con.prepareStatement("select * from blah");
* ...
* }
* ...
* } </pre>
*
* @param rs the result set to close.
* @param stmt the statement to close.
*/
public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt) throws SQLException
{
rs.close();
pstmt.close();
}
/** /**
* Closes a result set, statement and database connection (returning the connection to * Closes a result set, statement and database connection (returning the connection to
...@@ -383,6 +463,7 @@ public class DbConnectionManager { ...@@ -383,6 +463,7 @@ public class DbConnectionManager {
* @return a Statement * @return a Statement
* @throws SQLException if an error occurs. * @throws SQLException if an error occurs.
*/ */
@Deprecated
public static Statement createScrollableStatement(Connection con) throws SQLException { public static Statement createScrollableStatement(Connection con) throws SQLException {
if (isScrollResultsSupported()) { if (isScrollResultsSupported()) {
return con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, return con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
...@@ -426,16 +507,19 @@ public class DbConnectionManager { ...@@ -426,16 +507,19 @@ public class DbConnectionManager {
// If the driver supports scrollable result sets, use that feature. // If the driver supports scrollable result sets, use that feature.
if (isScrollResultsSupported()) { if (isScrollResultsSupported()) {
if (rowNumber > 0) { if (rowNumber > 0) {
rs.setFetchDirection(ResultSet.FETCH_FORWARD);
// We will attempt to do a relative fetch. This may fail in SQL Server if // We will attempt to do a relative fetch. This may fail in SQL Server if
// <resultset-navigation-strategy> is set to absolute. It would need to be // <resultset-navigation-strategy> is set to absolute. It would need to be
// set to looping to work correctly. // set to looping to work correctly.
// If so, manually scroll to the correct row. // If so, manually scroll to the correct row.
try { try {
rs.setFetchDirection(ResultSet.FETCH_FORWARD);
rs.relative(rowNumber); rs.relative(rowNumber);
} }
catch (SQLException e) { catch (SQLException e) {
// TODO change "Error ..." to "Disabling ..."
Log.error("Error in JDBC method rs.relative(rowNumber).", e);
//Log.error("Disabling JDBC method rs.relative(rowNumber).", e);
//scrollResultsSupported = false;
for (int i = 0; i < rowNumber; i++) { for (int i = 0; i < rowNumber; i++) {
rs.next(); rs.next();
} }
...@@ -614,6 +698,7 @@ public class DbConnectionManager { ...@@ -614,6 +698,7 @@ public class DbConnectionManager {
// However, it is a good idea to update the meta-data so that // However, it is a good idea to update the meta-data so that
// we don't have to incur the cost of catching an exception // we don't have to incur the cost of catching an exception
// each time. // each time.
Log.error("Disabling JDBC method stmt.setMaxRows(maxRows).", t);
maxRowsSupported = false; maxRowsSupported = false;
} }
} }
...@@ -638,6 +723,7 @@ public class DbConnectionManager { ...@@ -638,6 +723,7 @@ public class DbConnectionManager {
// However, it is a good idea to update the meta-data so that // However, it is a good idea to update the meta-data so that
// we don't have to incur the cost of catching an exception // we don't have to incur the cost of catching an exception
// each time. // each time.
Log.error("Disabling JDBC method rs.setFetchSize(fetchSize).", t);
fetchSizeSupported = false; fetchSizeSupported = false;
} }
} }
...@@ -692,7 +778,7 @@ public class DbConnectionManager { ...@@ -692,7 +778,7 @@ public class DbConnectionManager {
if (dbName.indexOf("oracle") != -1) { if (dbName.indexOf("oracle") != -1) {
databaseType = DatabaseType.oracle; databaseType = DatabaseType.oracle;
streamTextRequired = true; streamTextRequired = true;
scrollResultsSupported = false; scrollResultsSupported = false; /* TODO comment and test this, it should be supported since 10g */
// The i-net AUGURO JDBC driver // The i-net AUGURO JDBC driver
if (driverName.indexOf("auguro") != -1) { if (driverName.indexOf("auguro") != -1) {
streamTextRequired = false; streamTextRequired = false;
...@@ -725,12 +811,12 @@ public class DbConnectionManager { ...@@ -725,12 +811,12 @@ public class DbConnectionManager {
// MySQL properties // MySQL properties
else if (dbName.indexOf("mysql") != -1) { else if (dbName.indexOf("mysql") != -1) {
databaseType = DatabaseType.mysql; databaseType = DatabaseType.mysql;
transactionsSupported = false; transactionsSupported = false; /* TODO comment and test this, it should be supported since 5.0 */
} }
// HSQL properties // HSQL properties
else if (dbName.indexOf("hsql") != -1) { else if (dbName.indexOf("hsql") != -1) {
databaseType = DatabaseType.hsqldb; databaseType = DatabaseType.hsqldb;
scrollResultsSupported = false; // scrollResultsSupported = false; /* comment and test this, it should be supported since 1.7.2 */
} }
// DB2 properties. // DB2 properties.
else if (dbName.indexOf("db2") != 1) { else if (dbName.indexOf("db2") != 1) {
......
...@@ -26,6 +26,7 @@ import org.jivesoftware.util.Log; ...@@ -26,6 +26,7 @@ import org.jivesoftware.util.Log;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -96,18 +97,17 @@ public class EmbeddedConnectionProvider implements ConnectionProvider { ...@@ -96,18 +97,17 @@ public class EmbeddedConnectionProvider implements ConnectionProvider {
public void destroy() { public void destroy() {
// Shutdown the database. // Shutdown the database.
Connection con = null; Connection con = null;
PreparedStatement pstmt = null;
try { try {
con = getConnection(); con = getConnection();
Statement stmt = con.createStatement(); pstmt = con.prepareStatement("SHUTDOWN");
stmt.execute("SHUTDOWN"); pstmt.execute();
stmt.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (con != null) { con.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
// Blank out the settings // Blank out the settings
settings = null; settings = null;
......
...@@ -30,7 +30,6 @@ import java.sql.Connection; ...@@ -30,7 +30,6 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays; import java.util.Arrays;
import org.jivesoftware.database.bugfix.OF33; import org.jivesoftware.database.bugfix.OF33;
...@@ -186,8 +185,7 @@ public class SchemaManager { ...@@ -186,8 +185,7 @@ public class SchemaManager {
catch (SQLException sqle) { catch (SQLException sqle) {
// The database schema must not be installed. // The database schema must not be installed.
Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable.", sqle); Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable.", sqle);
DbConnectionManager.closeResultSet(rs); DbConnectionManager.closeStatement(rs, pstmt);
DbConnectionManager.closeStatement(pstmt);
if (schemaKey.equals("openfire")) { if (schemaKey.equals("openfire")) {
try { try {
// Releases of Openfire before 3.6.0 stored the version in a jiveVersion table. // Releases of Openfire before 3.6.0 stored the version in a jiveVersion table.
...@@ -201,15 +199,13 @@ public class SchemaManager { ...@@ -201,15 +199,13 @@ public class SchemaManager {
catch (SQLException sqlea) { catch (SQLException sqlea) {
// The database schema must not be installed. // The database schema must not be installed.
Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable.", sqlea); Log.debug("SchemaManager: Error verifying "+schemaKey+" version, probably ignorable.", sqlea);
DbConnectionManager.closeResultSet(rs); DbConnectionManager.closeStatement(rs, pstmt);
DbConnectionManager.closeStatement(pstmt);
// Releases of Openfire before 2.6.0 stored a major and minor version // Releases of Openfire before 2.6.0 stored a major and minor version
// number so the normal check for version can fail. Check for the // number so the normal check for version can fail. Check for the
// version using the old format in that case. // version using the old format in that case.
try { try {
if (pstmt != null) {
pstmt.close();
}
pstmt = con.prepareStatement(CHECK_VERSION_OLD); pstmt = con.prepareStatement(CHECK_VERSION_OLD);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
...@@ -224,8 +220,7 @@ public class SchemaManager { ...@@ -224,8 +220,7 @@ public class SchemaManager {
} }
} }
finally { finally {
DbConnectionManager.closeResultSet(rs); DbConnectionManager.closeStatement(rs, pstmt);
DbConnectionManager.closeStatement(pstmt);
} }
// If already up to date, return. // If already up to date, return.
if (currentVersion >= requiredVersion) { if (currentVersion >= requiredVersion) {
...@@ -403,20 +398,23 @@ public class SchemaManager { ...@@ -403,20 +398,23 @@ public class SchemaManager {
DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.db2) { DbConnectionManager.getDatabaseType() == DbConnectionManager.DatabaseType.db2) {
command.deleteCharAt(command.length() - 1); command.deleteCharAt(command.length() - 1);
} }
PreparedStatement pstmt = null;
try { try {
String cmdString = command.toString(); String cmdString = command.toString();
if (autoreplace) { if (autoreplace) {
cmdString = cmdString.replaceAll("jiveVersion", "ofVersion"); cmdString = cmdString.replaceAll("jiveVersion", "ofVersion");
} }
Statement stmt = con.createStatement(); pstmt = con.prepareStatement(cmdString);
stmt.execute(cmdString); pstmt.execute();
stmt.close();
} }
catch (SQLException e) { catch (SQLException e) {
// Lets show what failed // Lets show what failed
Log.error("SchemaManager: Failed to execute SQL:\n"+command.toString()); Log.error("SchemaManager: Failed to execute SQL:\n"+command.toString());
throw e; throw e;
} }
finally {
DbConnectionManager.closeStatement(pstmt);
}
} }
} }
} }
......
...@@ -190,6 +190,7 @@ public class SequenceManager { ...@@ -190,6 +190,7 @@ public class SequenceManager {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
boolean abortTransaction = false; boolean abortTransaction = false;
boolean success = false; boolean success = false;
...@@ -198,20 +199,16 @@ public class SequenceManager { ...@@ -198,20 +199,16 @@ public class SequenceManager {
// Get the current ID from the database. // Get the current ID from the database.
pstmt = con.prepareStatement(LOAD_ID); pstmt = con.prepareStatement(LOAD_ID);
pstmt.setInt(1, type); pstmt.setInt(1, type);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
long currentID = 1; long currentID = 1;
if (!rs.next()) { if (rs.next()) {
rs.close(); currentID = rs.getLong(1);
pstmt.close();
createNewID(con, type);
} }
else { else {
currentID = rs.getLong(1); createNewID(con, type);
rs.close();
pstmt.close();
} }
DbConnectionManager.fastcloseStmt(rs, pstmt);
// Increment the id to define our block. // Increment the id to define our block.
long newID = currentID + blockSize; long newID = currentID + blockSize;
...@@ -236,14 +233,7 @@ public class SequenceManager { ...@@ -236,14 +233,7 @@ public class SequenceManager {
abortTransaction = true; abortTransaction = true;
} }
finally { finally {
try { DbConnectionManager.closeStatement(rs, pstmt);
if (pstmt != null) {
pstmt.close();
}
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
DbConnectionManager.closeTransactionConnection(con, abortTransaction); DbConnectionManager.closeTransactionConnection(con, abortTransaction);
} }
......
...@@ -189,16 +189,17 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -189,16 +189,17 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
*/ */
public Collection<OfflineMessage> getMessages(String username, boolean delete) { public Collection<OfflineMessage> getMessages(String username, boolean delete) {
List<OfflineMessage> messages = new ArrayList<OfflineMessage>(); List<OfflineMessage> messages = new ArrayList<OfflineMessage>();
SAXReader xmlReader = null;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
SAXReader xmlReader = null; ResultSet rs = null;
try { try {
// Get a sax reader from the pool // Get a sax reader from the pool
xmlReader = xmlReaders.take(); xmlReader = xmlReaders.take();
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_OFFLINE); pstmt = con.prepareStatement(LOAD_OFFLINE);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String msgXML = rs.getString(1); String msgXML = rs.getString(1);
Date creationDate = new Date(Long.parseLong(rs.getString(2).trim())); Date creationDate = new Date(Long.parseLong(rs.getString(2).trim()));
...@@ -221,28 +222,33 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene ...@@ -221,28 +222,33 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
delay.addAttribute("stamp", dateFormat.format(creationDate)); delay.addAttribute("stamp", dateFormat.format(creationDate));
messages.add(message); messages.add(message);
} }
rs.close();
// Check if the offline messages loaded should be deleted, and that there are // Check if the offline messages loaded should be deleted, and that there are
// messages to delete. // messages to delete.
if (delete && !messages.isEmpty()) { if (delete && !messages.isEmpty()) {
pstmt.close(); PreparedStatement pstmt2 = null;
try {
pstmt = con.prepareStatement(DELETE_OFFLINE); pstmt2 = con.prepareStatement(DELETE_OFFLINE);
pstmt.setString(1, username); pstmt2.setString(1, username);
pstmt.executeUpdate(); pstmt2.executeUpdate();
removeUsernameFromSizeCache(username);
removeUsernameFromSizeCache(username); }
catch (Exception e) {
Log.error("Error deleting offline messages of username: " + username, e);
}
finally {
DbConnectionManager.closeStatement(pstmt2);
}
} }
} }
catch (Exception e) { catch (Exception e) {
Log.error("Error retrieving offline messages of username: " + username, e); Log.error("Error retrieving offline messages of username: " + username, e);
} }
finally { finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
// Return the sax reader to the pool // Return the sax reader to the pool
if (xmlReader != null) { if (xmlReader != null) {
xmlReaders.add(xmlReader); xmlReaders.add(xmlReader);
} }
DbConnectionManager.closeConnection(pstmt, con);
} }
return messages; return messages;
} }
......
...@@ -110,8 +110,9 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -110,8 +110,9 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
*/ */
public void add(String username, Element data) { public void add(String username, Element data) {
if (enabled) { if (enabled) {
java.sql.Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
data.write(writer); data.write(writer);
...@@ -119,14 +120,12 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -119,14 +120,12 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
pstmt = con.prepareStatement(LOAD_PRIVATE); pstmt = con.prepareStatement(LOAD_PRIVATE);
pstmt.setString(1, username); pstmt.setString(1, username);
pstmt.setString(2, data.getNamespaceURI()); pstmt.setString(2, data.getNamespaceURI());
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
boolean update = false; boolean update = false;
if (rs.next()) { if (rs.next()) {
update = true; update = true;
} }
rs.close(); DbConnectionManager.fastcloseStmt(rs, pstmt);
pstmt.close();
if (update) { if (update) {
pstmt = con.prepareStatement(UPDATE_PRIVATE); pstmt = con.prepareStatement(UPDATE_PRIVATE);
} }
...@@ -143,10 +142,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -143,10 +142,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
} }
...@@ -169,6 +165,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -169,6 +165,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
if (enabled) { if (enabled) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
SAXReader xmlReader = null; SAXReader xmlReader = null;
try { try {
// Get a sax reader from the pool // Get a sax reader from the pool
...@@ -177,27 +174,23 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -177,27 +174,23 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
pstmt = con.prepareStatement(LOAD_PRIVATE); pstmt = con.prepareStatement(LOAD_PRIVATE);
pstmt.setString(1, username); pstmt.setString(1, username);
pstmt.setString(2, data.getNamespaceURI()); pstmt.setString(2, data.getNamespaceURI());
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
data.clearContent(); data.clearContent();
String result = rs.getString(1).trim(); String result = rs.getString(1).trim();
Document doc = xmlReader.read(new StringReader(result)); Document doc = xmlReader.read(new StringReader(result));
data = doc.getRootElement(); data = doc.getRootElement();
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
// Return the sax reader to the pool // Return the sax reader to the pool
if (xmlReader != null) { if (xmlReader != null) {
xmlReaders.add(xmlReader); xmlReaders.add(xmlReader);
} }
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
return data; return data;
...@@ -209,7 +202,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -209,7 +202,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
public void userDeleting(User user, Map params) { public void userDeleting(User user, Map params) {
// Delete all private properties of the user // Delete all private properties of the user
java.sql.Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
...@@ -221,10 +214,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener { ...@@ -221,10 +214,7 @@ public class PrivateStorage extends BasicModule implements UserEventListener {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
......
...@@ -28,6 +28,7 @@ import java.lang.reflect.Method; ...@@ -28,6 +28,7 @@ import java.lang.reflect.Method;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.security.KeyStore; import java.security.KeyStore;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -746,14 +747,14 @@ public class XMPPServer { ...@@ -746,14 +747,14 @@ public class XMPPServer {
* Verify that the database is accessible. * Verify that the database is accessible.
*/ */
private void verifyDataSource() { private void verifyDataSource() {
java.sql.Connection conn = null; Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
conn = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT count(*) FROM ofID"); pstmt = con.prepareStatement("SELECT count(*) FROM ofID");
ResultSet rs = stmt.executeQuery(); rs = pstmt.executeQuery();
rs.next(); rs.next();
rs.close();
stmt.close();
} }
catch (Exception e) { catch (Exception e) {
System.err.println("Database setup or configuration error: " + System.err.println("Database setup or configuration error: " +
...@@ -763,14 +764,7 @@ public class XMPPServer { ...@@ -763,14 +764,7 @@ public class XMPPServer {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
finally { finally {
if (conn != null) { DbConnectionManager.closeConnection(rs, pstmt, con);
try {
conn.close();
}
catch (SQLException e) {
Log.error(e.getMessage(), e);
}
}
} }
} }
......
...@@ -129,6 +129,7 @@ public class DefaultAuthProvider implements AuthProvider { ...@@ -129,6 +129,7 @@ public class DefaultAuthProvider implements AuthProvider {
} }
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
if (username.contains("@")) { if (username.contains("@")) {
// Check that the specified domain matches the server's domain // Check that the specified domain matches the server's domain
int index = username.indexOf("@"); int index = username.indexOf("@");
...@@ -144,7 +145,7 @@ public class DefaultAuthProvider implements AuthProvider { ...@@ -144,7 +145,7 @@ public class DefaultAuthProvider implements AuthProvider {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PASSWORD); pstmt = con.prepareStatement(LOAD_PASSWORD);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (!rs.next()) { if (!rs.next()) {
throw new UserNotFoundException(username); throw new UserNotFoundException(username);
} }
...@@ -164,10 +165,7 @@ public class DefaultAuthProvider implements AuthProvider { ...@@ -164,10 +165,7 @@ public class DefaultAuthProvider implements AuthProvider {
throw new UserNotFoundException(sqle); throw new UserNotFoundException(sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -222,10 +220,7 @@ public class DefaultAuthProvider implements AuthProvider { ...@@ -222,10 +220,7 @@ public class DefaultAuthProvider implements AuthProvider {
throw new UserNotFoundException(sqle); throw new UserNotFoundException(sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.jivesoftware.openfire.component; package org.jivesoftware.openfire.component;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -247,7 +248,7 @@ public class ExternalComponentManager { ...@@ -247,7 +248,7 @@ public class ExternalComponentManager {
return; return;
} }
// Remove the permission for the entity from the database // Remove the permission for the entity from the database
java.sql.Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
...@@ -260,10 +261,7 @@ public class ExternalComponentManager { ...@@ -260,10 +261,7 @@ public class ExternalComponentManager {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -274,7 +272,7 @@ public class ExternalComponentManager { ...@@ -274,7 +272,7 @@ public class ExternalComponentManager {
*/ */
private static void addConfiguration(ExternalComponentConfiguration configuration) { private static void addConfiguration(ExternalComponentConfiguration configuration) {
// Remove the permission for the entity from the database // Remove the permission for the entity from the database
java.sql.Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
...@@ -289,10 +287,7 @@ public class ExternalComponentManager { ...@@ -289,10 +287,7 @@ public class ExternalComponentManager {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -307,29 +302,26 @@ public class ExternalComponentManager { ...@@ -307,29 +302,26 @@ public class ExternalComponentManager {
*/ */
private static ExternalComponentConfiguration getConfiguration(String subdomain, boolean useWildcard) { private static ExternalComponentConfiguration getConfiguration(String subdomain, boolean useWildcard) {
ExternalComponentConfiguration configuration = null; ExternalComponentConfiguration configuration = null;
java.sql.Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
// Check if there is a configuration for the subdomain // Check if there is a configuration for the subdomain
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_CONFIGURATION); pstmt = con.prepareStatement(LOAD_CONFIGURATION);
pstmt.setString(1, subdomain); pstmt.setString(1, subdomain);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
configuration = new ExternalComponentConfiguration(subdomain, false, Permission.valueOf(rs.getString(2)), configuration = new ExternalComponentConfiguration(subdomain, false, Permission.valueOf(rs.getString(2)),
rs.getString(1)); rs.getString(1));
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
if (configuration == null && useWildcard) { if (configuration == null && useWildcard) {
...@@ -339,23 +331,19 @@ public class ExternalComponentManager { ...@@ -339,23 +331,19 @@ public class ExternalComponentManager {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_WILDCARD_CONFIGURATION); pstmt = con.prepareStatement(LOAD_WILDCARD_CONFIGURATION);
pstmt.setString(1, subdomain); pstmt.setString(1, subdomain);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
configuration = new ExternalComponentConfiguration(subdomain, true, Permission.valueOf(rs.getString(2)), configuration = new ExternalComponentConfiguration(subdomain, true, Permission.valueOf(rs.getString(2)),
rs.getString(1)); rs.getString(1));
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); } }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
}
} }
return configuration; return configuration;
} }
...@@ -364,13 +352,14 @@ public class ExternalComponentManager { ...@@ -364,13 +352,14 @@ public class ExternalComponentManager {
Permission permission) { Permission permission) {
Collection<ExternalComponentConfiguration> answer = Collection<ExternalComponentConfiguration> answer =
new ArrayList<ExternalComponentConfiguration>(); new ArrayList<ExternalComponentConfiguration>();
java.sql.Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_CONFIGURATIONS); pstmt = con.prepareStatement(LOAD_CONFIGURATIONS);
pstmt.setString(1, permission.toString()); pstmt.setString(1, permission.toString());
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
ExternalComponentConfiguration configuration; ExternalComponentConfiguration configuration;
while (rs.next()) { while (rs.next()) {
String subdomain = rs.getString(1); String subdomain = rs.getString(1);
...@@ -381,16 +370,12 @@ public class ExternalComponentManager { ...@@ -381,16 +370,12 @@ public class ExternalComponentManager {
rs.getString(3)); rs.getString(3));
answer.add(configuration); answer.add(configuration);
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return answer; return answer;
} }
......
...@@ -409,10 +409,11 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -409,10 +409,11 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
private void loadServices() { private void loadServices() {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_SERVICES); pstmt = con.prepareStatement(LOAD_SERVICES);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String subdomain = rs.getString(1); String subdomain = rs.getString(1);
String description = rs.getString(2); String description = rs.getString(2);
...@@ -420,16 +421,12 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -420,16 +421,12 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
MultiUserChatServiceImpl muc = new MultiUserChatServiceImpl(subdomain, description, isHidden); MultiUserChatServiceImpl muc = new MultiUserChatServiceImpl(subdomain, description, isHidden);
mucServices.put(subdomain, muc); mucServices.put(subdomain, muc);
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -441,28 +438,25 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -441,28 +438,25 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
private long loadServiceID(String subdomain) { private long loadServiceID(String subdomain) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
Long id = (long)-1; Long id = (long)-1;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_SERVICE_ID); pstmt = con.prepareStatement(LOAD_SERVICE_ID);
pstmt.setString(1, subdomain); pstmt.setString(1, subdomain);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
id = rs.getLong(1); id = rs.getLong(1);
} }
else { else {
throw new Exception("Unable to locate Service ID for subdomain "+subdomain); throw new Exception("Unable to locate Service ID for subdomain "+subdomain);
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
// No problem, considering this as a "not found". // No problem, considering this as a "not found".
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return id; return id;
} }
...@@ -475,28 +469,25 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -475,28 +469,25 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
private String loadServiceSubdomain(Long serviceID) { private String loadServiceSubdomain(Long serviceID) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
String subdomain = null; String subdomain = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_SUBDOMAIN); pstmt = con.prepareStatement(LOAD_SUBDOMAIN);
pstmt.setLong(1, serviceID); pstmt.setLong(1, serviceID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
subdomain = rs.getString(1); subdomain = rs.getString(1);
} }
else { else {
throw new Exception("Unable to locate subdomain for service ID "+serviceID); throw new Exception("Unable to locate subdomain for service ID "+serviceID);
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
// No problem, considering this as a "not found". // No problem, considering this as a "not found".
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return subdomain; return subdomain;
} }
...@@ -529,10 +520,7 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -529,10 +520,7 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -562,10 +550,7 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -562,10 +550,7 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -586,10 +571,7 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis ...@@ -586,10 +571,7 @@ public class MultiUserChatManager extends BasicModule implements ClusterEventLis
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
......
...@@ -279,10 +279,7 @@ public class MUCServiceProperties implements Map<String, String> { ...@@ -279,10 +279,7 @@ public class MUCServiceProperties implements Map<String, String> {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -301,10 +298,7 @@ public class MUCServiceProperties implements Map<String, String> { ...@@ -301,10 +298,7 @@ public class MUCServiceProperties implements Map<String, String> {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -322,36 +316,30 @@ public class MUCServiceProperties implements Map<String, String> { ...@@ -322,36 +316,30 @@ public class MUCServiceProperties implements Map<String, String> {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
private void loadProperties() { private void loadProperties() {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setLong(1, serviceID); pstmt.setLong(1, serviceID);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String name = rs.getString(1); String name = rs.getString(1);
String value = rs.getString(2); String value = rs.getString(2);
properties.put(name, value); properties.put(name, value);
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
} }
\ No newline at end of file
...@@ -155,7 +155,7 @@ public class RosterItemProvider { ...@@ -155,7 +155,7 @@ public class RosterItemProvider {
pstmt.setLong(5, rosterID); pstmt.setLong(5, rosterID);
pstmt.executeUpdate(); pstmt.executeUpdate();
// Close now the statement (do not wait to be GC'ed) // Close now the statement (do not wait to be GC'ed)
pstmt.close(); DbConnectionManager.fastcloseStmt(pstmt);
// Delete old group list // Delete old group list
pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS); pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS);
...@@ -193,7 +193,7 @@ public class RosterItemProvider { ...@@ -193,7 +193,7 @@ public class RosterItemProvider {
pstmt.setLong(1, rosterItemID); pstmt.setLong(1, rosterItemID);
pstmt.executeUpdate(); pstmt.executeUpdate();
// Close now the statement (do not wait to be GC'ed) // Close now the statement (do not wait to be GC'ed)
pstmt.close(); DbConnectionManager.fastcloseStmt(pstmt);
// Remove roster // Remove roster
pstmt = con.prepareStatement(DELETE_ROSTER_ITEM); pstmt = con.prepareStatement(DELETE_ROSTER_ITEM);
...@@ -282,7 +282,7 @@ public class RosterItemProvider { ...@@ -282,7 +282,7 @@ public class RosterItemProvider {
Map<Long, RosterItem> itemsByID = new HashMap<Long, RosterItem>(); Map<Long, RosterItem> itemsByID = new HashMap<Long, RosterItem>();
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs; ResultSet rs = null;
try { try {
// Load all the contacts in the roster // Load all the contacts in the roster
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
...@@ -303,10 +303,11 @@ public class RosterItemProvider { ...@@ -303,10 +303,11 @@ public class RosterItemProvider {
itemsByID.put(item.getID(), item); itemsByID.put(item.getID(), item);
} }
// Close the statement and result set // Close the statement and result set
rs.close(); DbConnectionManager.fastcloseStmt(rs, pstmt);
pstmt.close();
// Set null to pstmt to be sure that it's not closed twice. It seems that // Set null to pstmt to be sure that it's not closed twice. It seems that
// Sybase driver is raising an error when trying to close an already closed statement. // Sybase driver is raising an error when trying to close an already closed statement.
// it2000 comment: TODO interesting, that's the only place with the sybase fix
// it2000 comment: one should move this in closeStatement()
pstmt = null; pstmt = null;
// Load the groups for the loaded contact // Load the groups for the loaded contact
...@@ -323,14 +324,13 @@ public class RosterItemProvider { ...@@ -323,14 +324,13 @@ public class RosterItemProvider {
while (rs.next()) { while (rs.next()) {
itemsByID.get(rs.getLong(1)).getGroups().add(rs.getString(2)); itemsByID.get(rs.getLong(1)).getGroups().add(rs.getString(2));
} }
rs.close();
} }
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
DbConnectionManager.closeConnection(pstmt, con); DbConnectionManager.closeConnection(rs, pstmt, con);
} }
return itemList.iterator(); return itemList.iterator();
} }
......
...@@ -187,10 +187,7 @@ public class RemoteServerManager { ...@@ -187,10 +187,7 @@ public class RemoteServerManager {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -217,10 +214,7 @@ public class RemoteServerManager { ...@@ -217,10 +214,7 @@ public class RemoteServerManager {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -239,27 +233,23 @@ public class RemoteServerManager { ...@@ -239,27 +233,23 @@ public class RemoteServerManager {
if (configuration == null) { if (configuration == null) {
java.sql.Connection con = null; java.sql.Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_CONFIGURATION); pstmt = con.prepareStatement(LOAD_CONFIGURATION);
pstmt.setString(1, domain); pstmt.setString(1, domain);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
configuration = new RemoteServerConfiguration(domain); configuration = new RemoteServerConfiguration(domain);
configuration.setRemotePort(rs.getInt(1)); configuration.setRemotePort(rs.getInt(1));
configuration.setPermission(Permission.valueOf(rs.getString(2))); configuration.setPermission(Permission.valueOf(rs.getString(2)));
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
if (configuration != null) { if (configuration != null) {
configurationsCache.put(domain, configuration); configurationsCache.put(domain, configuration);
...@@ -277,11 +267,12 @@ public class RemoteServerManager { ...@@ -277,11 +267,12 @@ public class RemoteServerManager {
new ArrayList<RemoteServerConfiguration>(); new ArrayList<RemoteServerConfiguration>();
java.sql.Connection con = null; java.sql.Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_CONFIGURATIONS); pstmt = con.prepareStatement(LOAD_CONFIGURATIONS);
pstmt.setString(1, permission.toString()); pstmt.setString(1, permission.toString());
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
RemoteServerConfiguration configuration; RemoteServerConfiguration configuration;
while (rs.next()) { while (rs.next()) {
configuration = new RemoteServerConfiguration(rs.getString(1)); configuration = new RemoteServerConfiguration(rs.getString(1));
...@@ -289,16 +280,12 @@ public class RemoteServerManager { ...@@ -289,16 +280,12 @@ public class RemoteServerManager {
configuration.setPermission(permission); configuration.setPermission(permission);
answer.add(configuration); answer.add(configuration);
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return answer; return answer;
} }
......
...@@ -100,25 +100,22 @@ public class User implements Cacheable, Externalizable, Result { ...@@ -100,25 +100,22 @@ public class User implements Cacheable, Externalizable, Result {
String propertyValue = null; String propertyValue = null;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTY); pstmt = con.prepareStatement(LOAD_PROPERTY);
pstmt.setString(1, username); pstmt.setString(1, username);
pstmt.setString(2, propertyName); pstmt.setString(2, propertyName);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
propertyValue = rs.getString(1); propertyValue = rs.getString(1);
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return propertyValue; return propertyValue;
} }
...@@ -514,24 +511,21 @@ public class User implements Cacheable, Externalizable, Result { ...@@ -514,24 +511,21 @@ public class User implements Cacheable, Externalizable, Result {
private void loadProperties() { private void loadProperties() {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
properties.put(rs.getString(1), rs.getString(2)); properties.put(rs.getString(1), rs.getString(2));
} }
rs.close();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle); Log.error(sqle.getMessage(), sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -550,10 +544,7 @@ public class User implements Cacheable, Externalizable, Result { ...@@ -550,10 +544,7 @@ public class User implements Cacheable, Externalizable, Result {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -572,10 +563,7 @@ public class User implements Cacheable, Externalizable, Result { ...@@ -572,10 +563,7 @@ public class User implements Cacheable, Externalizable, Result {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -593,10 +581,7 @@ public class User implements Cacheable, Externalizable, Result { ...@@ -593,10 +581,7 @@ public class User implements Cacheable, Externalizable, Result {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -628,5 +613,5 @@ public class User implements Cacheable, Externalizable, Result { ...@@ -628,5 +613,5 @@ public class User implements Cacheable, Externalizable, Result {
public String getUID() public String getUID()
{ {
return username; return username;
} }
} }
\ No newline at end of file
...@@ -74,9 +74,10 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -74,9 +74,10 @@ public class DefaultVCardProvider implements VCardProvider {
public Element loadVCard(String username) { public Element loadVCard(String username) {
synchronized (username.intern()) { synchronized (username.intern()) {
Element vCardElement = null; Connection con = null;
java.sql.Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
Element vCardElement = null;
SAXReader xmlReader = null; SAXReader xmlReader = null;
try { try {
// Get a sax reader from the pool // Get a sax reader from the pool
...@@ -84,7 +85,7 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -84,7 +85,7 @@ public class DefaultVCardProvider implements VCardProvider {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
vCardElement = vCardElement =
xmlReader.read(new StringReader(rs.getString(1))).getRootElement(); xmlReader.read(new StringReader(rs.getString(1))).getRootElement();
...@@ -98,10 +99,7 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -98,10 +99,7 @@ public class DefaultVCardProvider implements VCardProvider {
if (xmlReader != null) { if (xmlReader != null) {
xmlReaders.add(xmlReader); xmlReaders.add(xmlReader);
} }
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return vCardElement; return vCardElement;
} }
...@@ -126,10 +124,7 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -126,10 +124,7 @@ public class DefaultVCardProvider implements VCardProvider {
Log.error("Error creating vCard for username: " + username, e); Log.error("Error creating vCard for username: " + username, e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return vCardElement; return vCardElement;
} }
...@@ -152,10 +147,7 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -152,10 +147,7 @@ public class DefaultVCardProvider implements VCardProvider {
Log.error("Error updating vCard of username: " + username, e); Log.error("Error updating vCard of username: " + username, e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
return vCardElement; return vCardElement;
} }
...@@ -173,10 +165,7 @@ public class DefaultVCardProvider implements VCardProvider { ...@@ -173,10 +165,7 @@ public class DefaultVCardProvider implements VCardProvider {
Log.error("Error deleting vCard of username: " + username, e); Log.error("Error deleting vCard of username: " + username, e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
......
...@@ -297,10 +297,7 @@ public class JiveProperties implements Map<String, String> { ...@@ -297,10 +297,7 @@ public class JiveProperties implements Map<String, String> {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -318,10 +315,7 @@ public class JiveProperties implements Map<String, String> { ...@@ -318,10 +315,7 @@ public class JiveProperties implements Map<String, String> {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
...@@ -338,35 +332,29 @@ public class JiveProperties implements Map<String, String> { ...@@ -338,35 +332,29 @@ public class JiveProperties implements Map<String, String> {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
private void loadProperties() { private void loadProperties() {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt = con.prepareStatement(LOAD_PROPERTIES);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String name = rs.getString(1); String name = rs.getString(1);
String value = rs.getString(2); String value = rs.getString(2);
properties.put(name, value); properties.put(name, value);
} }
rs.close();
} }
catch (Exception e) { catch (Exception e) {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e.getMessage(), e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e.getMessage(), e); }
} }
} }
} }
\ 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