Author: nbubna
Date: Wed Sep  1 19:13:46 2010
New Revision: 991660

URL: http://svn.apache.org/viewvc?rev=991660&view=rev
Log:
VELOCITY-760 ensure PreparedStatements get closed (thanks to Jarkko who should 
be committing this himself :)

Modified:
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java?rev=991660&r1=991659&r2=991660&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
 Wed Sep  1 19:13:46 2010
@@ -225,10 +225,12 @@ public class DataSourceResourceLoader ex
 
         Connection conn = null;
         ResultSet rs = null;
+        PreparedStatement ps = null;
         try
         {
             conn = openDbConnection();
-            rs = readData(conn, templateColumn, name);
+            ps = getStatement(conn, templateColumn, name);
+            rs = ps.executeQuery();
 
             if (rs.next())
             {
@@ -269,6 +271,7 @@ public class DataSourceResourceLoader ex
         finally
         {
             closeResultSet(rs);
+            closeStatement(ps);
             closeDbConnection(conn);
         }
     }
@@ -297,11 +300,13 @@ public class DataSourceResourceLoader ex
         {
             Connection conn = null;
             ResultSet rs = null;
+            PreparedStatement ps = null;
 
             try
             {
                 conn = openDbConnection();
-                rs = readData(conn, timestampColumn, name);
+                ps = getStatement(conn, timestampColumn, name);
+                rs = ps.executeQuery();
 
                 if (rs.next())
                 {
@@ -335,6 +340,7 @@ public class DataSourceResourceLoader ex
             finally
             {
                 closeResultSet(rs);
+                closeStatement(ps);
                 closeDbConnection(conn);
             }
         }
@@ -411,9 +417,34 @@ public class DataSourceResourceLoader ex
             }
         }
     }
+    
+    /**
+     * Closes the PreparedStatement.
+     */
+    private void closeStatement(PreparedStatement ps)
+    {
+        if (ps != null)
+        {
+            try
+            {
+                ps.close();
+            }
+            catch (RuntimeException re)
+            {
+                throw re;
+            }
+            catch (Exception e)
+            {
+                String msg = "DataSourceResourceLoader: problem when closing 
PreparedStatement ";
+                log.error(msg, e);
+                throw new VelocityException(msg, e);
+            }
+        }
+    }
+        
 
     /**
-     * Reads the data from the datasource.  It simply does the following query 
:
+     * Creates the following PreparedStatement query :
      * <br>
      *  SELECT <i>columnNames</i> FROM <i>tableName</i> WHERE <i>keyColumn</i>
      *     = '<i>templateName</i>'
@@ -423,15 +454,15 @@ public class DataSourceResourceLoader ex
      * @param conn connection to datasource
      * @param columnNames columns to fetch from datasource
      * @param templateName name of template to fetch
-     * @return result set from query
+     * @return PreparedStatement
      */
-    private ResultSet readData(final Connection conn,
+    private PreparedStatement getStatement(final Connection conn,
                                final String columnNames,
                                final String templateName) throws SQLException
     {
         PreparedStatement ps = conn.prepareStatement("SELECT " + columnNames + 
" FROM "+ tableName + " WHERE " + keyColumn + " = ?");
         ps.setString(1, templateName);
-        return ps.executeQuery();
+        return ps;
     }
 
 }


Reply via email to