Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the scope of BULK_COPY_OPERATION_CACHE to connection. #2594

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1731,19 +1730,19 @@ private void getDestinationMetadata() throws SQLServerException {

String escapedDestinationTableName = Util.escapeSingleQuotes(destinationTableName);
String key = null;

HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> 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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ public static void clearUserTokenCache() {
/** transaction descriptor */
private byte[] transactionDescriptor = new byte[8];

static final HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> BULK_COPY_OPERATION_CACHE = new HashMap<>();
final HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> 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
Expand All @@ -1782,6 +1782,9 @@ private void setState(State state) {
this.state = state;
}

final HashMap<String, Map<Integer, SQLServerBulkCopy.BulkColumnMetaData>> 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.
Expand Down Expand Up @@ -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();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading