Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,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