com.sleepycat.je.txn
Class Txn

java.lang.Object
  extended by com.sleepycat.je.txn.Locker
      extended by com.sleepycat.je.txn.Txn
All Implemented Interfaces:
LogReadable, LogWritable
Direct Known Subclasses:
AutoTxn

public class Txn
extends Locker
implements LogWritable, LogReadable

A Txn is one that's created by a call to Environment.txnBegin. This class must support multithreaded use.


Field Summary
static int ACCUMULATED_LIMIT
           
static byte TXN_NOSYNC
           
static byte TXN_SYNC
           
static byte TXN_WRITE_NOSYNC
           
 
Fields inherited from class com.sleepycat.je.txn.Locker
defaultNoWait, deleteInfo, envImpl, handleLockToHandleMap, handleToHandleLockMap, id, lockManager, lockTimeOutMillis, readUncommittedDefault, thread
 
Constructor Summary
Txn()
          Constructor for reading from log.
Txn(EnvironmentImpl envImpl, TransactionConfig config)
          Create a transaction from Environment.txnBegin.
Txn(EnvironmentImpl envImpl, TransactionConfig config, long id)
           
 
Method Summary
 long abort(boolean forceFlush)
          Abort this transaction.
 void abort(Xid xid)
           
(package private)  void addLock(Long nodeId, Lock lock, LockType type, LockGrantType grantStatus, MemoryBudget mb)
          Add lock to the appropriate queue.
 void addLogInfo(long lastLsn)
          Called by the recovery manager when logging a transaction aware object.
protected  void checkState(boolean calledByAbort)
          Throw an exception if the transaction is not open.
 LockStats collectStats(LockStats stats)
          stats
 long commit()
          Call commit() with the default sync configuration property.
 long commit(byte flushSyncBehavior)
          Commit this transaction 1.
 void commit(Xid xid)
           
 boolean createdNode(long nodeId)
           
 void dumpLog(StringBuffer sb, boolean verbose)
          Write the object into the string buffer for log dumping.
protected  long generateId(TxnManager txnManager)
          UserTxns get a new unique id for each instance.
 boolean getAbortKnownDeleted(long nodeId)
           
 long getAbortLsn(long nodeId)
           
(package private)  int getAccumulatedDelta()
           
(package private)  long getFirstActiveLsn()
           
(package private)  int getInMemorySize()
           
(package private)  long getLastLsn()
          Access to last LSN.
 int getLogSize()
           
 boolean getOnlyAbortable()
          Get the state of a transaction's ONLY_ABORTABLE.
 long getTransactionId()
           
 Txn getTxnLocker()
          This is a transactional locker.
 boolean isHandleLockTransferrable()
           
 boolean isReadCommittedIsolation()
          Is read-committed isolation if so configured.
 boolean isSerializableIsolation()
          Is serializable isolation if so configured.
 boolean isSuspended()
           
 boolean isTransactional()
          Is always transactional.
 LockResult lock(long nodeId, LockType lockType, DatabaseImpl database)
          Gets a lock on this nodeId and, if it is a write lock, saves an abort LSN.
 boolean logEntryIsTransactional()
           
 void markDeleteAtTxnEnd(DatabaseImpl dbImpl, boolean deleteAtCommit, MemoryBudget mb)
          Database operations like remove and truncate leave behind residual DatabaseImpls that must be purged at transaction commit or abort.
(package private)  void moveWriteToReadLock(long nodeId, Lock lock, MemoryBudget mb)
          A lock is being demoted.
 Locker newNonTxnLocker()
          Returns 'this', since this locker holds no non-transactional locks.
 void operationEnd()
          Created transactions do nothing at the end of the operation.
 void operationEnd(boolean operationOK)
          Created transactions do nothing at the end of the operation.
 int prepare(Xid xid)
           
 void readFromLog(ByteBuffer logBuffer, byte entryTypeVersion)
          Initialize this object from the data in itemBuf.
 void registerCursor(CursorImpl cursor)
          Cursors operating under this transaction are added to the collection.
 void releaseNonTxnLocks()
          This locker holds no non-transactional locks.
