Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class StatementCache extends StatementDecoratorInterceptor implements Sta
*/
protected static final String STATEMENT_CACHE_ATTR = StatementCache.class.getName() + ".cache";

private static final String SET_POOLABLE = "setPoolable";

/**
* Default constructor.
*/
Expand Down Expand Up @@ -346,6 +348,11 @@ protected class CachedStatement extends StatementDecoratorInterceptor.StatementP
* Cache key for this statement.
*/
CacheKey key;
/**
* Poolable hint set by the application. Tracked here as some drivers
* (e.g. H2) always return false from {@code isPoolable()}.
*/
private boolean poolable = true;
/**
* Constructs a CachedStatement.
* @param parent the parent prepared statement
Expand All @@ -365,7 +372,7 @@ public void closeInvoked() {
proxy.setCacheKey(getCacheKey());
try {
// Only cache if statement has been marked as poolable and not close on completion
if (getDelegate().isPoolable() && !getDelegate().isCloseOnCompletion()) {
if (poolable && !getDelegate().isCloseOnCompletion()) {
// clear Resultset
ResultSet result = getDelegate().getResultSet();
if (result != null && !result.isClosed()) {
Expand Down Expand Up @@ -409,6 +416,15 @@ public void closeInvoked() {
delegate = null;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = super.invoke(proxy, method, args);
if (compare(SET_POOLABLE, method)) {
poolable = ((Boolean) args[0]).booleanValue();
}
return result;
}

/**
* Forces immediate closure of this cached statement.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
import org.apache.tomcat.jdbc.pool.interceptor.StatementCache;
import org.apache.tomcat.jdbc.pool.interceptor.StatementCounterInterceptor;
import org.apache.tomcat.jdbc.test.driver.Driver;

public class TestStatementCache extends DefaultTestCase {

Expand Down Expand Up @@ -112,6 +113,37 @@ public void testPreparedStatementCache2() throws Exception {
con.close();
}

@Test
public void testPreparedStatementCacheDriverNotPoolable() throws Exception {
datasource.setDriverClassName(Driver.class.getName());
datasource.setUrl("jdbc:tomcat:test");
config(true,false,100);
Connection con = datasource.getConnection();
PreparedStatement ps1 = con.prepareStatement("select 1");
Assert.assertFalse(ps1.isPoolable());
ps1.close();
Assert.assertEquals(1,interceptor.getCacheSize().get());
con.close();
}

@Test
public void testPreparedStatementNotPoolable() throws Exception {
datasource.setDriverClassName(Driver.class.getName());
datasource.setUrl("jdbc:tomcat:test");
config(true,false,100);
Connection con = datasource.getConnection();
PreparedStatement ps1 = con.prepareStatement("select 1");
ps1.setPoolable(false);
ps1.close();
Assert.assertEquals(0,interceptor.getCacheSize().get());
PreparedStatement ps2 = con.prepareStatement("select 1");
ps2.setPoolable(false);
ps2.setPoolable(true);
ps2.close();
Assert.assertEquals(1,interceptor.getCacheSize().get());
con.close();
}

@Test
public void testStatementClose1() throws Exception {
init();
Expand Down
8 changes: 8 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,14 @@
configuration. Pull request <pr>1084</pr> provided by Raphael Vullriede.
(markt)
</fix>
<fix>
<bug>70269</bug>: Correct a regression in the change that reset the
properties of statements cached by the <code>StatementCache</code>
interceptor that meant no statements were cached when using JDBC
drivers, such as H2 and MariaDB Connector/J, that always report
statements as not poolable. Pull request <pr>1083</pr> provided by
Raphael Vullriede. (markt)
</fix>
</changelog>
</subsection>
<subsection name="Other">
Expand Down