001: package org.concern.controller;
002:
003: import org.hibernate.*;
004: import org.apache.commons.logging.LogFactory;
005:
006: import javax.transaction.*;
007: import javax.transaction.Transaction;
008: import javax.transaction.xa.XAResource;
009: import java.util.List;
010: import java.util.Iterator;
011: import java.util.LinkedList;
012:
013: public class NoTransactionManager implements TransactionManager {
014: private static org.apache.commons.logging.Log LOG = LogFactory
015: .getLog(NoTransactionManager.class);
016:
017: private Sessions sessions;
018: private ThreadLocal transactions = new ThreadLocal();
019:
020: public NoTransactionManager() {
021: }
022:
023: public NoTransactionManager(Sessions sessions) {
024: setSessions(sessions);
025: }
026:
027: public void setSessions(Sessions sessions) {
028: this .sessions = sessions;
029: sessions.setTransactionManager(this );
030: }
031:
032: public void setRollbackOnly() throws IllegalStateException,
033: SystemException {
034: getTransaction().setRollbackOnly();
035: }
036:
037: public void rollback() throws IllegalStateException,
038: SecurityException, SystemException {
039: getTransaction().rollback();
040: }
041:
042: public int getStatus() throws SystemException {
043: Transaction transaction = getTransaction();
044: int status;
045: if (transaction != null)
046: status = transaction.getStatus();
047: else
048: status = Status.STATUS_NO_TRANSACTION;
049: return status;
050: }
051:
052: public Transaction getTransaction() throws SystemException {
053: return (Transaction) transactions.get();
054: }
055:
056: public void resume(Transaction transaction)
057: throws InvalidTransactionException, IllegalStateException,
058: SystemException {
059: }
060:
061: public void setTransactionTimeout(int i) {
062: }
063:
064: public Transaction suspend() throws SystemException {
065: return null;
066: }
067:
068: public void commit() throws SecurityException,
069: IllegalStateException, SystemException {
070: try {
071: getTransaction().commit();
072: } catch (SystemException e) {
073: throw e;
074: } catch (Exception e) {
075: e.printStackTrace();
076: throw new SystemException(e.getMessage());
077: }
078: }
079:
080: public void begin() throws SystemException {
081: new NoTransaction();
082: }
083:
084: private class NoTransaction implements Transaction {
085: private int status = Status.STATUS_UNKNOWN;
086: private org.hibernate.Transaction transaction;
087: List synchronizations = new LinkedList();
088:
089: public NoTransaction() throws SystemException {
090: transactions.set(this );
091: try {
092: status = Status.STATUS_ACTIVE;
093: transaction = sessions.getSession().beginTransaction();
094: } catch (HibernateException e) {
095: LOG.error(status, e);
096: throw new SystemException(e.getMessage());
097: }
098: }
099:
100: public void commit() throws RollbackException,
101: HeuristicMixedException, HeuristicRollbackException,
102: SecurityException, IllegalStateException,
103: SystemException {
104: try {
105: if (status != Status.STATUS_ACTIVE)
106: throw new IllegalStateException("status " + status);
107:
108: beforeCompletion();
109: transaction.commit();
110: status = Status.STATUS_COMMITTED;
111: } catch (HibernateException e) {
112: LOG.error(status, e);
113: throw new SystemException(e.getMessage());
114: } finally {
115: afterCompletion();
116: }
117: }
118:
119: public boolean delistResource(XAResource xaResource, int i)
120: throws IllegalStateException, SystemException {
121: return true;
122: }
123:
124: public boolean enlistResource(XAResource xaResource)
125: throws RollbackException, IllegalStateException,
126: SystemException {
127: return true;
128: }
129:
130: public int getStatus() throws SystemException {
131: return status;
132: }
133:
134: public void registerSynchronization(
135: Synchronization synchronization)
136: throws RollbackException, IllegalStateException,
137: SystemException {
138: synchronizations.add(synchronization);
139: }
140:
141: public void rollback() throws IllegalStateException,
142: SystemException {
143: try {
144: transaction.rollback();
145: status = Status.STATUS_ROLLEDBACK;
146: } catch (HibernateException e) {
147: LOG.error(status, e);
148: throw new SystemException(e.getMessage());
149: } finally {
150: afterCompletion();
151: }
152: }
153:
154: private void beforeCompletion() {
155: for (Iterator iterator = synchronizations.iterator(); iterator
156: .hasNext();) {
157: Synchronization synchronization = (Synchronization) iterator
158: .next();
159: synchronization.beforeCompletion();
160: }
161: }
162:
163: private void afterCompletion() {
164: for (Iterator iterator = synchronizations.iterator(); iterator
165: .hasNext();) {
166: Synchronization synchronization = (Synchronization) iterator
167: .next();
168: synchronization.afterCompletion(status);
169: }
170: }
171:
172: public void setRollbackOnly() throws IllegalStateException,
173: SystemException {
174: status = Status.STATUS_MARKED_ROLLBACK;
175: }
176: }
177: }
|