(package private)  void removeLock(long nodeId, Lock lock)
          Remove the lock from the set owned by this transaction.
 void setHandleLockOwner(boolean ignore, Database dbHandle, boolean dbIsClosing)
          Created transactions don't transfer locks until commit.
 void setOnlyAbortable()
          Set the state of a transaction to ONLY_ABORTABLE.
 void setPrepared(boolean prepared)
           
 void setSuspended(boolean suspended)
           
 void unRegisterCursor(CursorImpl cursor)
          Remove a cursor from the collection.
 void writeToLog(ByteBuffer logBuffer)
          Serialize this object into the buffer.
 
Methods inherited from class com.sleepycat.je.txn.Locker
addDeleteInfo, addToHandleMaps, demoteLock, dumpLockTable, getId, getLockTimeout, getTxnStartMillis, getTxnTimeOut, getWaitingFor, isReadUncommittedDefault, isTimedOut, operationEnd, releaseLock, rememberHandleWriteLock, setLockTimeout, setTxnTimeout, setWaitingFor, sharesLocksWith, toString, transferHandleLock, transferHandleLockToHandle, unregisterHandle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TXN_NOSYNC

public static final byte TXN_NOSYNC
See Also:
Constant Field Values

TXN_WRITE_NOSYNC

public static final byte TXN_WRITE_NOSYNC
See Also:
Constant Field Values

TXN_SYNC

public static final byte TXN_SYNC
See Also:
Constant Field Values

ACCUMULATED_LIMIT

public static int ACCUMULATED_LIMIT
Constructor Detail

Txn

public Txn(EnvironmentImpl envImpl,
           TransactionConfig config)
    throws DatabaseException
Create a transaction from Environment.txnBegin.

Throws:
DatabaseException

Txn

public Txn(EnvironmentImpl envImpl,
           TransactionConfig config,
           long id)
    throws DatabaseException
Throws:
DatabaseException

Txn

public Txn()
Constructor for reading from log.

Method Detail

generateId

protected long generateId(TxnManager txnManager)
UserTxns get a new unique id for each instance.

Specified by:
generateId in class Locker

getLastLsn

long getLastLsn()
Access to last LSN.


setPrepared

public void setPrepared(boolean prepared)

setSuspended

public void setSuspended(boolean suspended)

isSuspended

public boolean isSuspended()

lock

public LockResult lock(long nodeId,
                       LockType lockType,
                       DatabaseImpl database)
                throws DatabaseException
Gets a lock on this nodeId and, if it is a write lock, saves an abort LSN. Caller will set the abortLsn later, after the write lock has been obtained.

Specified by:
lock in class Locker
Throws:
DatabaseException

prepare

public int prepare(Xid xid)
            throws DatabaseException
Throws:
DatabaseException

commit

public void commit(Xid xid)
            throws DatabaseException
Throws:
DatabaseException

abort

public void abort(Xid xid)
           throws DatabaseException
Throws:
DatabaseException

commit

public long commit()
            throws DatabaseException
Call commit() with the default sync configuration property.

Throws:
DatabaseException

commit

public long commit(byte flushSyncBehavior)
            throws DatabaseException
Commit this transaction 1. Releases read locks 2. Writes a txn commit record into the log 3. Flushes the log to disk. 4. Add deleted LN info to IN compressor queue 5. Release all write locks If any step of this fails, we must convert this transaction to an abort.

Throws:
DatabaseException

abort

public long abort(boolean forceFlush)
           throws DatabaseException
Abort this transaction. Steps are: 1. Release LN read locks. 2. Write a txn abort entry to the log. This is only for log file cleaning optimization and there's no need to guarantee a flush to disk. 3. Find the last LN log entry written for this txn, and use that to traverse the log looking for nodes to undo. For each node, use the same undo logic as recovery to rollback the transaction. Note that we walk the log in order to undo in reverse order of the actual operations. For example, suppose the txn did this: delete K1/D1 (in LN 10) create K1/D1 (in LN 20) If we process LN10 before LN 20, we'd inadvertently create a duplicate tree of "K1", which would be fatal for the mapping tree. 4. Release the write lock for this LN.

