You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2010/09/01 21:11:19 UTC
svn commit: r991657 -
/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
Author: nbubna
Date: Wed Sep 1 19:11:19 2010
New Revision: 991657
URL: http://svn.apache.org/viewvc?rev=991657&view=rev
Log:
VELOCITY-760 ensure PreparedStatements get closed (thanks to Jarkko who should be committing this himself :)
Modified:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
Modified: velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java?rev=991657&r1=991656&r2=991657&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java (original)
+++ velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java Wed Sep 1 19:11:19 2010
@@ -224,10 +224,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())
{
@@ -268,6 +270,7 @@ public class DataSourceResourceLoader ex
finally
{
closeResultSet(rs);
+ closeStatement(ps);
closeDbConnection(conn);
}
}
@@ -296,11 +299,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())
{
@@ -334,6 +339,7 @@ public class DataSourceResourceLoader ex
finally
{
closeResultSet(rs);
+ closeStatement(ps);
closeDbConnection(conn);
}
}
@@ -410,9 +416,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>'
@@ -422,15 +453,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;
}
}