Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 21 additions & 13 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@

// Note all the public functions in this class also need to be defined in SQLServerConnectionPoolProxy.
public class SQLServerConnection implements ISQLServerConnection {
boolean contextIsAlreadyChanged = false;
boolean contextChanged = false;

long timerExpire;
boolean attemptRefreshTokenLocked = false;
Expand Down Expand Up @@ -277,10 +275,10 @@ static ParsedSQLCacheItem parseAndCacheSQL(Sha1HashKey key, String sql) throws
}

/** Size of the prepared statement handle cache */
private int statementPoolingCacheSize = 10;
private int statementPoolingCacheSize = 0;

/** Default size for prepared statement caches */
static final int DEFAULT_STATEMENT_POOLING_CACHE_SIZE = 10;
static final int DEFAULT_STATEMENT_POOLING_CACHE_SIZE = 0;
/** Cache of prepared statement handles */
private ConcurrentLinkedHashMap<Sha1HashKey, PreparedStatementHandle> preparedStatementHandleCache;
/** Cache of prepared statement parameter metadata */
Expand Down Expand Up @@ -3080,8 +3078,6 @@ final void poolCloseEventNotify() throws SQLServerException {
checkClosed();
if (catalog != null) {
connectionCommand("use " + Util.escapeSQLId(catalog), "setCatalog");
contextIsAlreadyChanged = true;
contextChanged = true;
sCatalog = catalog;
}
loggerExternal.exiting(getClassNameLogging(), "setCatalog");
Expand Down Expand Up @@ -5699,7 +5695,7 @@ final void unprepareUnreferencedPreparedStatementHandles(boolean force) {
*/
public int getStatementPoolingCacheSize() {
return statementPoolingCacheSize;
}
}

/**
* Returns the current number of pooled prepared statement handles.
Expand All @@ -5726,6 +5722,24 @@ public boolean isStatementPoolingEnabled() {
*
*/
public void setStatementPoolingCacheSize(int value) {
// Caching turned on?
String sPropKey = SQLServerDriverBooleanProperty.DISABLE_STATEMENT_POOLING.toString();
String sPropValue = activeConnectionProperties.getProperty(sPropKey);

// If DISABLE_STATEMENT_POOLING property is true, we can't allow cache size and will disable caching.
if ( null != sPropValue && sPropValue.equalsIgnoreCase("true"))
return;

if (0 < value) {
preparedStatementHandleCache = new Builder<Sha1HashKey, PreparedStatementHandle>()
.maximumWeightedCapacity(getStatementPoolingCacheSize())
.listener(new PreparedStatementCacheEvictionListener())
.build();

parameterMetadataCache = new Builder<Sha1HashKey, SQLServerParameterMetaData>()
.maximumWeightedCapacity(getStatementPoolingCacheSize())
.build();
}
if (value != this.statementPoolingCacheSize) {
value = Math.max(0, value);
statementPoolingCacheSize = value;
Expand Down Expand Up @@ -5788,12 +5802,6 @@ final void evictCachedPreparedStatementHandle(PreparedStatementHandle handle) {
preparedStatementHandleCache.remove(handle.getKey());
}

final void clearCachedPreparedStatementHandle() {
if (null != preparedStatementHandleCache) {
preparedStatementHandleCache.clear();
}
}

// Handle closing handles when removed from cache.
final class PreparedStatementCacheEvictionListener implements EvictionListener<Sha1HashKey, PreparedStatementHandle> {
public void onEviction(Sha1HashKey key, PreparedStatementHandle handle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ public void run() {
if (wrappedConnection.getConnectionLogger().isLoggable(Level.FINER))
wrappedConnection.getConnectionLogger().finer(toString() + " Connection proxy closed ");

// clear cached prepared statement handle on this connection
wrappedConnection.clearCachedPreparedStatementHandle();
wrappedConnection.poolCloseEventNotify();
wrappedConnection = null;
}
Expand Down
Loading