Throws:
DatabaseException

addLogInfo

public void addLogInfo(long lastLsn)
                throws DatabaseException
Called by the recovery manager when logging a transaction aware object. This method is synchronized by the caller, by being called within the log latch. Record the last LSN for this transaction, to create the transaction chain, and also record the LSN in the write info for abort logic.

Throws:
DatabaseException

getFirstActiveLsn

long getFirstActiveLsn()
                 throws DatabaseException
Returns:
first logged LSN, to aid recovery rollback.
Throws:
DatabaseException

markDeleteAtTxnEnd

public void markDeleteAtTxnEnd(DatabaseImpl dbImpl,
                               boolean deleteAtCommit,
                               MemoryBudget mb)
                        throws DatabaseException
Description copied from class: Locker
Database operations like remove and truncate leave behind residual DatabaseImpls that must be purged at transaction commit or abort.

Specified by:
markDeleteAtTxnEnd in class Locker
Parameters:
dbImpl - databaseImpl to remove
deleteAtCommit - true if this databaseImpl should be cleaned on commit, false if it should be cleaned on abort.
mb - environment memory budget.
Throws:
DatabaseException

addLock

void addLock(Long nodeId,
             Lock lock,
             LockType type,
             LockGrantType grantStatus,
             MemoryBudget mb)
       throws DatabaseException
Add lock to the appropriate queue.

Specified by:
addLock in class Locker
Throws:
DatabaseException

removeLock

void removeLock(long nodeId,
                Lock lock)
          throws DatabaseException
Remove the lock from the set owned by this transaction. If specified to LockManager.release, the lock manager will call this when its releasing a lock. Usually done because the transaction doesn't need to really keep the lock, i.e for a deleted record.

Specified by:
removeLock in class Locker
Throws:
DatabaseException

moveWriteToReadLock

void moveWriteToReadLock(long nodeId,
                         Lock lock,
                         MemoryBudget mb)
A lock is being demoted. Move it from the write collection into the read collection.

Specified by:
moveWriteToReadLock in class Locker

getAccumulatedDelta

int getAccumulatedDelta()

createdNode

public boolean createdNode(long nodeId)
                    throws DatabaseException
Specified by:
createdNode in class Locker
Returns:
true if this transaction created this node. We know that this is true if the node is write locked and has a null abort LSN.
Throws:
DatabaseException

getAbortLsn

public long getAbortLsn(long nodeId)
                 throws DatabaseException
Specified by:
getAbortLsn in class Locker
Returns:
the abortLsn for this node.
Throws:
DatabaseException

getAbortKnownDeleted

public boolean getAbortKnownDeleted(long nodeId)
                             throws DatabaseException
Specified by:
getAbortKnownDeleted in class Locker
Returns:
the abortKnownDeleted for this node.
Throws:
DatabaseException

isTransactional

public boolean isTransactional()
Is always transactional.

Specified by:
isTransactional in class Locker

isSerializableIsolation

public boolean isSerializableIsolation()
Is serializable isolation if so configured.

Specified by:
isSerializableIsolation in class Locker

isReadCommittedIsolation

public boolean isReadCommittedIsolation()
Is read-committed isolation if so configured.

Specified by:
isReadCommittedIsolation in class Locker

getTxnLocker

public Txn getTxnLocker()
This is a transactional locker.

Specified by:
getTxnLocker in class Locker

newNonTxnLocker

public Locker newNonTxnLocker()
                       throws DatabaseException
Returns 'this', since this locker holds no non-transactional locks.

Specified by:
newNonTxnLocker in class Locker
Throws:
DatabaseException

releaseNonTxnLocks

public void releaseNonTxnLocks()
                        throws DatabaseException
This locker holds no non-transactional locks.

Specified by:
releaseNonTxnLocks in class Locker
Throws:
DatabaseException

