From 677933240bca00632f0a837d6183f98ef86f81f3 Mon Sep 17 00:00:00 2001 From: machavan Date: Tue, 28 Jan 2025 11:39:08 +0530 Subject: [PATCH] Changed the scope of BULK_COPY_OPERATION_CACHE to connection. --- .../microsoft/sqlserver/jdbc/SQLServerBulkCopy.java | 9 ++++----- .../microsoft/sqlserver/jdbc/SQLServerConnection.java | 10 +++++++++- .../jdbc/unit/statement/BatchExecutionTest.java | 8 ++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java index 35f239608..7dcc1469a 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java @@ -5,7 +5,6 @@ package com.microsoft.sqlserver.jdbc; -import static com.microsoft.sqlserver.jdbc.SQLServerConnection.BULK_COPY_OPERATION_CACHE; import static com.microsoft.sqlserver.jdbc.Util.getHashedSecret; import static java.nio.charset.StandardCharsets.UTF_16LE; import static java.nio.charset.StandardCharsets.UTF_8; @@ -1731,19 +1730,19 @@ private void getDestinationMetadata() throws SQLServerException { String escapedDestinationTableName = Util.escapeSingleQuotes(destinationTableName); String key = null; - + HashMap> bulkCopyOperationCache = connection.getBulkCopyOperationCache(); if (connection.getcacheBulkCopyMetadata()) { String databaseName = connection.activeConnectionProperties .getProperty(SQLServerDriverStringProperty.DATABASE_NAME.toString()); key = getHashedSecret(new String[] {escapedDestinationTableName, databaseName}); - destColumnMetadata = BULK_COPY_OPERATION_CACHE.get(key); + destColumnMetadata = bulkCopyOperationCache.get(key); } if (null == destColumnMetadata || destColumnMetadata.isEmpty()) { if (connection.getcacheBulkCopyMetadata()) { DESTINATION_COL_METADATA_LOCK.lock(); try { - destColumnMetadata = BULK_COPY_OPERATION_CACHE.get(key); + destColumnMetadata = bulkCopyOperationCache.get(key); if (null == destColumnMetadata || destColumnMetadata.isEmpty()) { setDestinationColumnMetadata(escapedDestinationTableName); @@ -1758,7 +1757,7 @@ private void getDestinationMetadata() throws SQLServerException { // driver will not be aware of this and the inserted data will likely be corrupted. In such // scenario, we can't detect this without making an additional metadata query, which would // defeat the purpose of caching. - BULK_COPY_OPERATION_CACHE.put(key, destColumnMetadata); + bulkCopyOperationCache.put(key, destColumnMetadata); } } finally { DESTINATION_COL_METADATA_LOCK.unlock(); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index f5e6bc317..14d587d67 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -1763,7 +1763,7 @@ public static void clearUserTokenCache() { /** transaction descriptor */ private byte[] transactionDescriptor = new byte[8]; - static final HashMap> BULK_COPY_OPERATION_CACHE = new HashMap<>(); + final HashMap> bulkCopyOperationCache = new HashMap<>(); /** * Flag (Yukon and later) set to true whenever a transaction is rolled back..The flag's value is reset to false when @@ -1782,6 +1782,9 @@ private void setState(State state) { this.state = state; } + final HashMap> getBulkCopyOperationCache() { + return bulkCopyOperationCache; + } /** * This function actually represents whether a database session is not open. The session is not available before the * session is established and after the session is closed. @@ -2040,6 +2043,11 @@ final void resetPooledConnection() { if (null != preparedStatementHandleCache) { preparedStatementHandleCache.clear(); } + + //clear bulk copy operation cache for this connection + if (null != bulkCopyOperationCache) { + bulkCopyOperationCache.clear(); + } } /** diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java index ba3f6e70c..f271af8d2 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java @@ -170,9 +170,9 @@ public void testSqlServerBulkCopyCachingConnectionLevel() throws Exception { if (con.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerConnection43")) { bulkcopyMetadataCacheField = con.getClass().getSuperclass() - .getDeclaredField("BULK_COPY_OPERATION_CACHE"); + .getDeclaredField("bulkCopyOperationCache"); } else { - bulkcopyMetadataCacheField = con.getClass().getDeclaredField("BULK_COPY_OPERATION_CACHE"); + bulkcopyMetadataCacheField = con.getClass().getDeclaredField("bulkCopyOperationCache"); } bulkcopyMetadataCacheField.setAccessible(true); @@ -233,9 +233,9 @@ public void testSqlServerBulkCopyCachingConnectionLevelMultiThreaded() throws Ex if (con.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerConnection43")) { bulkcopyMetadataCacheField = con.getClass().getSuperclass() - .getDeclaredField("BULK_COPY_OPERATION_CACHE"); + .getDeclaredField("bulkCopyOperationCache"); } else { - bulkcopyMetadataCacheField = con.getClass().getDeclaredField("BULK_COPY_OPERATION_CACHE"); + bulkcopyMetadataCacheField = con.getClass().getDeclaredField("bulkCopyOperationCache"); } bulkcopyMetadataCacheField.setAccessible(true);