Skip to content

Commit 87f1a69

Browse files
Fix BZ 70269 - StatementCache regression with drivers that report statements as not poolable (#1083)
https://bz.apache.org/bugzilla/show_bug.cgi?id=70269 Co-authored-by: Mark Thomas <markt@apache.org>
1 parent 9649e5f commit 87f1a69

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class StatementCache extends StatementDecoratorInterceptor implements Sta
6363
*/
6464
protected static final String STATEMENT_CACHE_ATTR = StatementCache.class.getName() + ".cache";
6565

66+
private static final String SET_POOLABLE = "setPoolable";
67+
6668
/**
6769
* Default constructor.
6870
*/
@@ -346,6 +348,11 @@ protected class CachedStatement extends StatementDecoratorInterceptor.StatementP
346348
* Cache key for this statement.
347349
*/
348350
CacheKey key;
351+
/**
352+
* Poolable hint set by the application. Tracked here as some drivers
353+
* (e.g. H2) always return false from {@code isPoolable()}.
354+
*/
355+
private boolean poolable = true;
349356
/**
350357
* Constructs a CachedStatement.
351358
* @param parent the parent prepared statement
@@ -365,7 +372,7 @@ public void closeInvoked() {
365372
proxy.setCacheKey(getCacheKey());
366373
try {
367374
// Only cache if statement has been marked as poolable and not close on completion
368-
if (getDelegate().isPoolable() && !getDelegate().isCloseOnCompletion()) {
375+
if (poolable && !getDelegate().isCloseOnCompletion()) {
369376
// clear Resultset
370377
ResultSet result = getDelegate().getResultSet();
371378
if (result != null && !result.isClosed()) {
@@ -409,6 +416,15 @@ public void closeInvoked() {
409416
delegate = null;
410417
}
411418

419+
@Override
420+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
421+
Object result = super.invoke(proxy, method, args);
422+
if (compare(SET_POOLABLE, method)) {
423+
poolable = ((Boolean) args[0]).booleanValue();
424+
}
425+
return result;
426+
}
427+
412428
/**
413429
* Forces immediate closure of this cached statement.
414430
*/

‎modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/TestStatementCache.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
2828
import org.apache.tomcat.jdbc.pool.interceptor.StatementCache;
2929
import org.apache.tomcat.jdbc.pool.interceptor.StatementCounterInterceptor;
30+
import org.apache.tomcat.jdbc.test.driver.Driver;
3031

3132
public class TestStatementCache extends DefaultTestCase {
3233

@@ -112,6 +113,37 @@ public void testPreparedStatementCache2() throws Exception {
112113
con.close();
113114
}
114115

116+
@Test
117+
public void testPreparedStatementCacheDriverNotPoolable() throws Exception {
118+
datasource.setDriverClassName(Driver.class.getName());
119+
datasource.setUrl("jdbc:tomcat:test");
120+
config(true,false,100);
121+
Connection con = datasource.getConnection();
122+
PreparedStatement ps1 = con.prepareStatement("select 1");
123+
Assert.assertFalse(ps1.isPoolable());
124+
ps1.close();
125+
Assert.assertEquals(1,interceptor.getCacheSize().get());
126+
con.close();
127+
}
128+
129+
@Test
130+
public void testPreparedStatementNotPoolable() throws Exception {
131+
datasource.setDriverClassName(Driver.class.getName());
132+
datasource.setUrl("jdbc:tomcat:test");
133+
config(true,false,100);
134+
Connection con = datasource.getConnection();
135+
PreparedStatement ps1 = con.prepareStatement("select 1");
136+
ps1.setPoolable(false);
137+
ps1.close();
138+
Assert.assertEquals(0,interceptor.getCacheSize().get());
139+
PreparedStatement ps2 = con.prepareStatement("select 1");
140+
ps2.setPoolable(false);
141+
ps2.setPoolable(true);
142+
ps2.close();
143+
Assert.assertEquals(1,interceptor.getCacheSize().get());
144+
con.close();
145+
}
146+
115147
@Test
116148
public void testStatementClose1() throws Exception {
117149
init();

‎webapps/docs/changelog.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,14 @@
747747
configuration. Pull request <pr>1084</pr> provided by Raphael Vullriede.
748748
(markt)
749749
</fix>
750+
<fix>
751+
<bug>70269</bug>: Correct a regression in the change that reset the
752+
properties of statements cached by the <code>StatementCache</code>
753+
interceptor that meant no statements were cached when using JDBC
754+
drivers, such as H2 and MariaDB Connector/J, that always report
755+
statements as not poolable. Pull request <pr>1083</pr> provided by
756+
Raphael Vullriede. (markt)
757+
</fix>
750758
</changelog>
751759
</subsection>
752760
<subsection name="Other">

0 commit comments

Comments
 (0)