operationEnd

public void operationEnd()
                  throws DatabaseException
Created transactions do nothing at the end of the operation.

Specified by:
operationEnd in class Locker
Throws:
DatabaseException

operationEnd

public void operationEnd(boolean operationOK)
                  throws DatabaseException
Created transactions do nothing at the end of the operation.

Specified by:
operationEnd in class Locker
Parameters:
operationOK - is whether the operation succeeded, since that may impact ending behavior. (i.e for AutoTxn)
Throws:
DatabaseException

setHandleLockOwner

public void setHandleLockOwner(boolean ignore,
                               Database dbHandle,
                               boolean dbIsClosing)
                        throws DatabaseException
Created transactions don't transfer locks until commit.

Specified by:
setHandleLockOwner in class Locker
Throws:
DatabaseException

registerCursor

public void registerCursor(CursorImpl cursor)
                    throws DatabaseException
Cursors operating under this transaction are added to the collection.

Specified by:
registerCursor in class Locker
Throws:
DatabaseException

unRegisterCursor

public void unRegisterCursor(CursorImpl cursor)
                      throws DatabaseException
Remove a cursor from the collection.

Specified by:
unRegisterCursor in class Locker
Throws:
DatabaseException

isHandleLockTransferrable

public boolean isHandleLockTransferrable()
Overrides:
isHandleLockTransferrable in class Locker
Returns:
true if this txn is willing to give up the handle lock to another txn before this txn ends.

collectStats

public LockStats collectStats(LockStats stats)
                       throws DatabaseException
stats

Specified by:
collectStats in class Locker
Throws:
DatabaseException

setOnlyAbortable

public void setOnlyAbortable()
Set the state of a transaction to ONLY_ABORTABLE.

Overrides:
setOnlyAbortable in class Locker

getOnlyAbortable

public boolean getOnlyAbortable()
Get the state of a transaction's ONLY_ABORTABLE.


checkState

protected void checkState(boolean calledByAbort)
                   throws DatabaseException
Throw an exception if the transaction is not open. If calledByAbort is true, it means we're being called from abort().

Specified by:
checkState in class Locker
Throws:
DatabaseException

getLogSize

public int getLogSize()
Specified by:
getLogSize in interface LogWritable
Returns:
number of bytes used to store this object.
See Also:
LogWritable.getLogSize()

writeToLog

public void writeToLog(ByteBuffer logBuffer)
Description copied from interface: LogWritable
Serialize this object into the buffer.

Specified by:
writeToLog in interface LogWritable
Parameters:
logBuffer - is the destination buffer
See Also:
LogWritable.writeToLog(java.nio.ByteBuffer)

readFromLog

public void readFromLog(ByteBuffer logBuffer,
                        byte entryTypeVersion)
Description copied from interface: LogReadable
Initialize this object from the data in itemBuf.

Specified by:
readFromLog in interface LogReadable
See Also:
It's ok for FindBugs to whine about id not being synchronized.

dumpLog

public void dumpLog(StringBuffer sb,
                    boolean verbose)
Description copied from interface: LogReadable
Write the object into the string buffer for log dumping. Each object should be dumped without indentation or new lines and should be valid XML.

Specified by:
dumpLog in interface LogReadable
Parameters:
sb - destination string buffer
verbose - if true, dump the full, verbose version
See Also:
LogReadable.dumpLog(java.lang.StringBuffer, boolean)

getTransactionId

public long getTransactionId()
Specified by:
getTransactionId in interface LogReadable
Returns:
return the transaction id if this log entry is transactional, 0 otherwise.
See Also:
LogReadable.getTransactionId()

logEntryIsTransactional

public boolean logEntryIsTransactional()
Specified by:
logEntryIsTransactional in interface LogReadable
Returns:
true if the LogEntry is a transactional log entry type.
See Also:
LogReadable.logEntryIsTransactional()

getInMemorySize

int getInMemorySize()


Copyright 2004-2005 Sleepycat, Inc. All Rights Reserved.