From a786db8ab53c879858268da6a0cbe4ba9956d160 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Mon, 4 Feb 2008 05:42:54 +0000 Subject: Make TransactionManager creation more flexible. --- .../com/amazon/carbonado/repo/jdbc/JDBCBlob.java | 4 ++-- .../com/amazon/carbonado/repo/jdbc/JDBCClob.java | 4 ++-- .../amazon/carbonado/repo/jdbc/JDBCRepository.java | 14 +++++++---- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 10 ++++---- .../carbonado/repo/sleepycat/BDBRepository.java | 16 +++++++------ .../carbonado/repo/sleepycat/BDBStorage.java | 24 +++++++++---------- .../amazon/carbonado/spi/AbstractRepository.java | 28 +++++++--------------- 7 files changed, 47 insertions(+), 53 deletions(-) (limited to 'src/main/java/com/amazon') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCBlob.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCBlob.java index 69a8f3d..c031a9e 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCBlob.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCBlob.java @@ -126,7 +126,7 @@ class JDBCBlob extends AbstractBlob implements JDBCLob { throw new FetchException("Blob value is null"); } try { - JDBCTransaction txn = mRepo.localTxnScope().getTxn(); + JDBCTransaction txn = mRepo.localTransactionScope().getTxn(); if (txn != null) { txn.register(this); } @@ -143,7 +143,7 @@ class JDBCBlob extends AbstractBlob implements JDBCLob { if ((mBlob = mLoader.load(mRepo)) == null) { throw new PersistException("Blob value is null"); } - JDBCTransaction txn = mRepo.localTxnScope().getTxn(); + JDBCTransaction txn = mRepo.localTransactionScope().getTxn(); if (txn != null) { txn.register(this); } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCClob.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCClob.java index eedaf80..dc2ec0a 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCClob.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCClob.java @@ -126,7 +126,7 @@ class JDBCClob extends AbstractClob implements JDBCLob { throw new FetchException("Clob value is null"); } try { - JDBCTransaction txn = mRepo.localTxnScope().getTxn(); + JDBCTransaction txn = mRepo.localTransactionScope().getTxn(); if (txn != null) { txn.register(this); } @@ -143,7 +143,7 @@ class JDBCClob extends AbstractClob implements JDBCLob { if ((mClob = mLoader.load(mRepo)) == null) { throw new PersistException("Clob value is null"); } - JDBCTransaction txn = mRepo.localTxnScope().getTxn(); + JDBCTransaction txn = mRepo.localTransactionScope().getTxn(); if (txn != null) { txn.register(this); } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java index dd4e1ad..75fde77 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java @@ -55,6 +55,7 @@ import com.amazon.carbonado.info.StorableProperty; import com.amazon.carbonado.sequence.SequenceCapability; import com.amazon.carbonado.sequence.SequenceValueProducer; import com.amazon.carbonado.spi.AbstractRepository; +import com.amazon.carbonado.spi.TransactionManager; import com.amazon.carbonado.spi.TransactionScope; import com.amazon.carbonado.util.ThrowUnchecked; @@ -194,6 +195,8 @@ public class JDBCRepository extends AbstractRepository private final JDBCSupportStrategy mSupportStrategy; private JDBCExceptionTransformer mExceptionTransformer; + private final JDBCTransactionManager mTxnMgr; + // Mappings from IsolationLevel to best matching supported level. final IsolationLevel mReadUncommittedLevel; final IsolationLevel mReadCommittedLevel; @@ -246,6 +249,8 @@ public class JDBCRepository extends AbstractRepository // Temporarily set to generic one, in case there's a problem during initialization. mExceptionTransformer = new JDBCExceptionTransformer(); + mTxnMgr = new JDBCTransactionManager(this); + getLog().info("Opening repository \"" + getName() + '"'); // Test connectivity and get some info on transaction isolation levels. @@ -697,12 +702,11 @@ public class JDBCRepository extends AbstractRepository return mSupportStrategy.createSequenceValueProducer(name); } - protected JDBCTransactionManager createTransactionManager() { - return new JDBCTransactionManager(this); + protected final TransactionManager transactionManager() { + return mTxnMgr; } - // Provides access to transaction scope from other classes. - final TransactionScope localTxnScope() { - return localTransactionScope(); + protected final TransactionScope localTransactionScope() { + return mTxnMgr.localScope(); } } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index e6fe277..942ebee 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -213,7 +213,7 @@ class JDBCStorage extends StandardQueryFactory if (jblob != null) { try { - JDBCTransaction txn = mRepository.localTxnScope().getTxn(); + JDBCTransaction txn = mRepository.localTransactionScope().getTxn(); if (txn != null) { txn.register(jblob); } @@ -235,7 +235,7 @@ class JDBCStorage extends StandardQueryFactory if (jclob != null) { try { - JDBCTransaction txn = mRepository.localTxnScope().getTxn(); + JDBCTransaction txn = mRepository.localTransactionScope().getTxn(); if (txn != null) { txn.register(jclob); } @@ -606,7 +606,7 @@ class JDBCStorage extends StandardQueryFactory } public Cursor fetch(FilterValues values) throws FetchException { - boolean forUpdate = mRepository.localTxnScope().isForUpdate(); + boolean forUpdate = mRepository.localTransactionScope().isForUpdate(); Connection con = mRepository.getConnection(); try { PreparedStatement ps = con.prepareStatement(prepareSelect(values, forUpdate)); @@ -674,7 +674,7 @@ class JDBCStorage extends StandardQueryFactory throws IOException { indent(app, indentLevel); - boolean forUpdate = mRepository.localTxnScope().isForUpdate(); + boolean forUpdate = mRepository.localTransactionScope().isForUpdate(); app.append(prepareSelect(values, forUpdate)); app.append('\n'); return true; @@ -684,7 +684,7 @@ class JDBCStorage extends StandardQueryFactory throws IOException { try { - boolean forUpdate = mRepository.localTxnScope().isForUpdate(); + boolean forUpdate = mRepository.localTransactionScope().isForUpdate(); String statement = prepareSelect(values, forUpdate); return mRepository.getSupportStrategy().printPlan(app, indentLevel, statement); } catch (FetchException e) { diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java index a0c549b..4bf4e2b 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java @@ -63,6 +63,7 @@ import com.amazon.carbonado.sequence.SequenceValueProducer; import com.amazon.carbonado.spi.AbstractRepository; import com.amazon.carbonado.spi.ExceptionTransformer; import com.amazon.carbonado.spi.LobEngine; +import com.amazon.carbonado.spi.TransactionManager; import com.amazon.carbonado.spi.TransactionScope; /** @@ -93,6 +94,7 @@ abstract class BDBRepository extends AbstractRepository private final AtomicReference mRootRef; private final StorableCodecFactory mStorableCodecFactory; private final ExceptionTransformer mExTransformer; + private final BDBTransactionManager mTxnMgr; Checkpointer mCheckpointer; DeadlockDetector mDeadlockDetector; @@ -141,6 +143,7 @@ abstract class BDBRepository extends AbstractRepository mTriggerFactories = builder.getTriggerFactories(); mRootRef = rootRef; mExTransformer = exTransformer; + mTxnMgr = new BDBTransactionManager(mExTransformer, this); mRunCheckpointer = !builder.getReadOnly() && builder.getRunCheckpointer(); mRunDeadlockDetector = builder.getRunDeadlockDetector(); @@ -342,10 +345,6 @@ abstract class BDBRepository extends AbstractRepository return new SequenceValueGenerator(BDBRepository.this, name); } - protected BDBTransactionManager createTransactionManager() { - return new BDBTransactionManager(mExTransformer, this); - } - /** * @see com.amazon.carbonado.spi.RepositoryBuilder#isMaster */ @@ -489,9 +488,12 @@ abstract class BDBRepository extends AbstractRepository return mExTransformer.toRepositoryException(e); } - // Provides access to transaction scope from other classes. - final TransactionScope localTxnScope() { - return localTransactionScope(); + protected final TransactionManager transactionManager() { + return mTxnMgr; + } + + protected final TransactionScope localTransactionScope() { + return mTxnMgr.localScope(); } /** diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java index e7368ac..126e40f 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBStorage.java @@ -203,7 +203,7 @@ abstract class BDBStorage implements Storage, Storag } } - TransactionScope scope = localTxnScope(); + TransactionScope scope = localTransactionScope(); // Lock out shutdown task. scope.getLock().lock(); @@ -317,7 +317,7 @@ abstract class BDBStorage implements Storage, Storag boolean reverseOrder) throws FetchException { - TransactionScope scope = localTxnScope(); + TransactionScope scope = localTransactionScope(); if (reverseRange) { { @@ -447,7 +447,7 @@ abstract class BDBStorage implements Storage, Storag boolean isPrimaryEmpty; try { - TransactionScope scope = mRepository.localTxnScope(); + TransactionScope scope = mRepository.localTransactionScope(); // Lock out shutdown task. scope.getLock().lock(); try { @@ -572,7 +572,7 @@ abstract class BDBStorage implements Storage, Storag } try { - Txn txn = mRepository.localTxnScope().getTxn(); + Txn txn = mRepository.localTransactionScope().getTxn(); return db_compact(txn, mPrimaryDatabase, start, end); } catch (Exception e) { throw mRepository.toRepositoryException(e); @@ -667,8 +667,8 @@ abstract class BDBStorage implements Storage, Storag return mRepository.toRepositoryException(e); } - TransactionScope localTxnScope() { - return mRepository.localTxnScope(); + TransactionScope localTransactionScope() { + return mRepository.localTransactionScope(); } /** @@ -726,7 +726,7 @@ abstract class BDBStorage implements Storage, Storag * prevent threads from starting work that will likely fail along the way. */ void checkClosed() throws FetchException { - TransactionScope scope = localTxnScope(); + TransactionScope scope = localTransactionScope(); // Lock out shutdown task. scope.getLock().lock(); @@ -747,7 +747,7 @@ abstract class BDBStorage implements Storage, Storag } void close() throws Exception { - TransactionScope scope = mRepository.localTxnScope(); + TransactionScope scope = mRepository.localTransactionScope(); scope.getLock().lock(); try { if (mPrimaryDatabase != null) { @@ -998,7 +998,7 @@ abstract class BDBStorage implements Storage, Storag } public byte[] tryLoad(byte[] key) throws FetchException { - TransactionScope scope = mStorage.localTxnScope(); + TransactionScope scope = mStorage.localTransactionScope(); byte[] result; // Lock out shutdown task. scope.getLock().lock(); @@ -1021,7 +1021,7 @@ abstract class BDBStorage implements Storage, Storag } public boolean tryInsert(S storable, byte[] key, byte[] value) throws PersistException { - TransactionScope scope = mStorage.localTxnScope(); + TransactionScope scope = mStorage.localTransactionScope(); Object result; // Lock out shutdown task. scope.getLock().lock(); @@ -1044,7 +1044,7 @@ abstract class BDBStorage implements Storage, Storag } public void store(S storable, byte[] key, byte[] value) throws PersistException { - TransactionScope scope = mStorage.localTxnScope(); + TransactionScope scope = mStorage.localTransactionScope(); // Lock out shutdown task. scope.getLock().lock(); try { @@ -1061,7 +1061,7 @@ abstract class BDBStorage implements Storage, Storag } public boolean tryDelete(byte[] key) throws PersistException { - TransactionScope scope = mStorage.localTxnScope(); + TransactionScope scope = mStorage.localTransactionScope(); // Lock out shutdown task. scope.getLock().lock(); try { diff --git a/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java b/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java index c9bc0b5..191f94e 100644 --- a/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java +++ b/src/main/java/com/amazon/carbonado/spi/AbstractRepository.java @@ -54,7 +54,6 @@ public abstract class AbstractRepository implements Repository, ShutdownCapability, SequenceCapability { private final String mName; - private final TransactionManager mTxnMgr; private final ReadWriteLock mShutdownLock; private final StoragePool mStoragePool; @@ -97,8 +96,6 @@ public abstract class AbstractRepository } } }; - - mTxnMgr = createTransactionManager(); } public String getName() { @@ -112,19 +109,19 @@ public abstract class AbstractRepository } public Transaction enterTransaction() { - return mTxnMgr.localScope().enter(null); + return localTransactionScope().enter(null); } public Transaction enterTransaction(IsolationLevel level) { - return mTxnMgr.localScope().enter(level); + return localTransactionScope().enter(level); } public Transaction enterTopTransaction(IsolationLevel level) { - return mTxnMgr.localScope().enterTop(level); + return localTransactionScope().enterTop(level); } public IsolationLevel getTransactionIsolationLevel() { - return mTxnMgr.localScope().getIsolationLevel(); + return localTransactionScope().getIsolationLevel(); } /** @@ -207,18 +204,14 @@ public abstract class AbstractRepository } /** - * Returns the TransactionManager which was passed into the constructor. + * Returns the repository's TransactionManager. */ - protected TransactionManager transactionManager() { - return mTxnMgr; - } + protected abstract TransactionManager transactionManager(); /** * Returns the thread-local TransactionScope, creating it if needed. */ - protected TransactionScope localTransactionScope() { - return mTxnMgr.localScope(); - } + protected abstract TransactionScope localTransactionScope(); /** * Call to prevent shutdown hook from running. Be sure to call @@ -281,11 +274,6 @@ public abstract class AbstractRepository protected abstract SequenceValueProducer createSequenceValueProducer(String name) throws RepositoryException; - /** - * Called upon to create a new TransactionManager instance. - */ - protected abstract TransactionManager createTransactionManager(); - void info(String message) { Log log = getLog(); if (log != null) { @@ -337,7 +325,7 @@ public abstract class AbstractRepository repository.mSequencePool.returnReservedValues(null); try { - repository.mTxnMgr.close(suspendThreads); + repository.transactionManager().close(suspendThreads); } catch (Throwable e) { repository.error("Failed to close TransactionManager", e); } -- cgit v1.2.3