cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject svn commit: r1345232 - /cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/persistence/jdbc/RMTxStore.java
Date Fri, 01 Jun 2012 15:21:38 GMT
Author: ay
Date: Fri Jun  1 15:21:38 2012
New Revision: 1345232

URL: http://svn.apache.org/viewvc?rev=1345232&view=rev
Log:
[CXF-4354] RMTxStore should explicitly close ResultSets objects

Modified:
    cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/persistence/jdbc/RMTxStore.java

Modified: cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/persistence/jdbc/RMTxStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/persistence/jdbc/RMTxStore.java?rev=1345232&r1=1345231&r2=1345232&view=diff
==============================================================================
--- cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/persistence/jdbc/RMTxStore.java
(original)
+++ cxf/trunk/rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/persistence/jdbc/RMTxStore.java
Fri Jun  1 15:21:38 2012
@@ -315,10 +315,11 @@ public class RMTxStore implements RMStor
         if (LOG.isLoggable(Level.FINE)) {
             LOG.info("Getting destination sequence for id: " + sid);
         }
+        ResultSet res = null;
         try {
             synchronized (selectDestSequenceStmt) {
                 selectDestSequenceStmt.setString(1, sid.getValue());
-                ResultSet res = selectDestSequenceStmt.executeQuery();
+                res = selectDestSequenceStmt.executeQuery();
             
                 if (res.next()) {
                     EndpointReferenceType acksTo = RMUtils.createReference(res.getString(1));
 
@@ -335,6 +336,14 @@ public class RMTxStore implements RMStor
             }
         } catch (SQLException ex) {
             LOG.log(Level.WARNING, new Message("SELECT_DEST_SEQ_FAILED_MSG", LOG).toString(),
ex);
+        } finally {
+            if (res != null) {
+                try {
+                    res.close();
+                } catch (SQLException e) {
+                    // ignore
+                }
+            }
         }
         return null;
     }
@@ -343,10 +352,11 @@ public class RMTxStore implements RMStor
         if (LOG.isLoggable(Level.FINE)) {
             LOG.info("Getting source sequences for id: " + sid);
         }
+        ResultSet res = null;
         try {
             synchronized (selectSrcSequenceStmt) {
                 selectSrcSequenceStmt.setString(1, sid.getValue());
-                ResultSet res = selectSrcSequenceStmt.executeQuery();
+                res = selectSrcSequenceStmt.executeQuery();
             
                 if (res.next()) {
                     long cmn = res.getLong(1);
@@ -366,7 +376,15 @@ public class RMTxStore implements RMStor
         } catch (SQLException ex) {
             // ignore
             LOG.log(Level.WARNING, new Message("SELECT_SRC_SEQ_FAILED_MSG", LOG).toString(),
ex);
-        }
+        } finally {
+            if (res != null) {
+                try {
+                    res.close();
+                } catch (SQLException e) {
+                    // ignore
+                }
+            }
+        } 
         return null;
     }
 
@@ -406,10 +424,11 @@ public class RMTxStore implements RMStor
             LOG.info("Getting destination sequences for endpoint: " + endpointIdentifier);
         }
         Collection<DestinationSequence> seqs = new ArrayList<DestinationSequence>();
+        ResultSet res = null;
         try {
             synchronized (selectDestSequencesStmt) {
                 selectDestSequencesStmt.setString(1, endpointIdentifier);
-                ResultSet res = selectDestSequencesStmt.executeQuery(); 
+                res = selectDestSequencesStmt.executeQuery(); 
                 while (res.next()) {
                     Identifier sid = new Identifier();                
                     sid.setValue(res.getString(1));
@@ -428,7 +447,15 @@ public class RMTxStore implements RMStor
             }
         } catch (SQLException ex) {
             LOG.log(Level.WARNING, new Message("SELECT_DEST_SEQ_FAILED_MSG", LOG).toString(),
ex);
-        }
+        } finally {
+            if (res != null) {
+                try {
+                    res.close();
+                } catch (SQLException e) {
+                    // ignore
+                }
+            }
+        } 
         return seqs;
     }
     
