📄 currenttransaction.java
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2000,2007 Oracle. All rights reserved. * * $Id: CurrentTransaction.java,v 1.46.2.1 2007/02/01 14:49:39 cwl Exp $ */package com.sleepycat.collections;import java.lang.ref.WeakReference;import java.util.ArrayList;import java.util.List;import java.util.WeakHashMap;import com.sleepycat.compat.DbCompat;import com.sleepycat.je.Cursor;import com.sleepycat.je.CursorConfig;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseException;import com.sleepycat.je.Environment;import com.sleepycat.je.EnvironmentConfig;import com.sleepycat.je.LockMode;import com.sleepycat.je.Transaction;import com.sleepycat.je.TransactionConfig;import com.sleepycat.util.RuntimeExceptionWrapper;/** * Provides access to the current transaction for the current thread within the * context of a Berkeley DB environment. This class provides explicit * transaction control beyond that provided by the {@link TransactionRunner} * class. However, both methods of transaction control manage per-thread * transactions. * * @author Mark Hayes */public class CurrentTransaction { /* For internal use, this class doubles as an Environment wrapper. */ private static WeakHashMap envMap = new WeakHashMap(); private LockMode writeLockMode; private boolean cdbMode; private boolean txnMode; private boolean lockingMode; private Environment env; private ThreadLocal localTrans = new ThreadLocal(); private ThreadLocal localCdbCursors; /** * Gets the CurrentTransaction accessor for a specified Berkeley DB * environment. This method always returns the same reference when called * more than once with the same environment parameter. * * @param env is an open Berkeley DB environment. * * @return the CurrentTransaction accessor for the given environment, or * null if the environment is not transactional. */ public static CurrentTransaction getInstance(Environment env) { CurrentTransaction currentTxn = getInstanceInternal(env); return currentTxn.isTxnMode() ? currentTxn : null; } /** * Gets the CurrentTransaction accessor for a specified Berkeley DB * environment. Unlike getInstance(), this method never returns null. * * @param env is an open Berkeley DB environment. */ static CurrentTransaction getInstanceInternal(Environment env) { synchronized (envMap) { CurrentTransaction myEnv = null; WeakReference myEnvRef = (WeakReference) envMap.get(env); if (myEnvRef != null) { myEnv = (CurrentTransaction) myEnvRef.get(); } if (myEnv == null) { myEnv = new CurrentTransaction(env); envMap.put(env, new WeakReference(myEnv)); } return myEnv; } } private CurrentTransaction(Environment env) { this.env = env; try { EnvironmentConfig config = env.getConfig(); txnMode = config.getTransactional(); lockingMode = DbCompat.getInitializeLocking(config); if (txnMode || lockingMode) { writeLockMode = LockMode.RMW; } else { writeLockMode = LockMode.DEFAULT; } cdbMode = DbCompat.getInitializeCDB(config); if (cdbMode) { localCdbCursors = new ThreadLocal(); } } catch (DatabaseException e) { throw new RuntimeExceptionWrapper(e); } } /** * Returns whether environment is configured for locking. */ final boolean isLockingMode() { return lockingMode; } /** * Returns whether this is a transactional environment. */ final boolean isTxnMode() { return txnMode; } /** * Returns whether this is a Concurrent Data Store environment. */ final boolean isCdbMode() { return cdbMode; } /** * Return the LockMode.RMW or null, depending on whether locking is * enabled. LockMode.RMW will cause an error if passed when locking * is not enabled. Locking is enabled if locking or transactions were * specified for this environment. */ final LockMode getWriteLockMode() { return writeLockMode; } /** * Returns the underlying Berkeley DB environment. */ public final Environment getEnvironment() { return env; } /** * Returns the transaction associated with the current thread for this * environment, or null if no transaction is active. */ public final Transaction getTransaction() { Trans trans = (Trans) localTrans.get(); return (trans != null) ? trans.txn : null; } /** * Returns whether auto-commit may be performed by the collections API. * True is returned if no collections API transaction is currently active, * and no XA transaction is currently active. */ boolean isAutoCommitAllowed() throws DatabaseException { return getTransaction() == null && DbCompat.getThreadTransaction(env) == null; } /** * Begins a new transaction for this environment and associates it with * the current thread. If a transaction is already active for this * environment and thread, a nested transaction will be created. * * @param config the transaction configuration used for calling * {@link Environment#beginTransaction}, or null to use the default * configuration. * * @return the new transaction. * * @throws DatabaseException if the transaction cannot be started, in which * case any existing transaction is not affected. * * @throws IllegalStateException if a transaction is already active and * nested transactions are not supported by the environment. */ public final Transaction beginTransaction(TransactionConfig config) throws DatabaseException { Trans trans = (Trans) localTrans.get(); if (trans != null) { if (trans.txn != null) { if (!DbCompat.NESTED_TRANSACTIONS) { throw new IllegalStateException( "Nested transactions are not supported"); } Transaction parentTxn = trans.txn; trans = new Trans(trans, config); trans.txn = env.beginTransaction(parentTxn, config); localTrans.set(trans); } else { trans.txn = env.beginTransaction(null, config); trans.config = config; } } else { trans = new Trans(null, config); trans.txn = env.beginTransaction(null, config); localTrans.set(trans); } return trans.txn; } /** * Commits the transaction that is active for the current thread for this * environment and makes the parent transaction (if any) the current * transaction. * * @return the parent transaction or null if the committed transaction was * not nested. * * @throws DatabaseException if an error occurs committing the transaction. * The transaction will still be closed and the parent transaction will * become the current transaction. * * @throws IllegalStateException if no transaction is active for the * current thread for this environment. */ public final Transaction commitTransaction() throws DatabaseException, IllegalStateException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -