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:
Oliver Gierke
2012-09-12 12:58:31 +02:00
parent fdecec48b2
commit 6744446a48

View File

@@ -109,9 +109,11 @@ 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;
if (!db.authenticate(username, password == null ? null : password.toCharArray())) { synchronized (db) {
throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName if (!db.authenticate(username, password == null ? null : password.toCharArray())) {
+ "], username = [" + username + "], password = [" + password + "]", databaseName, credentials); throw new CannotGetMongoDbConnectionException("Failed to authenticate to database [" + databaseName
+ "], username = [" + username + "], password = [" + password + "]", databaseName, credentials);
}
} }
} }
@@ -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);
} }
/** /**