|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.hivetranse.transaction.AbstractTransactionService
public abstract class AbstractTransactionService
Base class for every actual implementation of TransactionService
.
This class manages the stack of transaction contexts and the notification
of TransactionEventListener
s.
Subclasses just have to implement two abstract methods to begin and close an
actual transaction, and possibly a third method to store the changing context
somewhere. For those subclasses, the notion of transaction can be any
Object
.
ServiceModel of subclasses must be threaded or pooled
Nested Class Summary | |
---|---|
protected static class |
AbstractTransactionService.Context
|
protected static class |
AbstractTransactionService.TransactionObject
|
Field Summary | |
---|---|
protected AbstractTransactionService.Context |
_current
|
protected org.apache.hivemind.util.EventListenerList |
_listeners
|
protected org.apache.commons.logging.Log |
_logger
|
protected Stack<AbstractTransactionService.Context> |
_stack
|
protected boolean |
_wrapRuntimeExceptions
|
Constructor Summary | |
---|---|
protected |
AbstractTransactionService(org.apache.commons.logging.Log logger,
boolean wrapRuntimeExceptions)
|
Method Summary | |
---|---|
void |
activateService()
|
void |
addTransactionEventListener(TransactionEventListener listener)
Adds a new listener to the list of TransactionEventListener s
notified by this TransactionService upon transaction. |
void |
begin(TransactionDemarcation demarcation)
Marks the beginning of a new transaction demarcation scope. |
protected java.lang.Object |
beginTransaction()
|
protected void |
cleanUp()
|
protected java.lang.Exception |
cleanUp(AbstractTransactionService.Context context)
|
protected abstract java.lang.Object |
createTransaction()
Method called when creation of a new "transaction" is needed. |
void |
end()
Marks the end of a transaction demarcation scope. |
protected abstract void |
endTransaction(java.lang.Object tx,
boolean commit)
Method called when the current "transaction" must end, either committed or rolled back. |
protected void |
fireAfterNewTransactionBegins()
|
protected void |
fireAfterTransactionEnds(boolean committed)
|
protected void |
fireBeforeNewTransactionBegins()
|
protected void |
fireBeforeTransactionEnds(boolean committed)
|
java.lang.Object |
getCurrentTransaction()
Get the current "transaction" context for the calling thread. |
void |
passivateService()
|
void |
removeTransactionEventListener(TransactionEventListener listener)
Removes a listener from the list of TransactionEventListener s
notified by this TransactionService upon transaction. |
void |
setRollbackOnly()
Marks the current transaction to be rolled back when it will end. |
protected void |
storeTransaction()
|
protected void |
storeTransaction(java.lang.Object tx)
Method called whenever the current transaction context changes. |
void |
threadDidDiscardService()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final org.apache.commons.logging.Log _logger
protected final boolean _wrapRuntimeExceptions
protected final Stack<AbstractTransactionService.Context> _stack
protected final org.apache.hivemind.util.EventListenerList _listeners
protected AbstractTransactionService.Context _current
Constructor Detail |
---|
protected AbstractTransactionService(org.apache.commons.logging.Log logger, boolean wrapRuntimeExceptions)
Method Detail |
---|
public void threadDidDiscardService()
threadDidDiscardService
in interface org.apache.hivemind.Discardable
public void activateService()
activateService
in interface org.apache.hivemind.PoolManageable
public void passivateService()
passivateService
in interface org.apache.hivemind.PoolManageable
public void addTransactionEventListener(TransactionEventListener listener)
TransactionService
TransactionEventListener
s
notified by this TransactionService
upon transaction.
addTransactionEventListener
in interface TransactionService
listener
- the listener to add to the listpublic void removeTransactionEventListener(TransactionEventListener listener)
TransactionService
TransactionEventListener
s
notified by this TransactionService
upon transaction.
removeTransactionEventListener
in interface TransactionService
listener
- the listener to remove from the listpublic java.lang.Object getCurrentTransaction()
TransactionService
This method is for internal use (eg, by services proxying actual connections).
getCurrentTransaction
in interface TransactionService
null
if there is no transaction in the current context. Note
that if there is a transaction in the current context, this object
cannot be null
, hence the method can be used to
check if there is a pending transaction for the current context.public void begin(TransactionDemarcation demarcation) throws TransactionException, MandatoryTransactionException, ForbiddenTransactionException
TransactionService
demarcation
, the service may create a new transaction,
throw an exception, or do nothing at all.
begin
in interface TransactionService
demarcation
- transaction for the new beginning scope
MandatoryTransactionException
- if there is no current
transaction and demarcation == Mandatory
ForbiddenTransactionException
- in a transaction currently
exists and demarcation == Never
TransactionException
- in case of any problem preventing the
creation of a new transaction (implementation-dependent)public void end() throws TransactionException
TransactionService
demarcation
for this scope, the service may
end the current transaction, throw an exception, or do nothing at all.
The current transaction, if necessary, will be either committed or
rolled back, depending on whether setRollbackOnly
was called
previously for this transaction.
In addition, the method might activate a previously active transaction.
If there is no current transaction, this is not considered an error but an indication of scope-leaving, which lets the possibility of scope unfolding, in order to set the new current transaction context.
end
in interface TransactionService
TransactionException
- in case of any problem preventing the
commitment of the current transaction (implementation-dependent)public void setRollbackOnly() throws TransactionException
TransactionService
If there is no current transaction, calling this method is considered an error.
setRollbackOnly
in interface TransactionService
MandatoryTransactionException
- in case there is no transaction
in the current call context.
TransactionException
protected void fireBeforeNewTransactionBegins()
protected void fireAfterNewTransactionBegins()
protected void fireBeforeTransactionEnds(boolean committed)
protected void fireAfterTransactionEnds(boolean committed)
protected void cleanUp()
protected java.lang.Object beginTransaction()
protected abstract java.lang.Object createTransaction() throws java.lang.Exception
Please note that "transaction" may be anything and does not have to be just a transaction, ie it may be a JDBC Connection, a Hibernate Session...
AbstractTransactionService
.
java.lang.Exception
- any exception, that will eventually be wrapped into
an -unchecked- TransactionExceptionprotected abstract void endTransaction(java.lang.Object tx, boolean commit) throws java.lang.Exception
tx
- the "transaction" object (ie, the same object returned by
createTransaction
)commit
- true
if the transaction represented by
tx
must be committed, false
if it must be
rolled back
java.lang.Exception
- any exception, that will eventually be wrapped into
an -unchecked- TransactionExceptionprotected void storeTransaction(java.lang.Object tx)
Override this method if you have specific needs when a transaction context changes occur. For instance, subclasses could store the new current transaction context into ThreadLocalStorage for use by other services.
Please carefully note that overriding this method is not equivalent to using TransactionEvents (these are triggered when a Transaction is created or ended, but they do not indicate what the next transaction context is).
tx
- new current "transaction" object, as created by
createTransaction
; might be null
to indicate
that there is no available transaction context from now on.protected void storeTransaction()
protected java.lang.Exception cleanUp(AbstractTransactionService.Context context)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |