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