summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorStig Inge Lea Bjørnsen2013-10-29 13:31:34 (EDT)
committer Jan Bartel2013-10-30 22:06:50 (EDT)
commit824252da9847993ecf668477ab03115fb0901381 (patch)
tree6fe01ebaac14d200c11ad36dd87456918794e616
parent77096b07d161fd17a2fe81e804583923c982f2d1 (diff)
downloadorg.eclipse.jetty.project-824252da9847993ecf668477ab03115fb0901381.zip
org.eclipse.jetty.project-824252da9847993ecf668477ab03115fb0901381.tar.gz
org.eclipse.jetty.project-824252da9847993ecf668477ab03115fb0901381.tar.bz2
420273 Close JDBC statements to prevent database resource leakages.
Make JDBCSessionManager and JDBCSessionIdManager explicitly close JDBC statements. When using a connection pool, not closing JDBC Statements and PreparedStatements lead to cursor leakages in the database. ResultSets are still not explicitly closed as they are closed automatically when their statement is closed. Signed-off-by: Stig Inge Lea Bjørnsen <stiginge@pvv.org>
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionIdManager.java104
-rw-r--r--jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java36
2 files changed, 124 insertions, 16 deletions
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionIdManager.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionIdManager.java
index 1a10ba6..4e0cbf3 100644
--- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionIdManager.java
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionIdManager.java
@@ -634,10 +634,21 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
if (!(index1Exists && index2Exists))
{
Statement statement = connection.createStatement();
- if (!index1Exists)
- statement.executeUpdate("create index "+index1+" on "+_sessionTable+" (expiryTime)");
- if (!index2Exists)
- statement.executeUpdate("create index "+index2+" on "+_sessionTable+" (sessionId, contextPath)");
+ try
+ {
+ if (!index1Exists)
+ statement.executeUpdate("create index "+index1+" on "+_sessionTable+" (expiryTime)");
+ if (!index2Exists)
+ statement.executeUpdate("create index "+index2+" on "+_sessionTable+" (sessionId, contextPath)");
+ }
+ finally
+ {
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+ }
}
//set up some strings representing the statements for session manipulation
@@ -676,23 +687,37 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
throws SQLException
{
Connection connection = null;
+ PreparedStatement statement = null;
+ PreparedStatement query = null;
try
{
connection = getConnection();
connection.setAutoCommit(true);
- PreparedStatement query = connection.prepareStatement(_queryId);
+ query = connection.prepareStatement(_queryId);
query.setString(1, id);
ResultSet result = query.executeQuery();
//only insert the id if it isn't in the db already
if (!result.next())
{
- PreparedStatement statement = connection.prepareStatement(_insertId);
+ statement = connection.prepareStatement(_insertId);
statement.setString(1, id);
statement.executeUpdate();
}
}
finally
{
+ if (query!=null)
+ {
+ try { query.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection != null)
connection.close();
}
@@ -708,16 +733,23 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
throws SQLException
{
Connection connection = null;
+ PreparedStatement statement = null;
try
{
connection = getConnection();
connection.setAutoCommit(true);
- PreparedStatement statement = connection.prepareStatement(_deleteId);
+ statement = connection.prepareStatement(_deleteId);
statement.setString(1, id);
statement.executeUpdate();
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection != null)
connection.close();
}
@@ -735,17 +767,24 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
throws SQLException
{
Connection connection = null;
+ PreparedStatement statement = null;
try
{
connection = getConnection();
connection.setAutoCommit(true);
- PreparedStatement statement = connection.prepareStatement(_queryId);
+ statement = connection.prepareStatement(_queryId);
statement.setString(1, id);
ResultSet result = statement.executeQuery();
return result.next();
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection != null)
connection.close();
}
@@ -765,6 +804,7 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
private void scavenge ()
{
Connection connection = null;
+ PreparedStatement statement = null;
List<String> expiredSessionIds = new ArrayList<String>();
try
{
@@ -775,7 +815,7 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
connection = getConnection();
connection.setAutoCommit(true);
//"select sessionId from JettySessions where expiryTime > (lastScavengeTime - scanInterval) and expiryTime < lastScavengeTime";
- PreparedStatement statement = connection.prepareStatement(_selectBoundedExpiredSessions);
+ statement = connection.prepareStatement(_selectBoundedExpiredSessions);
long lowerBound = (_lastScavengeTime - _scavengeIntervalMs);
long upperBound = _lastScavengeTime;
if (LOG.isDebugEnabled())
@@ -812,10 +852,21 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
if (upperBound > 0)
{
if (LOG.isDebugEnabled()) LOG.debug("Deleting old expired sessions expired before "+upperBound);
- statement = connection.prepareStatement(_deleteOldExpiredSessions);
- statement.setLong(1, upperBound);
- int rows = statement.executeUpdate();
- if (LOG.isDebugEnabled()) LOG.debug("Deleted "+rows+" rows of old sessions expired before "+upperBound);
+ try
+ {
+ statement = connection.prepareStatement(_deleteOldExpiredSessions);
+ statement.setLong(1, upperBound);
+ int rows = statement.executeUpdate();
+ if (LOG.isDebugEnabled()) LOG.debug("Deleted "+rows+" rows of old sessions expired before "+upperBound);
+ }
+ finally
+ {
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+ }
}
}
}
@@ -852,6 +903,9 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
throws Exception
{
Connection connection = null;
+ PreparedStatement statement = null;
+ Statement sessionsTableStatement = null;
+ Statement sessionIdsTableStatement = null;
List<String> expiredSessionIds = new ArrayList<String>();
try
{
@@ -859,7 +913,7 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
connection.setAutoCommit(false);
- PreparedStatement statement = connection.prepareStatement(_selectExpiredSessions);
+ statement = connection.prepareStatement(_selectExpiredSessions);
long now = System.currentTimeMillis();
if (LOG.isDebugEnabled()) LOG.debug ("Searching for sessions expired before {}", now);
@@ -872,8 +926,8 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
if (LOG.isDebugEnabled()) LOG.debug ("Found expired sessionId={}", sessionId);
}
- Statement sessionsTableStatement = null;
- Statement sessionIdsTableStatement = null;
+ sessionsTableStatement = null;
+ sessionIdsTableStatement = null;
if (!expiredSessionIds.isEmpty())
{
@@ -897,6 +951,24 @@ public class JDBCSessionIdManager extends AbstractSessionIdManager
}
finally
{
+ if (sessionIdsTableStatement!=null)
+ {
+ try { sessionIdsTableStatement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
+ if (sessionsTableStatement!=null)
+ {
+ try { sessionsTableStatement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
try
{
if (connection != null)
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java
index 7dc90ea..aedba24 100644
--- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionManager.java
@@ -843,6 +843,12 @@ public class JDBCSessionManager extends AbstractSessionManager
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection!=null)
{
try { connection.close();}
@@ -920,6 +926,12 @@ public class JDBCSessionManager extends AbstractSessionManager
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection!=null)
connection.close();
}
@@ -967,6 +979,12 @@ public class JDBCSessionManager extends AbstractSessionManager
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection!=null)
connection.close();
}
@@ -998,6 +1016,12 @@ public class JDBCSessionManager extends AbstractSessionManager
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection!=null)
connection.close();
}
@@ -1033,6 +1057,12 @@ public class JDBCSessionManager extends AbstractSessionManager
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection!=null)
connection.close();
}
@@ -1064,6 +1094,12 @@ public class JDBCSessionManager extends AbstractSessionManager
}
finally
{
+ if (statement!=null)
+ {
+ try { statement.close(); }
+ catch(Exception e) { LOG.warn(e); }
+ }
+
if (connection!=null)
connection.close();
}