From 43dee69fe0301ef6609f1962af97333c15913a1b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 25 Jun 2012 11:16:31 +0200 Subject: [PATCH] DATAMONGO-464 - Fixed resource synchronization in MongoDbUtils. MongoDbUtils now correctly returns DB instances for others than the first one bound. So far the lookup for an alternate database resulted in the first one bound to be returned. --- .../data/mongodb/core/MongoDbUtils.java | 11 ++-- .../mongodb/core/MongoDbUtilsUnitTests.java | 65 +++++++++++++++++++ 2 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoDbUtilsUnitTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoDbUtils.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoDbUtils.java index 6dfdc0991..0826e25f4 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoDbUtils.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoDbUtils.java @@ -15,14 +15,15 @@ */ package org.springframework.data.mongodb.core; -import com.mongodb.DB; -import com.mongodb.Mongo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.data.mongodb.CannotGetMongoDbConnectionException; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.Assert; +import com.mongodb.DB; +import com.mongodb.Mongo; + /** * Helper class featuring helper methods for internal MongoDb classes. *

@@ -78,7 +79,7 @@ public abstract class MongoDbUtils { DB db = null; if (TransactionSynchronizationManager.isSynchronizationActive() && dbHolder.doesNotHoldNonDefaultDB()) { // Spring transaction management is active -> - db = dbHolder.getDB(); + db = dbHolder.getDB(databaseName); if (db != null && !dbHolder.isSynchronizedWithTransaction()) { LOGGER.debug("Registering Spring transaction synchronization for existing Mongo DB"); TransactionSynchronizationManager.registerSynchronization(new MongoSynchronization(dbHolder, mongo)); @@ -110,9 +111,9 @@ public abstract class MongoDbUtils { LOGGER.debug("Registering Spring transaction synchronization for new Hibernate Session"); DbHolder holderToUse = dbHolder; if (holderToUse == null) { - holderToUse = new DbHolder(db); + holderToUse = new DbHolder(databaseName, db); } else { - holderToUse.addDB(db); + holderToUse.addDB(databaseName, db); } TransactionSynchronizationManager.registerSynchronization(new MongoSynchronization(holderToUse, mongo)); holderToUse.setSynchronizedWithTransaction(true); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoDbUtilsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoDbUtilsUnitTests.java new file mode 100644 index 000000000..571e82b92 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoDbUtilsUnitTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +import com.mongodb.DB; +import com.mongodb.Mongo; + +/** + * Unit tests for {@link MongoDbUtils}. + * + * @author Oliver Gierke + */ +public class MongoDbUtilsUnitTests { + + Mongo mongo; + + @Before + public void setUp() throws Exception { + this.mongo = new Mongo(); + TransactionSynchronizationManager.initSynchronization(); + } + + @After + public void tearDown() { + + for (Object key : TransactionSynchronizationManager.getResourceMap().keySet()) { + TransactionSynchronizationManager.unbindResource(key); + } + + TransactionSynchronizationManager.clearSynchronization(); + } + + @Test + public void returnsNewInstanceForDifferentDatabaseName() { + + DB first = MongoDbUtils.getDB(mongo, "first"); + assertThat(first, is(notNullValue())); + assertThat(MongoDbUtils.getDB(mongo, "first"), is(first)); + + DB second = MongoDbUtils.getDB(mongo, "second"); + assertThat(second, is(not(first))); + assertThat(MongoDbUtils.getDB(mongo, "second"), is(second)); + } +}