Skip to content

Commit

Permalink
Additional test case + ability to change pool size
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasSQL committed May 7, 2017
1 parent baf5544 commit 8f50fb7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5597,7 +5597,14 @@ public boolean isStatementPoolingEnabled() {
* @value The new cache size.
*/
public void setStatementPoolingCacheSize(int value) {
this.statementPoolingCacheSize = value;
if (value != this.statementPoolingCacheSize) {
value = Math.max(0, value);
this.statementPoolingCacheSize = value;

if (null != this.preparedStatementCache) {
this.preparedStatementCache.setCapacity(value);
}
}
}

/** Get prepared statement cache entry if exists */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void testStatementPoolingEviction() throws SQLException {
String query = String.format("/*statementpoolingevictiontest_%s*/SELECT * FROM sys.tables; -- ", lookupUniqueifier);

// Add new statements to fill up the statement pool.
for(int i = 0; i < cacheSize; ++i) {
for (int i = 0; i < cacheSize; ++i) {
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement)con.prepareStatement(query + new Integer(i).toString())) {
pstmt.execute(); // sp_executesql
pstmt.execute(); // sp_prepexec, actual handle created and cached.
Expand All @@ -229,7 +229,7 @@ public void testStatementPoolingEviction() throws SQLException {
// Add new statements to fill up the statement discard action queue
// (new statement pushes existing statement from pool into discard
// action queue).
for(int i = cacheSize; i < cacheSize + 5; ++i) {
for (int i = cacheSize; i < cacheSize + 5; ++i) {
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement)con.prepareStatement(query + new Integer(i).toString())) {
pstmt.execute(); // sp_executesql
pstmt.execute(); // sp_prepexec, actual handle created and cached.
Expand All @@ -242,7 +242,7 @@ public void testStatementPoolingEviction() throws SQLException {
}

// If we use it, now discard queue should be "full".
if(0 == testNo)
if (0 == testNo)
assertSame(discardedStatementCount, con.getDiscardedServerPreparedStatementCount());
else
assertSame(0, con.getDiscardedServerPreparedStatementCount());
Expand All @@ -257,6 +257,28 @@ public void testStatementPoolingEviction() throws SQLException {

// Discard queue should now be empty.
assertSame(0, con.getDiscardedServerPreparedStatementCount());

// Set statement pool size to 0 and verify statements get discarded.
int statementsInCache = con.getStatementPoolingCacheEntryCount();
con.setStatementPoolingCacheSize(0);
assertSame(0, con.getStatementPoolingCacheEntryCount());

if(0 == testNo)
// Verify statements moved over to discard action queue.
assertSame(statementsInCache, con.getDiscardedServerPreparedStatementCount());

// Run discard actions (otherwise run on pstmt.close)
con.closeDiscardedServerPreparedStatements();

assertSame(0, con.getDiscardedServerPreparedStatementCount());

// Verify new statement does not go into cache (since cache is now off)
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement)con.prepareStatement(query)) {
pstmt.execute(); // sp_executesql
pstmt.execute(); // sp_prepexec, actual handle created and cached.

assertSame(0, con.getStatementPoolingCacheEntryCount());
}
}
}
}
Expand Down

1 comment on commit 8f50fb7

@davidfrankson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious how a size of 10 was picked? What is the expected memory usage for 10 cached statements?

Please sign in to comment.