[PATCH] Fixed open cursor leaks in Oracle by closing all ResultSets and PreparedStatements

Michael Jaszczyk <[email protected]>
Newsgroups gmane.comp.java.openjms.devel
Message-ID <[email protected]>
-- 
PIRONET NDH
Michael Jaszczyk - Director Software
Maarweg 149 - 161, 50825 Cologne - Germany 
Phone: +49 (0)221 770 1720 - Fax: +49 (0)221 770 1005 
<mailto:[email protected]> - <http://www.pironet-ndh.com/>
RDBMSAdapter.patch (text/plain, 6.5 KB)
Index: RDBMSAdapter.java
===================================================================
RCS file: /cvsroot/openjms/openjms/src/main/org/exolab/jms/persistence/RDBMSAdapter.java,v
retrieving revision 1.53
diff -u -w -i -b -r1.53 RDBMSAdapter.java
--- RDBMSAdapter.java	28 Oct 2003 07:27:29 -0000	1.53
+++ RDBMSAdapter.java	5 Jan 2004 08:48:27 -0000
@@ -54,36 +54,25 @@
 import java.sql.SQLException;
 import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Vector;
 
-import javax.jms.JMSException;
-import javax.sql.ConnectionPoolDataSource;
-import javax.sql.DataSource;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
-import EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock;
-import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
 import org.exolab.core.foundation.HandleIfc;
 import org.exolab.jms.authentication.User;
 import org.exolab.jms.client.JmsDestination;
 import org.exolab.jms.client.JmsQueue;
 import org.exolab.jms.client.JmsTopic;
-import org.exolab.jms.config.Configuration;
 import org.exolab.jms.config.ConfigurationManager;
 import org.exolab.jms.config.DatabaseConfiguration;
 import org.exolab.jms.config.RdbmsDatabaseConfiguration;
-import org.exolab.jms.events.BasicEventManager;
-import org.exolab.jms.events.Event;
 import org.exolab.jms.events.EventHandler;
-import org.exolab.jms.events.IllegalEventDefinedException;
 import org.exolab.jms.message.MessageImpl;
 import org.exolab.jms.messagemgr.PersistentMessageHandle;
 
+import EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock;
+import EDU.oswego.cs.dl.util.concurrent.ReadWriteLock;
+
 
 /**
  * This adapter is a wrapper class around the persistency mechanism.
@@ -208,24 +197,14 @@
             connection.commit();
             Users.initialise(connection);
         } catch (PersistenceException exception) {
-            // rollback and rethrow the exception
-            if (connection != null) {
-                try {
-                    connection.rollback();
-                } catch (Exception ignore) {
-                }
-            }
+            	SQLHelper.rollback(connection);
             throw exception;
         } catch (Exception exception) {
             throw new PersistenceException(
                 "Failed to initialise database adapter", exception);
         } finally {
-            if (connection != null) {
-                try {
-                    connection.close();
-                } catch (Exception ignore) {
-                }
-            }
+            SQLHelper.close(connection);
+            
         }
 
         // check whether we should initiate automatic garbage collection
@@ -283,28 +262,33 @@
 
         long lastId = -1;
         boolean successful = false;
-
+		PreparedStatement query = null;
+		ResultSet result = null;
+		PreparedStatement insert = null;
         try {
-            PreparedStatement query = connection.prepareStatement(
+            query = connection.prepareStatement(
                 "select * from message_id where id = 1");
-            ResultSet result = query.executeQuery();
+            result = query.executeQuery();
 
             if (result.next()) {
                 lastId = result.getInt("maxId");
             } else {
                 // first entry create.
-                PreparedStatement insert = connection.prepareStatement(
+                insert = connection.prepareStatement(
                     "insert into message_id values (?,?)");
                 insert.setInt(1, 1);
                 insert.setLong(2, 0);
                 insert.executeUpdate();
                 lastId = 0;
-                insert.close();
             }
-            query.close();
         } catch (Exception exception) {
             throw new PersistenceException("Failed to get last message id",
                 exception);
+        }finally{
+        	SQLHelper.close(result);
+        	SQLHelper.close(insert);
+			SQLHelper.close(query);
+        	
         }
 
         return lastId;
@@ -313,18 +297,19 @@
     // implementation of PersistenceAdapter.updateIds
     public void updateIds(Connection connection, long id)
         throws PersistenceException {
-
+		PreparedStatement insert = null;
         try {
-            PreparedStatement insert =
+            insert =
                 connection.prepareStatement
                 ("update message_id set maxId = ? where id = 1");
 
             insert.setLong(1, id);
             insert.executeUpdate();
-            insert.close();
         } catch (Exception exception) {
             throw new PersistenceException("Failed to update message id",
                 exception);
+        }finally{
+        	SQLHelper.close(insert);
         }
     }
 
@@ -885,19 +870,22 @@
         throws PersistenceException {
 
         String version = null;
-
+		PreparedStatement query = null;
+		ResultSet result = null;
         try {
-            PreparedStatement query = connection.prepareStatement(
+            query = connection.prepareStatement(
                 "select * from system_data where id = 1");
-            ResultSet result = query.executeQuery();
+            result = query.executeQuery();
             if (result.next()) {
                 version = result.getString("version");
             }
-            query.close();
-            result.close();
         } catch (SQLException exception) {
             throw new PersistenceException(
                 "Failed to get the schema version", exception);
+        } finally{
+        	SQLHelper.close(result);
+			SQLHelper.close(query);
+        	
         }
         return version;
     }
@@ -911,17 +899,20 @@
         throws PersistenceException {
 
         _log.info("Initialising schema version " + SCHEMA_VERSION);
+		PreparedStatement insert = null;
         try {
-            PreparedStatement insert = connection.prepareStatement(
+            insert = connection.prepareStatement(
                 "insert into system_data values (?,?,?)");
             insert.setInt(1, 1);
             insert.setString(2, SCHEMA_VERSION);
             insert.setDate(3, new Date(System.currentTimeMillis()));
             insert.executeUpdate();
-            insert.close();
+            
         } catch (SQLException exception) {
             throw new PersistenceException(
                 "Failed to initialise schema version", exception);
+        } finally{
+        	SQLHelper.close(insert);
         }
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.