001: /*
002: * $Id: AbstractTransaction.java 10590 2008-01-29 01:39:47Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transaction;
012:
013: import org.mule.MuleServer;
014: import org.mule.api.transaction.Transaction;
015: import org.mule.api.transaction.TransactionException;
016: import org.mule.config.i18n.CoreMessages;
017: import org.mule.context.notification.TransactionNotification;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: /**
023: * This base class provides low level features for transactions.
024: */
025: public abstract class AbstractTransaction implements Transaction {
026:
027: protected final transient Log logger = LogFactory
028: .getLog(getClass());
029:
030: /*
031: * (non-Javadoc)
032: *
033: * @see org.mule.api.Transaction#isRollbackOnly()
034: */
035: public boolean isRollbackOnly() throws TransactionException {
036: return getStatus() == STATUS_MARKED_ROLLBACK;
037: }
038:
039: /*
040: * (non-Javadoc)
041: *
042: * @see org.mule.api.Transaction#isBegun()
043: */
044: public boolean isBegun() throws TransactionException {
045: int status = getStatus();
046: return status != STATUS_NO_TRANSACTION
047: && status != STATUS_UNKNOWN;
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.mule.api.Transaction#isRolledBack()
054: */
055: public boolean isRolledBack() throws TransactionException {
056: return getStatus() == STATUS_ROLLEDBACK;
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see org.mule.api.Transaction#isCommitted()
063: */
064: public boolean isCommitted() throws TransactionException {
065: return getStatus() == STATUS_COMMITTED;
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.mule.api.Transaction#begin()
072: */
073: public void begin() throws TransactionException {
074: logger.debug("Beginning transaction");
075: doBegin();
076: TransactionCoordination.getInstance().bindTransaction(this );
077: fireNotification(new TransactionNotification(this ,
078: TransactionNotification.TRANSACTION_BEGAN));
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.mule.api.Transaction#commit()
085: */
086: public void commit() throws TransactionException {
087: try {
088: logger.debug("Committing transaction " + this );
089:
090: if (isRollbackOnly()) {
091: throw new IllegalTransactionStateException(CoreMessages
092: .transactionMarkedForRollback());
093: }
094:
095: doCommit();
096: fireNotification(new TransactionNotification(this ,
097: TransactionNotification.TRANSACTION_COMMITTED));
098: } finally {
099: TransactionCoordination.getInstance().unbindTransaction(
100: this );
101: }
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.mule.api.Transaction#rollback()
108: */
109: public void rollback() throws TransactionException {
110: try {
111: logger.debug("Rolling back transaction");
112: setRollbackOnly();
113: doRollback();
114: fireNotification(new TransactionNotification(this ,
115: TransactionNotification.TRANSACTION_ROLLEDBACK));
116: } finally {
117: TransactionCoordination.getInstance().unbindTransaction(
118: this );
119: }
120: }
121:
122: /**
123: * Really begin the transaction. Note that resources are enlisted yet.
124: *
125: * @throws TransactionException
126: */
127: protected abstract void doBegin() throws TransactionException;
128:
129: /**
130: * Commit the transaction on the underlying resource
131: *
132: * @throws TransactionException
133: */
134: protected abstract void doCommit() throws TransactionException;
135:
136: /**
137: * Rollback the transaction on the underlying resource
138: *
139: * @throws TransactionException
140: */
141: protected abstract void doRollback() throws TransactionException;
142:
143: /**
144: * Fires a server notification to all registered
145: * {@link org.mule.api.context.notification.listener.TransactionNotificationListener}s.
146: *
147: */
148: protected void fireNotification(TransactionNotification notification) {
149: // TODO profile this piece of code
150: MuleServer.getMuleContext().fireNotification(notification);
151: }
152:
153: public boolean isXA() {
154: return false;
155: }
156:
157: public void resume() throws TransactionException {
158: throw new IllegalTransactionStateException(CoreMessages
159: .notMuleXaTransaction(this ));
160: }
161:
162: public javax.transaction.Transaction suspend()
163: throws TransactionException {
164: throw new IllegalTransactionStateException(CoreMessages
165: .notMuleXaTransaction(this));
166: }
167: }
|