001: package org.jbpm.persistence.jta;
002:
003: import javax.naming.InitialContext;
004: import javax.transaction.SystemException;
005: import javax.transaction.UserTransaction;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.hibernate.engine.SessionFactoryImplementor;
010: import org.hibernate.util.JTAHelper;
011: import org.jbpm.JbpmContext;
012: import org.jbpm.JbpmException;
013: import org.jbpm.persistence.db.DbPersistenceService;
014: import org.jbpm.persistence.db.DbPersistenceServiceFactory;
015:
016: public class JtaDbPersistenceService extends DbPersistenceService {
017:
018: private static final long serialVersionUID = 1L;
019:
020: private static Log log = LogFactory.getLog(JbpmContext.class);
021:
022: boolean isJtaTxCreated = false;
023:
024: public JtaDbPersistenceService(
025: DbPersistenceServiceFactory persistenceServiceFactory) {
026: super (persistenceServiceFactory);
027:
028: if (!isCurrentJtaTransactionAvailable()) {
029: beginJtaTransaction();
030: isJtaTxCreated = true;
031: }
032: }
033:
034: public void close() {
035: super .close();
036:
037: if (isJtaTxCreated) {
038: endJtaTransaction();
039: }
040: }
041:
042: boolean isCurrentJtaTransactionAvailable() {
043: SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) persistenceServiceFactory
044: .getSessionFactory();
045: return JTAHelper
046: .isTransactionInProgress(sessionFactoryImplementor);
047: }
048:
049: void beginJtaTransaction() {
050: try {
051: log.debug("start user JTA transaction");
052: getUserTransaction().begin();
053: } catch (Exception e) {
054: throw new JbpmException("couldn't start JTA transaction", e);
055: }
056: }
057:
058: void endJtaTransaction() {
059: int status = -1;
060: log.debug("end user JTA transaction");
061: UserTransaction userTransaction = getUserTransaction();
062: try {
063: status = userTransaction.getStatus();
064: } catch (SystemException e) {
065: throw new JbpmException(
066: "couldn't get status for user transaction", e);
067: }
068:
069: boolean isRollback = JTAHelper.isRollback(status);
070: if (isRollback) {
071: log.debug("end jta transation with ROLLBACK");
072: try {
073: userTransaction.rollback();
074: } catch (Exception e) {
075: throw new JbpmException(
076: "couldn't rollback JTA transaction", e);
077: }
078: } else {
079: log.debug("end jta transation with COMMIT");
080: try {
081: userTransaction.commit();
082: } catch (Exception e) {
083: throw new JbpmException(
084: "couldn't commit JTA transaction", e);
085: }
086: }
087: }
088:
089: UserTransaction getUserTransaction() {
090: UserTransaction userTransaction = null;
091: if (userTransaction == null) {
092: String jndiName = "UserTransaction";
093: try {
094: userTransaction = (UserTransaction) new InitialContext()
095: .lookup(jndiName);
096: } catch (Exception e) {
097: throw new JbpmException(
098: "couldn't lookup UserTransaction in JNDI with name "
099: + jndiName, e);
100: }
101: }
102: return userTransaction;
103: }
104:
105: public boolean isJtaTxCreated() {
106: return isJtaTxCreated;
107: }
108:
109: public void setJtaTxCreated(boolean isJtaTxCreated) {
110: this.isJtaTxCreated = isJtaTxCreated;
111: }
112: }
|