summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2012-01-24 21:35:01 +0000
committerBrian S. O'Neill <bronee@gmail.com>2012-01-24 21:35:01 +0000
commitaea108ba2b5f22cf5615bd02d719786e84d359da (patch)
treeb913c64fd530f6bd114348aba30fd1857ff26549 /src/main/java/com/amazon/carbonado
parent48fbe4abbd41c8faa0065a2be4f11839268bd0c7 (diff)
Fix race condition when making initial connection to a slow database.
Diffstat (limited to 'src/main/java/com/amazon/carbonado')
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java8
-rw-r--r--src/main/java/com/amazon/carbonado/spi/BelatedRepositoryCreator.java11
2 files changed, 17 insertions, 2 deletions
diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java
index 5ac62e3..b98993e 100644
--- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java
+++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepositoryBuilder.java
@@ -85,6 +85,9 @@ public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder {
public Repository build(AtomicReference<Repository> rootRef) throws RepositoryException {
assertReady();
+
+ final Repository originalRoot = rootRef.get();
+
JDBCRepository repo = new JDBCRepository
(rootRef, getName(), isMaster(), getTriggerFactories(),
getDataSource(), getDataSourceCloseOnShutdown(),
@@ -94,7 +97,10 @@ public class JDBCRepositoryBuilder extends AbstractRepositoryBuilder {
getSuppressReloadMap(),
mSequenceSelectStatement, mForceStoredSequence, mPrimaryKeyCheckDisabled,
mResolver);
- rootRef.set(repo);
+
+ // Don't wipe out root when using BelatedRepositoryCreator.
+ rootRef.compareAndSet(originalRoot, repo);
+
return repo;
}
diff --git a/src/main/java/com/amazon/carbonado/spi/BelatedRepositoryCreator.java b/src/main/java/com/amazon/carbonado/spi/BelatedRepositoryCreator.java
index afb4fba..eeb2b04 100644
--- a/src/main/java/com/amazon/carbonado/spi/BelatedRepositoryCreator.java
+++ b/src/main/java/com/amazon/carbonado/spi/BelatedRepositoryCreator.java
@@ -50,6 +50,8 @@ public class BelatedRepositoryCreator extends BelatedCreator<Repository, Support
final RepositoryBuilder mBuilder;
final AtomicReference<Repository> mRootRef;
+ private boolean mRetry;
+
/**
* @param log error reporting log
* @param builder builds real Repository
@@ -80,9 +82,16 @@ public class BelatedRepositoryCreator extends BelatedCreator<Repository, Support
@Override
protected Repository createReal() throws SupportException {
+ // For first attempt, use the real root reference. For retries, it
+ // should not be used because it will destroy the root which was
+ // successfully built and is in use. Instead, pass a dummy ref.
+ AtomicReference<Repository> rootRef =
+ mRetry ? new AtomicReference<Repository>() : mRootRef;
+ mRetry = true;
+
Exception error;
try {
- return mBuilder.build(mRootRef);
+ return mBuilder.build(rootRef);
} catch (SupportException e) {
// Cannot recover from this.
throw e;