Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions velocity-engine-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@
<scope>test</scope>
<classifier>${test.jdbc.driver.classifier}</classifier>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>9.0.104</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,14 @@ public void close() throws IOException
super.close();
try
{
resultSet.close();
factory.releaseStatement(templateSQL, (PreparedStatement)resultSet.getStatement());
PreparedStatement statement = (PreparedStatement) resultSet.getStatement();
try {
if (!resultSet.isClosed()) {
resultSet.close();
}
} finally {
factory.releaseStatement(templateSQL, statement);
}
}
catch (RuntimeException re)
{
Expand Down Expand Up @@ -295,7 +301,7 @@ public synchronized Reader getResourceReader(final String name, String encoding)
throw new ResourceNotFoundException("DataSourceResourceLoader: Template name was empty or null");
}

ResultSet rs = null;
ResultSet rs;
try
{
PreparedStatement statement = factory.prepareStatement(templateSQL);
Expand All @@ -314,6 +320,11 @@ public synchronized Reader getResourceReader(final String name, String encoding)
}
else
{
try {
closeResultSet(rs);
} finally {
factory.releaseStatement(templateSQL, statement);
}
throw new ResourceNotFoundException("DataSourceResourceLoader: "
+ "could not find resource '"
+ name + "'");
Expand Down Expand Up @@ -408,7 +419,9 @@ private void closeResultSet(final ResultSet rs)
{
try
{
rs.close();
if (!rs.isClosed()) {
rs.close();
}
}
catch (RuntimeException re)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void init(DataSource dataSource, ExtProperties properties)
public PreparedStatement prepareStatement(String sql) throws SQLException
{
Connection connection = dataSource.getConnection();
return connection.prepareStatement(sql);
return PreparedStatementProxy.newInstance(connection.prepareStatement(sql), connection);
}

/**
Expand All @@ -49,11 +49,15 @@ public void releaseStatement(String sql, PreparedStatement stmt) throws SQLExcep
Connection connection = stmt.getConnection();
try
{
stmt.close();
if (!stmt.isClosed()) {
stmt.close();
}
}
finally
{
connection.close();
if (!connection.isClosed()) {
connection.close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.apache.velocity.runtime.resource.loader;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* Proxy for {@link java.sql.PreparedStatement} that guarantees getConnection will always return the DataSource
* provided wrapped {@link java.sql.Connection} and not the underlying database specific connection.
* Also overrides executeQuery to return a proxy for {@link java.sql.ResultSet} see {@link ResultSetProxy}
*/
public class PreparedStatementProxy implements InvocationHandler {
private final PreparedStatement wrappedPreparedStatement;
private PreparedStatement proxyPreparedStatement;
private final Connection wrappedConnection;

public PreparedStatementProxy(PreparedStatement wrappedPreparedStatement, Connection wrappedConnection) {
this.wrappedPreparedStatement = wrappedPreparedStatement;
this.wrappedConnection = wrappedConnection;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result;
if (method.getName().equals("getConnection")) {
result = wrappedConnection;
} else if (method.getName().equals("executeQuery")) {
return ResultSetProxy.newInstance((ResultSet) method.invoke(wrappedPreparedStatement, args), proxyPreparedStatement);
} else {
result = method.invoke(wrappedPreparedStatement, args);
}
return result;
}

public static PreparedStatement newInstance(PreparedStatement ps, Connection con) throws SQLException {
PreparedStatementProxy invocationHandler = new PreparedStatementProxy(ps, con);
PreparedStatement proxyPreparedStatement = (PreparedStatement) Proxy.newProxyInstance(ps.getClass().getClassLoader(),
ps.getClass().getInterfaces(),
invocationHandler);
invocationHandler.setProxyPreparedStatement(proxyPreparedStatement);
return proxyPreparedStatement;
}

public void setProxyPreparedStatement(PreparedStatement proxyPreparedStatement) {
this.proxyPreparedStatement = proxyPreparedStatement;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.apache.velocity.runtime.resource.loader;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Proxy for {@link java.sql.ResultSet} that guarantees getStatement will return the original wrapped {@link java.sql.Statement}
* and will not throw an exception or return null if the ResultSet is closed.
*/
public class ResultSetProxy implements InvocationHandler {
private final ResultSet wrappedResultSet;
private final Statement wrappedStatement;

public ResultSetProxy(ResultSet wrappedResultSet, Statement wrappedStatement) {
this.wrappedResultSet = wrappedResultSet;
this.wrappedStatement = wrappedStatement;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result;
if (method.getName().equals("getStatement")) {
result = wrappedStatement;
} else {
result = method.invoke(wrappedResultSet, args);
}
return result;
}

public static ResultSet newInstance(ResultSet rs, Statement ps) throws SQLException {
return (ResultSet) Proxy.newProxyInstance(rs.getClass().getClassLoader(),
rs.getClass().getInterfaces(),
new ResultSetProxy(rs, ps));
}

}
Loading