Skip to content

Commit 6884680

Browse files
Backport #2361 (#2421)
1 parent c4edecd commit 6884680

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,11 @@ final Connection getConnection() {
18071807
final void resetPooledConnection() {
18081808
tdsChannel.resetPooledConnection();
18091809
initResettableValues();
1810+
1811+
// reset prepared statement handle cache
1812+
if (null != preparedStatementHandleCache) {
1813+
preparedStatementHandleCache.clear();
1814+
}
18101815
}
18111816

18121817
/**

src/test/java/com/microsoft/sqlserver/jdbc/connection/PoolingTest.java

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
import org.junit.jupiter.api.BeforeAll;
2424
import org.junit.jupiter.api.Tag;
2525
import org.junit.jupiter.api.Test;
26-
import org.junit.platform.runner.JUnitPlatform;
27-
import org.junit.runner.RunWith;
2826

2927
import com.microsoft.sqlserver.jdbc.ISQLServerConnection;
3028
import com.microsoft.sqlserver.jdbc.RandomUtil;
29+
import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource;
3130
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
3231
import com.microsoft.sqlserver.jdbc.TestResource;
3332
import com.microsoft.sqlserver.jdbc.TestUtils;
@@ -42,7 +41,6 @@
4241
* Tests pooled connection
4342
*
4443
*/
45-
@RunWith(JUnitPlatform.class)
4644
public class PoolingTest extends AbstractTest {
4745
static String tempTableName = RandomUtil.getIdentifier("#poolingtest");
4846
static String tableName = RandomUtil.getIdentifier("PoolingTestTable");
@@ -217,6 +215,69 @@ public void testApacheDBCP() throws SQLException {
217215
}
218216
}
219217

218+
/**
219+
* test that prepared statement cache is cleared when disableStatementPooling is not set
220+
*/
221+
@Test
222+
public void testDisableStatementPooling() throws SQLException {
223+
SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();
224+
ds.setURL(connectionString + ";disableStatementPooling=false;statementPoolingCacheSize=20");
225+
PooledConnection pConn = ds.getPooledConnection();
226+
227+
// create test table
228+
try (Connection conn = pConn.getConnection(); Statement stmt = conn.createStatement()) {
229+
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
230+
231+
stmt.execute(
232+
"create table " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (c1 int, c2 varchar(20))");
233+
}
234+
235+
try {
236+
for (int i = 0; i < 5; i++) {
237+
try (Connection conn = pConn.getConnection();) {
238+
conn.setAutoCommit(false);
239+
240+
try (Statement stmt = conn.createStatement(); PreparedStatement pstmt = conn.prepareStatement(
241+
"insert into " + AbstractSQLGenerator.escapeIdentifier(tableName) + " values (?, ?)")) {
242+
243+
for (int j = 0; j < 3; j++) {
244+
pstmt.setInt(1, j);
245+
pstmt.setString(2, "test" + j);
246+
pstmt.addBatch();
247+
}
248+
249+
pstmt.executeBatch();
250+
conn.commit();
251+
}
252+
}
253+
}
254+
255+
try (Connection conn = pConn.getConnection(); Statement stmt = conn.createStatement();
256+
ResultSet rs = stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(tableName)
257+
+ " order by c1 desc")) {
258+
259+
int i = 1;
260+
while (rs.next()) {
261+
if (i <= 5) {
262+
assertEquals(2, rs.getInt(1));
263+
assertEquals("test2", rs.getString(2));
264+
} else if (i > 5 && i <= 10) {
265+
assertEquals(1, rs.getInt(1));
266+
assertEquals("test1", rs.getString(2));
267+
} else if (i > 10 && i <= 15) {
268+
assertEquals(0, rs.getInt(1));
269+
assertEquals("test0", rs.getString(2));
270+
}
271+
i++;
272+
}
273+
}
274+
} finally {
275+
if (null != pConn) {
276+
pConn.close();
277+
}
278+
}
279+
}
280+
220281
/**
221282
* setup connection, get connection from pool, and test threads
222283
*

0 commit comments

Comments
 (0)