@@ -437,10 +464,11 @@ public class RMTxStore implements RMStor
             LOG.info("Getting source sequences for endpoint: " + endpointIdentifier);
         }
         Collection<SourceSequence> seqs = new ArrayList<SourceSequence>();
+        ResultSet res = null;
         try {
             synchronized (selectSrcSequencesStmt) {
                 selectSrcSequencesStmt.setString(1, endpointIdentifier);
-                ResultSet res = selectSrcSequencesStmt.executeQuery();
+                res = selectSrcSequencesStmt.executeQuery();
                 while (res.next()) {
                     Identifier sid = new Identifier();
                     sid.setValue(res.getString(1));
@@ -462,17 +490,26 @@ public class RMTxStore implements RMStor
         } catch (SQLException ex) {
             // ignore
             LOG.log(Level.WARNING, new Message("SELECT_SRC_SEQ_FAILED_MSG", LOG).toString(),
ex);
-        }
+        } finally {
+            if (res != null) {
+                try {
+                    res.close();
+                } catch (SQLException e) {
+                    // ignore
+                }
+            }
+        } 
         return seqs;
     }
     
     public Collection<RMMessage> getMessages(Identifier sid, boolean outbound) {
         Collection<RMMessage> msgs = new ArrayList<RMMessage>();
+        ResultSet res = null;
         try {
             PreparedStatement stmt = outbound ? selectOutboundMessagesStmt : selectInboundMessagesStmt;
             synchronized (stmt) {
                 stmt.setString(1, sid.getValue());
-                ResultSet res = stmt.executeQuery();
+                res = stmt.executeQuery();
                 while (res.next()) {
                     long mn = res.getLong(1);
                     String to = res.getString(2);
@@ -487,6 +524,14 @@ public class RMTxStore implements RMStor
         } catch (Exception ex) {
             LOG.log(Level.WARNING, new Message(outbound ? "SELECT_OUTBOUND_MSGS_FAILED_MSG"
                 : "SELECT_INBOUND_MSGS_FAILED_MSG", LOG).toString(), ex);
+        } finally {
+            if (res != null) {
+                try {
+                    res.close();
+                } catch (SQLException e) {
+                    // ignore
+                }
+            }
         }
         return msgs;
     }
@@ -605,29 +650,26 @@ public class RMTxStore implements RMStor
     
     protected void updateSourceSequence(SourceSequence seq) 
         throws SQLException {
-        if (null == updateSrcSequenceStmt) {
-            updateSrcSequenceStmt = connection.prepareStatement(UPDATE_SRC_SEQUENCE_STMT_STR);
+        synchronized (updateSrcSequenceStmt) {
+            updateSrcSequenceStmt.setLong(1, seq.getCurrentMessageNr()); 
+            updateSrcSequenceStmt.setString(2, seq.isLastMessage() ? "1" : "0"); 
+            updateSrcSequenceStmt.setString(3, seq.getIdentifier().getValue());
+            updateSrcSequenceStmt.execute();
         }
-        updateSrcSequenceStmt.setLong(1, seq.getCurrentMessageNr()); 
-        updateSrcSequenceStmt.setString(2, seq.isLastMessage() ? "1" : "0"); 
-        updateSrcSequenceStmt.setString(3, seq.getIdentifier().getValue());
-        updateSrcSequenceStmt.execute();
     }
     
     protected void updateDestinationSequence(DestinationSequence seq) 
         throws SQLException, IOException {
-        if (null == updateDestSequenceStmt) {
-            updateDestSequenceStmt = connection.prepareStatement(UPDATE_DEST_SEQUENCE_STMT_STR);
+        synchronized (updateDestSequenceStmt) {
+            long lastMessageNr = seq.getLastMessageNumber();
+            updateDestSequenceStmt.setLong(1, lastMessageNr); 
+            InputStream is = PersistenceUtils.getInstance()
+                .serialiseAcknowledgment(seq.getAcknowledgment());
+            updateDestSequenceStmt.setBinaryStream(2, is, is.available()); 
+            updateDestSequenceStmt.setString(3, seq.getIdentifier() .getValue());
+            updateDestSequenceStmt.execute();
         }
-        long lastMessageNr = seq.getLastMessageNumber();
-        updateDestSequenceStmt.setLong(1, lastMessageNr); 
-        InputStream is = PersistenceUtils.getInstance()
-            .serialiseAcknowledgment(seq.getAcknowledgment());
-        updateDestSequenceStmt.setBinaryStream(2, is, is.available()); 
-        updateDestSequenceStmt.setString(3, seq.getIdentifier() .getValue());
-        updateDestSequenceStmt.execute();
     }
-    
     protected void createTables() throws SQLException {
         
         Statement stmt = null;
@@ -641,8 +683,9 @@ public class RMTxStore implements RMStor
                 LOG.fine("Table CXF_RM_SRC_SEQUENCES already exists.");
                 verifyTable(SRC_SEQUENCES_TABLE_NAME, SRC_SEQUENCES_TABLE_COLS);
             }
+        } finally {
+            stmt.close();
         }
-        stmt.close();
 
         stmt = connection.createStatement();
         try {
@@ -654,8 +697,9 @@ public class RMTxStore implements RMStor
                 LOG.fine("Table CXF_RM_DEST_SEQUENCES already exists.");
                 verifyTable(DEST_SEQUENCES_TABLE_NAME, DEST_SEQUENCES_TABLE_COLS);      
 
             }
+        } finally {
+            stmt.close();
         }
-        stmt.close();
         
         for (String tableName : new String[] {OUTBOUND_MSGS_TABLE_NAME, INBOUND_MSGS_TABLE_NAME})
{
             stmt = connection.createStatement();
@@ -670,8 +714,9 @@ public class RMTxStore implements RMStor
                     }
                     verifyTable(tableName, MESSAGES_TABLE_COLS);
                 }
+            } finally {
+                stmt.close();
             }
