diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 23139cbe7..b144ea08f 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -122,7 +122,7 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial private Boolean isAzureDW = null; - static class Sha1HashKey implements java.io.Serializable { + static class SQLServerHashKey implements java.io.Serializable { /** * Always refresh SerialVersionUID when prompted @@ -130,29 +130,29 @@ static class Sha1HashKey implements java.io.Serializable { private static final long serialVersionUID = 166788428640603097L; private byte[] bytes; - Sha1HashKey(String sql, + SQLServerHashKey(String sql, String parametersDefinition) { - this(String.format("%s%s", sql, parametersDefinition)); + this(sql + parametersDefinition); } - Sha1HashKey(String s) { - bytes = getSha1Digest().digest(s.getBytes()); + SQLServerHashKey(String s) { + bytes = getSha256Digest().digest(s.getBytes()); } public boolean equals(Object obj) { - if (!(obj instanceof Sha1HashKey)) + if (!(obj instanceof SQLServerHashKey)) return false; - return java.util.Arrays.equals(bytes, ((Sha1HashKey) obj).bytes); + return java.util.Arrays.equals(bytes, ((SQLServerHashKey) obj).bytes); } public int hashCode() { return java.util.Arrays.hashCode(bytes); } - private java.security.MessageDigest getSha1Digest() { + private java.security.MessageDigest getSha256Digest() { try { - return java.security.MessageDigest.getInstance("SHA-1"); + return java.security.MessageDigest.getInstance("SHA-256"); } catch (final java.security.NoSuchAlgorithmException e) { // This is not theoretically possible, but we're forced to catch it anyway @@ -170,9 +170,9 @@ class PreparedStatementHandle { private boolean isDirectSql; private volatile boolean evictedFromCache; private volatile boolean explicitlyDiscarded; - private Sha1HashKey key; + private SQLServerHashKey key; - PreparedStatementHandle(Sha1HashKey key, + PreparedStatementHandle(SQLServerHashKey key, int handle, boolean isDirectSql, boolean isEvictedFromCache) { @@ -211,7 +211,7 @@ int getHandle() { } /** Get the cache key. */ - Sha1HashKey getKey() { + SQLServerHashKey getKey() { return key; } @@ -258,19 +258,19 @@ void removeReference() { static final private int PARSED_SQL_CACHE_SIZE = 100; /** Cache of parsed SQL meta data */ - static private ConcurrentLinkedHashMap parsedSQLCache; + static private ConcurrentLinkedHashMap parsedSQLCache; static { - parsedSQLCache = new Builder().maximumWeightedCapacity(PARSED_SQL_CACHE_SIZE).build(); + parsedSQLCache = new Builder().maximumWeightedCapacity(PARSED_SQL_CACHE_SIZE).build(); } /** Get prepared statement cache entry if exists, if not parse and create a new one */ - static ParsedSQLCacheItem getCachedParsedSQL(Sha1HashKey key) { + static ParsedSQLCacheItem getCachedParsedSQL(SQLServerHashKey key) { return parsedSQLCache.get(key); } /** Parse and create a information about parsed SQL text */ - static ParsedSQLCacheItem parseAndCacheSQL(Sha1HashKey key, + static ParsedSQLCacheItem parseAndCacheSQL(SQLServerHashKey key, String sql) throws SQLServerException { JDBCSyntaxTranslator translator = new JDBCSyntaxTranslator(); @@ -291,9 +291,9 @@ static ParsedSQLCacheItem parseAndCacheSQL(Sha1HashKey key, private int statementPoolingCacheSize = DEFAULT_STATEMENT_POOLING_CACHE_SIZE; /** Cache of prepared statement handles */ - private ConcurrentLinkedHashMap preparedStatementHandleCache; + private ConcurrentLinkedHashMap preparedStatementHandleCache; /** Cache of prepared statement parameter metadata */ - private ConcurrentLinkedHashMap parameterMetadataCache; + private ConcurrentLinkedHashMap parameterMetadataCache; /** * Checks whether statement pooling is enabled or disabled. The default is set to true; */ @@ -5797,15 +5797,15 @@ public void setStatementPoolingCacheSize(int value) { * @param value */ private void prepareCache() { - preparedStatementHandleCache = new Builder().maximumWeightedCapacity(getStatementPoolingCacheSize()) + preparedStatementHandleCache = new Builder().maximumWeightedCapacity(getStatementPoolingCacheSize()) .listener(new PreparedStatementCacheEvictionListener()).build(); - parameterMetadataCache = new Builder().maximumWeightedCapacity(getStatementPoolingCacheSize()) + parameterMetadataCache = new Builder().maximumWeightedCapacity(getStatementPoolingCacheSize()) .build(); } /** Get a parameter metadata cache entry if statement pooling is enabled */ - final SQLServerParameterMetaData getCachedParameterMetadata(Sha1HashKey key) { + final SQLServerParameterMetaData getCachedParameterMetadata(SQLServerHashKey key) { if (!isStatementPoolingEnabled()) return null; @@ -5813,7 +5813,7 @@ final SQLServerParameterMetaData getCachedParameterMetadata(Sha1HashKey key) { } /** Register a parameter metadata cache entry if statement pooling is enabled */ - final void registerCachedParameterMetadata(Sha1HashKey key, + final void registerCachedParameterMetadata(SQLServerHashKey key, SQLServerParameterMetaData pmd) { if (!isStatementPoolingEnabled() || null == pmd) return; @@ -5822,7 +5822,7 @@ final void registerCachedParameterMetadata(Sha1HashKey key, } /** Get or create prepared statement handle cache entry if statement pooling is enabled */ - final PreparedStatementHandle getCachedPreparedStatementHandle(Sha1HashKey key) { + final PreparedStatementHandle getCachedPreparedStatementHandle(SQLServerHashKey key) { if (!isStatementPoolingEnabled()) return null; @@ -5830,7 +5830,7 @@ final PreparedStatementHandle getCachedPreparedStatementHandle(Sha1HashKey key) } /** Get or create prepared statement handle cache entry if statement pooling is enabled */ - final PreparedStatementHandle registerCachedPreparedStatementHandle(Sha1HashKey key, + final PreparedStatementHandle registerCachedPreparedStatementHandle(SQLServerHashKey key, int handle, boolean isDirectSql) { if (!isStatementPoolingEnabled() || null == key) @@ -5858,8 +5858,8 @@ final void evictCachedPreparedStatementHandle(PreparedStatementHandle handle) { } // Handle closing handles when removed from cache. - final class PreparedStatementCacheEvictionListener implements EvictionListener { - public void onEviction(Sha1HashKey key, + final class PreparedStatementCacheEvictionListener implements EvictionListener { + public void onEviction(SQLServerHashKey key, PreparedStatementHandle handle) { if (null != handle) { handle.setIsEvictedFromCache(true); // Mark as evicted from cache. diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index a7168b0f0..24bc1270e 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -33,7 +33,7 @@ import java.util.logging.Level; import com.microsoft.sqlserver.jdbc.SQLServerConnection.PreparedStatementHandle; -import com.microsoft.sqlserver.jdbc.SQLServerConnection.Sha1HashKey; +import com.microsoft.sqlserver.jdbc.SQLServerConnection.SQLServerHashKey; /** * SQLServerPreparedStatement provides JDBC prepared statement functionality. SQLServerPreparedStatement provides methods for the user to supply @@ -75,7 +75,7 @@ public class SQLServerPreparedStatement extends SQLServerStatement implements IS private PreparedStatementHandle cachedPreparedStatementHandle; /** Hash of user supplied SQL statement used for various cache lookups */ - private Sha1HashKey sqlTextCacheKey; + private SQLServerHashKey sqlTextCacheKey; /** * Array with parameter names generated in buildParamTypeDefinitions For mapping encryption information to parameters, as the second result set @@ -214,7 +214,7 @@ String getClassNameInternal() { stmtPoolable = true; // Create a cache key for this statement. - sqlTextCacheKey = new Sha1HashKey(sql); + sqlTextCacheKey = new SQLServerHashKey(sql); // Parse or fetch SQL metadata from cache. ParsedSQLCacheItem parsedSQL = getCachedParsedSQL(sqlTextCacheKey); @@ -631,7 +631,7 @@ boolean onRetValue(TDSReader tdsReader) throws SQLServerException { // Cache the reference to the newly created handle, NOT for cursorable handles. if (null == cachedPreparedStatementHandle && !isCursorable(executeMethod)) { cachedPreparedStatementHandle = connection.registerCachedPreparedStatementHandle( - new Sha1HashKey(preparedSQL, preparedTypeDefinitions), prepStmtHandle, executedSqlDirectly); + new SQLServerHashKey(preparedSQL, preparedTypeDefinitions), prepStmtHandle, executedSqlDirectly); } param.skipValue(tdsReader, true); @@ -978,7 +978,7 @@ private boolean reuseCachedHandle(boolean hasNewTypeDefinitions, // Check for new cache reference. if (null == cachedPreparedStatementHandle) { - PreparedStatementHandle cachedHandle = connection.getCachedPreparedStatementHandle(new Sha1HashKey(preparedSQL, preparedTypeDefinitions)); + PreparedStatementHandle cachedHandle = connection.getCachedPreparedStatementHandle(new SQLServerHashKey(preparedSQL, preparedTypeDefinitions)); // If handle was found then re-use, only if AE is not on and is not a batch query with new type definitions (We shouldn't reuse handle // if it is batch query and has new type definition, or if it is on, make sure encryptionMetadataIsRetrieved is retrieved. if (null != cachedHandle) { diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java index 2a643eb90..e0ed687be 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java @@ -28,7 +28,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.microsoft.sqlserver.jdbc.SQLServerConnection.Sha1HashKey; +import com.microsoft.sqlserver.jdbc.SQLServerConnection.SQLServerHashKey; /** * SQLServerStatment provides the basic implementation of JDBC statement functionality. It also provides a number of base class implementation methods @@ -752,7 +752,7 @@ final void processResponse(TDSReader tdsReader) throws SQLServerException { private String ensureSQLSyntax(String sql) throws SQLServerException { if (sql.indexOf(LEFT_CURLY_BRACKET) >= 0) { - Sha1HashKey cacheKey = new Sha1HashKey(sql); + SQLServerHashKey cacheKey = new SQLServerHashKey(sql); // Check for cached SQL metadata. ParsedSQLCacheItem cacheItem = getCachedParsedSQL(cacheKey);