001: package org.concern.controller;
002:
003: import org.concern.ControllerException;
004: import org.apache.commons.logging.LogFactory;
005:
006: import javax.transaction.*;
007: import java.util.Stack;
008:
009: public class Transactions {
010: private static org.apache.commons.logging.Log LOG = LogFactory
011: .getLog(Transactions.class);
012:
013: public static final int REQUIRED = 1;
014: public static final int MANDATORY = 2;
015: public static final int NEVER = 3;
016:
017: private ThreadLocal currentStatus = new ThreadLocal();
018: private int countRequired = 0;
019: private int countMandatory = 0;
020: private int countNever = 0;
021: private int count = 0;
022: private TransactionManager transactionManager;
023:
024: public Transactions() {
025: }
026:
027: public Transactions(TransactionManager transactionManager) {
028: this .transactionManager = transactionManager;
029: }
030:
031: public void setTransactionManager(
032: TransactionManager transactionManager) {
033: this .transactionManager = transactionManager;
034: }
035:
036: public void setRollbackOnly() {
037: try {
038: transactionManager.setRollbackOnly();
039: } catch (SystemException e) {
040: LOG.error("Transaction Propagation Nesting: " + count, e);
041: throw new ControllerException(e.getMessage());
042: }
043: }
044:
045: private Stack getStatusStack() {
046: Stack stack = (Stack) currentStatus.get();
047: if (stack == null)
048: currentStatus.set(stack = new Stack());
049: return stack;
050: }
051:
052: public void beginRequired() throws ControllerException {
053: count++;
054: countRequired++;
055: try {
056: int status = transactionManager.getStatus();
057: getStatusStack().push(new Integer(status));
058: if (status != Status.STATUS_ACTIVE) {
059: LOG.trace("TRANSACTION BEGIN "
060: + Thread.currentThread().getName());
061: transactionManager.begin();
062: }
063: } catch (Exception e) {
064: LOG.error(count, e);
065: throw new ControllerException(e.getMessage());
066: }
067: }
068:
069: public void endRequired() throws ControllerException {
070: countRequired--;
071: try {
072: int status = ((Integer) getStatusStack().pop()).intValue();
073: if (status != Status.STATUS_ACTIVE)
074: end();
075: } catch (Exception e) {
076: LOG.error(count, e);
077: throw new ControllerException(e.getMessage());
078: }
079: }
080:
081: public void beginMandatory() throws ControllerException {
082: count++;
083: countMandatory++;
084: try {
085: int status = transactionManager.getStatus();
086: if (status != Status.STATUS_ACTIVE)
087: throw new ControllerException("transaction required");
088: } catch (Exception e) {
089: LOG.error(count, e);
090: throw new ControllerException(e.getMessage());
091: }
092: }
093:
094: public void endMandatory() {
095: count--;
096: countMandatory--;
097: }
098:
099: public void beginNever() throws ControllerException {
100: count++;
101: countNever++;
102: try {
103: int status = transactionManager.getStatus();
104: if (status == Status.STATUS_ACTIVE)
105: throw new ControllerException("transaction not allowed");
106:
107: transactionManager.begin();
108: } catch (Exception e) {
109: LOG.error(count, e);
110: throw new ControllerException(e.getMessage());
111: }
112: }
113:
114: public void endNever() {
115: countNever--;
116: try {
117: end();
118: } catch (Exception e) {
119: LOG.error(count, e);
120: throw new ControllerException(e.getMessage());
121: }
122: }
123:
124: private void end() throws SystemException, HeuristicMixedException,
125: HeuristicRollbackException, RollbackException {
126: count--;
127: if (transactionManager.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
128: LOG.trace("TRANSACTION ROLLBACK "
129: + Thread.currentThread().getName());
130: transactionManager.rollback();
131: } else {
132: LOG.trace("TRANSACTION COMMIT "
133: + Thread.currentThread().getName());
134: transactionManager.commit();
135: }
136: }
137:
138: public void begin() {
139: count++;
140: try {
141: LOG.trace("TRANSACTION BEGIN "
142: + Thread.currentThread().getName());
143: transactionManager.begin();
144: } catch (Exception e) {
145: LOG.error(count, e);
146: throw new ControllerException(e.getMessage());
147: }
148: }
149:
150: public void commit() {
151: count--;
152: try {
153: LOG.trace("TRANSACTION COMMIT "
154: + Thread.currentThread().getName());
155: transactionManager.commit();
156: } catch (Exception e) {
157: LOG.error(count, e);
158: throw new ControllerException(e.getMessage());
159: }
160: }
161:
162: public void rollback() {
163: count--;
164: try {
165: LOG.trace("TRANSACTION ROLLBACK "
166: + Thread.currentThread().getName());
167: transactionManager.rollback();
168: } catch (Exception e) {
169: LOG.error(count, e);
170: throw new ControllerException(e.getMessage());
171: }
172: }
173:
174: public int getPropagationNesting() {
175: return count;
176: }
177: }
|