Skip to content

Commit

Permalink
Changed connection property name to cacheBulkCopyMetadata; Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyc committed Jul 5, 2024
1 parent e625a69 commit 77f8f78
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,17 @@ CallableStatement prepareCall(String sql, int nType, int nConcur, int nHold,
boolean getUseBulkCopyForBatchInsert();

/**
* Returns value of 'enableBulkCopyCache' from Connection String.
* Returns value of 'cacheBulkCopyMetadata' from Connection String.
*
* @param enableBulkCopyCache
* @param cacheBulkCopyMetadata
* indicates whether the driver should use connection level caching of metadata for bulk copy
*/
void setEnableBulkCopyCache(boolean enableBulkCopyCache);
void setcacheBulkCopyMetadata(boolean cacheBulkCopyMetadata);

/**
* Sets the value for 'enableBulkCopyCache' property
* Sets the value for 'cacheBulkCopyMetadata' property
*
* @return enableBulkCopyCache boolean value
* @return cacheBulkCopyMetadata boolean value
*/
boolean getEnableBulkCopyCache();
boolean getcacheBulkCopyMetadata();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1352,17 +1352,17 @@ public interface ISQLServerDataSource extends javax.sql.CommonDataSource {
boolean getCalcBigDecimalPrecision();

/**
* Returns value of 'enableBulkCopyCache' from Connection String.
* Returns value of 'cacheBulkCopyMetadata' from Connection String.
*
* @param enableBulkCopyCache
* @param cacheBulkCopyMetadata
* indicates whether the driver should use connection level caching of metadata for bulk copy
*/
void setEnableBulkCopyCache(boolean enableBulkCopyCache);
void setcacheBulkCopyMetadata(boolean cacheBulkCopyMetadata);

/**
* Sets the value for 'enableBulkCopyCache' property
* Sets the value for 'cacheBulkCopyMetadata' property
*
* @return enableBulkCopyCache boolean value
* @return cacheBulkCopyMetadata boolean value
*/
boolean getEnableBulkCopyCache();
boolean getcacheBulkCopyMetadata();
}
17 changes: 14 additions & 3 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -1732,15 +1732,15 @@ private void getDestinationMetadata() throws SQLServerException {
String escapedDestinationTableName = Util.escapeSingleQuotes(destinationTableName);
String key = null;

if (connection.getEnableBulkCopyCache()) {
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);
}

if (null == destColumnMetadata || destColumnMetadata.isEmpty()) {
if (connection.getEnableBulkCopyCache()) {
if (connection.getcacheBulkCopyMetadata()) {
DESTINATION_COL_METADATA_LOCK.lock();
destColumnMetadata = BULK_COPY_OPERATION_CACHE.get(key);

Expand All @@ -1753,7 +1753,7 @@ private void getDestinationMetadata() throws SQLServerException {
// 2. is_computed
// 3. encryption_type
//
// Using this caching method, 'enableBulkCopyCache', may have unintended consequences if the
// Using this caching method, 'cacheBulkCopyMetadata', may have unintended consequences if the
// table changes somehow between inserts. For example, if the collation_name changes, the
// 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
Expand All @@ -1763,8 +1763,19 @@ private void getDestinationMetadata() throws SQLServerException {
DESTINATION_COL_METADATA_LOCK.unlock();
}
}

if (loggerExternal.isLoggable(Level.FINER)) {
loggerExternal.finer(this.toString() + " Acquiring existing destination column metadata " +
"from cache for bulk copy");
}

} else {
setDestinationColumnMetadata(escapedDestinationTableName);

if (loggerExternal.isLoggable(Level.FINER)) {
loggerExternal.finer(this.toString() + " cacheBulkCopyMetadata=false - Querying server " +
"for destination column metadata");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
private Boolean enablePrepareOnFirstPreparedStatementCall = null;

/** Used for toggling bulk copy caching */
private Boolean enableBulkCopyCache = null;
private Boolean cacheBulkCopyMetadata = null;

/** Used for toggling use of sp_prepare */
private String prepareMethod = null;
Expand Down Expand Up @@ -3052,7 +3052,7 @@ else if (0 == requestedPacketSize)
sPropKey = SQLServerDriverBooleanProperty.ENABLE_BULK_COPY_CACHE.toString();
sPropValue = activeConnectionProperties.getProperty(sPropKey);
if (null != sPropValue) {
setEnableBulkCopyCache(isBooleanPropertyOn(sPropKey, sPropValue));
setcacheBulkCopyMetadata(isBooleanPropertyOn(sPropKey, sPropValue));
}

sPropKey = SQLServerDriverStringProperty.SSL_PROTOCOL.toString();
Expand Down Expand Up @@ -8062,17 +8062,17 @@ public void setEnablePrepareOnFirstPreparedStatementCall(boolean value) {
}

@Override
public boolean getEnableBulkCopyCache() {
if (null == this.enableBulkCopyCache) {
public boolean getcacheBulkCopyMetadata() {
if (null == this.cacheBulkCopyMetadata) {
return false;
}

return this.enableBulkCopyCache;
return this.cacheBulkCopyMetadata;
}

@Override
public void setEnableBulkCopyCache(boolean value) {
this.enableBulkCopyCache = value;
public void setcacheBulkCopyMetadata(boolean value) {
this.cacheBulkCopyMetadata = value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,13 @@ public void setEnablePrepareOnFirstPreparedStatementCall(boolean value) {
}

@Override
public boolean getEnableBulkCopyCache() {
return wrappedConnection.getEnableBulkCopyCache();
public boolean getcacheBulkCopyMetadata() {
return wrappedConnection.getcacheBulkCopyMetadata();
}

@Override
public void setEnableBulkCopyCache(boolean value) {
wrappedConnection.setEnableBulkCopyCache(value);
public void setcacheBulkCopyMetadata(boolean value) {
wrappedConnection.setcacheBulkCopyMetadata(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,14 @@ public boolean getEnablePrepareOnFirstPreparedStatementCall() {
}

@Override
public void setEnableBulkCopyCache(boolean enableBulkCopyCache) {
public void setcacheBulkCopyMetadata(boolean cacheBulkCopyMetadata) {
setBooleanProperty(connectionProps,
SQLServerDriverBooleanProperty.ENABLE_BULK_COPY_CACHE.toString(),
enableBulkCopyCache);
cacheBulkCopyMetadata);
}

@Override
public boolean getEnableBulkCopyCache() {
public boolean getcacheBulkCopyMetadata() {
boolean defaultValue = SQLServerDriverBooleanProperty.ENABLE_BULK_COPY_CACHE
.getDefaultValue();
return getBooleanProperty(connectionProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ enum SQLServerDriverBooleanProperty {
XOPEN_STATES("xopenStates", false),
FIPS("fips", false),
ENABLE_PREPARE_ON_FIRST_PREPARED_STATEMENT("enablePrepareOnFirstPreparedStatementCall", SQLServerConnection.DEFAULT_ENABLE_PREPARE_ON_FIRST_PREPARED_STATEMENT_CALL),
ENABLE_BULK_COPY_CACHE("enableBulkCopyCache", false),
ENABLE_BULK_COPY_CACHE("cacheBulkCopyMetadata", false),
USE_BULK_COPY_FOR_BATCH_INSERT("useBulkCopyForBatchInsert", false),
USE_FMT_ONLY("useFmtOnly", false),
SEND_TEMPORAL_DATATYPES_AS_STRING_FOR_BULK_COPY("sendTemporalDataTypesAsStringForBulkCopy", true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected Object[][] getContents() {
{"R_socketTimeoutPropertyDescription", "The number of milliseconds to wait before the java.net.SocketTimeoutException is raised."},
{"R_serverPreparedStatementDiscardThresholdPropertyDescription", "The threshold for when to close discarded prepare statements on the server (calling a batch of sp_unprepares). A value of 1 or less will cause sp_unprepare to be called immediately on PreparedStatment close."},
{"R_enablePrepareOnFirstPreparedStatementCallPropertyDescription", "This setting specifies whether a prepared statement is prepared (sp_prepexec) on first use (property=true) or on second after first calling sp_executesql (property=false)."},
{"R_enableBulkCopyCachePropertyDescription", "This setting specifies whether the driver caches the metadata used for bulk copy for batch inserts."},
{"R_cacheBulkCopyMetadataPropertyDescription", "This setting specifies whether the driver caches the metadata used for bulk copy for batch inserts."},
{"R_statementPoolingCacheSizePropertyDescription", "This setting specifies the size of the prepared statement cache for a connection. A value less than 1 means no cache."},
{"R_gsscredentialPropertyDescription", "Impersonated GSS Credential to access SQL Server."},
{"R_msiClientIdPropertyDescription", "Client Id of User Assigned Managed Identity to be used for generating access token for Azure AD MSI Authentication"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ private List<String> getVerifiedMethodNames() {
verifiedMethodNames.add("getServerMessageHandler");
verifiedMethodNames.add("setServerMessageHandler");
verifiedMethodNames.add("getCalcBigDecimalScale");
verifiedMethodNames.add("setEnableBulkCopyCache");
verifiedMethodNames.add("getEnableBulkCopyCache");
verifiedMethodNames.add("setcacheBulkCopyMetadata");
verifiedMethodNames.add("getcacheBulkCopyMetadata");
verifiedMethodNames.add("setCalcBigDecimalScale");
verifiedMethodNames.add("getUseFlexibleCallableStatements");
verifiedMethodNames.add("setUseFlexibleCallableStatements");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void testSqlServerBulkCopyCachingConnectionLevel() throws Exception {
long ms = 1578743412000L;

try (SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(
connectionString + ";useBulkCopyForBatchInsert=true;enableBulkCopyCache=true;sendTemporalDataTypesAsStringForBulkCopy=false;");
connectionString + ";useBulkCopyForBatchInsert=true;cacheBulkCopyMetadata=true;sendTemporalDataTypesAsStringForBulkCopy=false;");
Statement stmt = con.createStatement()) {

// Needs to be on a JDK version greater than 8
Expand Down

0 comments on commit 77f8f78

Please sign in to comment.