Skip to content

Commit 7e795d4

Browse files
authored
Changed the scope of BULK_COPY_OPERATION_CACHE to connection. (#2594)
1 parent c01814b commit 7e795d4

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package com.microsoft.sqlserver.jdbc;
77

8-
import static com.microsoft.sqlserver.jdbc.SQLServerConnection.BULK_COPY_OPERATION_CACHE;
98
import static com.microsoft.sqlserver.jdbc.Util.getHashedSecret;
109
import static java.nio.charset.StandardCharsets.UTF_16LE;
1110
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -1731,19 +1730,19 @@ private void getDestinationMetadata() throws SQLServerException {
17311730

17321731
String escapedDestinationTableName = Util.escapeSingleQuotes(destinationTableName);
17331732
String key = null;
1734-
1733+
HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> bulkCopyOperationCache = connection.getBulkCopyOperationCache();
17351734
if (connection.getcacheBulkCopyMetadata()) {
17361735
String databaseName = connection.activeConnectionProperties
17371736
.getProperty(SQLServerDriverStringProperty.DATABASE_NAME.toString());
17381737
key = getHashedSecret(new String[] {escapedDestinationTableName, databaseName});
1739-
destColumnMetadata = BULK_COPY_OPERATION_CACHE.get(key);
1738+
destColumnMetadata = bulkCopyOperationCache.get(key);
17401739
}
17411740

17421741
if (null == destColumnMetadata || destColumnMetadata.isEmpty()) {
17431742
if (connection.getcacheBulkCopyMetadata()) {
17441743
DESTINATION_COL_METADATA_LOCK.lock();
17451744
try {
1746-
destColumnMetadata = BULK_COPY_OPERATION_CACHE.get(key);
1745+
destColumnMetadata = bulkCopyOperationCache.get(key);
17471746

17481747
if (null == destColumnMetadata || destColumnMetadata.isEmpty()) {
17491748
setDestinationColumnMetadata(escapedDestinationTableName);
@@ -1758,7 +1757,7 @@ private void getDestinationMetadata() throws SQLServerException {
17581757
// driver will not be aware of this and the inserted data will likely be corrupted. In such
17591758
// scenario, we can't detect this without making an additional metadata query, which would
17601759
// defeat the purpose of caching.
1761-
BULK_COPY_OPERATION_CACHE.put(key, destColumnMetadata);
1760+
bulkCopyOperationCache.put(key, destColumnMetadata);
17621761
}
17631762
} finally {
17641763
DESTINATION_COL_METADATA_LOCK.unlock();

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ public static void clearUserTokenCache() {
17631763
/** transaction descriptor */
17641764
private byte[] transactionDescriptor = new byte[8];
17651765

1766-
static final HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> BULK_COPY_OPERATION_CACHE = new HashMap<>();
1766+
final HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> bulkCopyOperationCache = new HashMap<>();
17671767

17681768
/**
17691769
* 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) {
17821782
this.state = state;
17831783
}
17841784

1785+
final HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> getBulkCopyOperationCache() {
1786+
return bulkCopyOperationCache;
1787+
}
17851788
/**
17861789
* This function actually represents whether a database session is not open. The session is not available before the
17871790
* session is established and after the session is closed.
@@ -2040,6 +2043,11 @@ final void resetPooledConnection() {
20402043
if (null != preparedStatementHandleCache) {
20412044
preparedStatementHandleCache.clear();
20422045
}
2046+
2047+
//clear bulk copy operation cache for this connection
2048+
if (null != bulkCopyOperationCache) {
2049+
bulkCopyOperationCache.clear();
2050+
}
20432051
}
20442052

20452053
/**

src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ public void testSqlServerBulkCopyCachingConnectionLevel() throws Exception {
170170

171171
if (con.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerConnection43")) {
172172
bulkcopyMetadataCacheField = con.getClass().getSuperclass()
173-
.getDeclaredField("BULK_COPY_OPERATION_CACHE");
173+
.getDeclaredField("bulkCopyOperationCache");
174174
} else {
175-
bulkcopyMetadataCacheField = con.getClass().getDeclaredField("BULK_COPY_OPERATION_CACHE");
175+
bulkcopyMetadataCacheField = con.getClass().getDeclaredField("bulkCopyOperationCache");
176176
}
177177

178178
bulkcopyMetadataCacheField.setAccessible(true);
@@ -233,9 +233,9 @@ public void testSqlServerBulkCopyCachingConnectionLevelMultiThreaded() throws Ex
233233

234234
if (con.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerConnection43")) {
235235
bulkcopyMetadataCacheField = con.getClass().getSuperclass()
236-
.getDeclaredField("BULK_COPY_OPERATION_CACHE");
236+
.getDeclaredField("bulkCopyOperationCache");
237237
} else {
238-
bulkcopyMetadataCacheField = con.getClass().getDeclaredField("BULK_COPY_OPERATION_CACHE");
238+
bulkcopyMetadataCacheField = con.getClass().getDeclaredField("bulkCopyOperationCache");
239239
}
240240

241241
bulkcopyMetadataCacheField.setAccessible(true);

0 commit comments

Comments
 (0)