Skip to content

Commit 48a6fe6

Browse files
committed
Clear parameters/batch/warnings, when statement is returned to cache
1 parent 1edd014 commit 48a6fe6

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

ebean-datasource/src/main/java/io/ebean/datasource/pool/PstmtCache.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ boolean returnStatement(ExtendedPreparedStatement stmt) {
6767
if (alreadyInCache != null) {
6868
return false;
6969
}
70+
try {
71+
// before putting a statement back to the cache, we will clear the parameters, batch and warnings
72+
stmt.clearParameters();
73+
stmt.clearBatch();
74+
stmt.clearWarnings();
75+
} catch (SQLException e) {
76+
Log.error("Error clearing PreparedStatement", e);
77+
return false;
78+
}
7079
// add the returning prepared statement to the cache.
7180
// Note that the LRUCache will automatically close fully old unused
7281
// statements when the cache has hit its maximum size.

ebean-datasource/src/test/java/io/ebean/datasource/test/FactoryTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import io.ebean.datasource.DataSourceConfig;
44
import io.ebean.datasource.DataSourceFactory;
55
import io.ebean.datasource.DataSourcePool;
6+
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
78

89
import java.sql.Connection;
910
import java.sql.PreparedStatement;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
1013

1114
public class FactoryTest {
1215

@@ -49,4 +52,39 @@ public void dataSourceFactory_get_createPool() throws Exception {
4952
}
5053

5154
}
55+
56+
57+
@Test
58+
public void testPreparedStatement() throws Exception {
59+
60+
DataSourceConfig config = new DataSourceConfig();
61+
config.setDriver("org.h2.Driver");
62+
config.setUrl("jdbc:h2:mem:tests");
63+
config.setUsername("sa");
64+
config.setPassword("");
65+
66+
DataSourcePool pool = DataSourceFactory.create("test", config);
67+
String sql = "select * from information_schema.settings where setting_name != ?";
68+
try (Connection connection = pool.getConnection()) {
69+
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
70+
stmt.executeQuery();
71+
Assertions.fail("Not expected");
72+
} catch (SQLException e) {
73+
// expected: Parameter "#1" not set
74+
}
75+
76+
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
77+
stmt.setString(1, "foo");
78+
ResultSet rs = stmt.executeQuery();
79+
rs.close();
80+
}
81+
82+
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
83+
stmt.executeQuery();
84+
Assertions.fail("Not expected");
85+
} catch (SQLException e) {
86+
// expected: Parameter "#1" not set
87+
}
88+
}
89+
}
5290
}

0 commit comments

Comments
 (0)