|
23 | 23 | import org.junit.jupiter.api.BeforeAll; |
24 | 24 | import org.junit.jupiter.api.Tag; |
25 | 25 | import org.junit.jupiter.api.Test; |
26 | | -import org.junit.platform.runner.JUnitPlatform; |
27 | | -import org.junit.runner.RunWith; |
28 | 26 |
|
29 | 27 | import com.microsoft.sqlserver.jdbc.ISQLServerConnection; |
30 | 28 | import com.microsoft.sqlserver.jdbc.RandomUtil; |
| 29 | +import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource; |
31 | 30 | import com.microsoft.sqlserver.jdbc.SQLServerXADataSource; |
32 | 31 | import com.microsoft.sqlserver.jdbc.TestResource; |
33 | 32 | import com.microsoft.sqlserver.jdbc.TestUtils; |
|
42 | 41 | * Tests pooled connection |
43 | 42 | * |
44 | 43 | */ |
45 | | -@RunWith(JUnitPlatform.class) |
46 | 44 | public class PoolingTest extends AbstractTest { |
47 | 45 | static String tempTableName = RandomUtil.getIdentifier("#poolingtest"); |
48 | 46 | static String tableName = RandomUtil.getIdentifier("PoolingTestTable"); |
@@ -217,6 +215,69 @@ public void testApacheDBCP() throws SQLException { |
217 | 215 | } |
218 | 216 | } |
219 | 217 |
|
| 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 | + |
220 | 281 | /** |
221 | 282 | * setup connection, get connection from pool, and test threads |
222 | 283 | * |
|
0 commit comments