-            stmt.close();
         }
     }
     
@@ -696,9 +741,8 @@ public class RMTxStore implements RMStor
                 }
                 
                 for (String[] newCol : newCols) {
-                    Statement st = null;
+                    Statement st = connection.createStatement();
                     try {
-                        st = connection.createStatement();
                         st.executeUpdate(MessageFormat.format(ALTER_TABLE_STMT_STR, 
                                                               tableName, newCol[0], newCol[1]));
                         if (LOG.isLoggable(Level.FINE)) {
@@ -706,9 +750,7 @@ public class RMTxStore implements RMStor
                                     new Object[] {tableName, newCol[0]});
                         }
                     } finally {
-                        if (st != null) {
-                            st.close();
-                        }
+                        st.close();
                     }
                 }
             }
@@ -732,8 +774,9 @@ public class RMTxStore implements RMStor
                                                     schemaName));
         } catch (SQLException ex) {
             // assume it is already created or no authorization is provided (create one manually)
+        } finally {
+            stmt.close();
         }
-        stmt.close();
         stmt = connection.createStatement();
         SQLException ex0 = null;
         for (int i = 0; i < SET_SCHEMA_STMT_STRS.length; i++) {
@@ -747,9 +790,13 @@ public class RMTxStore implements RMStor
                     throw ex0;
                 }
                 // continue
+            } finally {
+                // close the statement after its last use
+                if (ex0 == null || i == SET_SCHEMA_STMT_STRS.length - 1) {
+                    stmt.close();
+                }
             }
         }
-        stmt.close();
     }
 
     private void createStatements() throws SQLException {



Mime
View raw message