DATAMONGO-532 - Synchronize DB authentication.
In multithreaded environments Mongo database authentication can be triggered twice if two or more threads refer to the same db instance. This is now prevented by synchronizing calls to db.authenticate(…).
This commit is contained in:
@@ -109,11 +109,13 @@ public abstract class MongoDbUtils {
|
|||||||
String username = credentials.getUsername();
|
String username = credentials.getUsername();
|
||||||
String password = credentials.hasPassword() ? credentials.getPassword() : null;
|
String password = credentials.hasPassword() ? credentials.getPassword() : null;
|
||||||
|
|
||||||
|
synchronized (db) {
|
||||||
if (!db.authenticate(username, password == null ? null : password.toCharArray())) {
|
if (!db.authenticate(username, password == null ? null : password.toCharArray())) {
|
||||||
throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName
|
throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName
|
||||||
+ "], username = [" + username + "], password = [" + password + "]", databaseName, credentials);
|
+ "], username = [" + username + "], password = [" + password + "]", databaseName, credentials);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TX sync active, bind new database to thread
|
// TX sync active, bind new database to thread
|
||||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||||
@@ -159,7 +161,7 @@ public abstract class MongoDbUtils {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo);
|
DbHolder dbHolder = (DbHolder) TransactionSynchronizationManager.getResource(mongo);
|
||||||
return (dbHolder != null && dbHolder.containsDB(db));
|
return dbHolder != null && dbHolder.containsDB(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user