Skip to content

Commit

Permalink
clear pstmt cache (#2361)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Apr 2, 2024
1 parent 30f2ae0 commit 7c78c48
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,11 @@ final Connection getConnection() {
final void resetPooledConnection() {
tdsChannel.resetPooledConnection();
initResettableValues();

// reset prepared statement handle cache
if (null != preparedStatementHandleCache) {
preparedStatementHandleCache.clear();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.ISQLServerConnection;
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
Expand All @@ -42,7 +41,6 @@
* Tests pooled connection
*
*/
@RunWith(JUnitPlatform.class)
public class PoolingTest extends AbstractTest {
static String tempTableName = RandomUtil.getIdentifier("#poolingtest");
static String tableName = RandomUtil.getIdentifier("PoolingTestTable");
Expand Down Expand Up @@ -217,6 +215,69 @@ public void testApacheDBCP() throws SQLException {
}
}

/**
* test that prepared statement cache is cleared when disableStatementPooling is not set
*/
@Test
public void testDisableStatementPooling() throws SQLException {
SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();
ds.setURL(connectionString + ";disableStatementPooling=false;statementPoolingCacheSize=20");
PooledConnection pConn = ds.getPooledConnection();

// create test table
try (Connection conn = pConn.getConnection(); Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);

stmt.execute(
"create table " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (c1 int, c2 varchar(20))");
}

try {
for (int i = 0; i < 5; i++) {
try (Connection conn = pConn.getConnection();) {
conn.setAutoCommit(false);

try (Statement stmt = conn.createStatement(); PreparedStatement pstmt = conn.prepareStatement(
"insert into " + AbstractSQLGenerator.escapeIdentifier(tableName) + " values (?, ?)")) {

for (int j = 0; j < 3; j++) {
pstmt.setInt(1, j);
pstmt.setString(2, "test" + j);
pstmt.addBatch();
}

pstmt.executeBatch();
conn.commit();
}
}
}

try (Connection conn = pConn.getConnection(); Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ " order by c1 desc")) {

int i = 1;
while (rs.next()) {
if (i <= 5) {
assertEquals(2, rs.getInt(1));
assertEquals("test2", rs.getString(2));
} else if (i > 5 && i <= 10) {
assertEquals(1, rs.getInt(1));
assertEquals("test1", rs.getString(2));
} else if (i > 10 && i <= 15) {
assertEquals(0, rs.getInt(1));
assertEquals("test0", rs.getString(2));
}
i++;
}
}
} finally {
if (null != pConn) {
pConn.close();
}
}
}

/**
* setup connection, get connection from pool, and test threads
*
Expand Down

0 comments on commit 7c78c48

Please sign in to comment.