001: /**
002: * Copyright (C) 2001-2005 France Telecom R&D
003: */package org.objectweb.speedo.workingset.ejb.lib;
004:
005: import org.objectweb.speedo.api.ExceptionHelper;
006: import org.objectweb.speedo.api.SpeedoProperties;
007: import org.objectweb.speedo.api.SpeedoRuntimeException;
008: import org.objectweb.speedo.pm.jdo.api.JDOPOManagerItf;
009: import org.objectweb.speedo.workingset.ejb.api.EJBTransactionItf;
010: import org.objectweb.speedo.workingset.lib.AbstractTransaction;
011: import org.objectweb.util.monolog.api.BasicLevel;
012:
013: import javax.persistence.PersistenceException;
014: import javax.naming.InitialContext;
015: import javax.transaction.TransactionManager;
016:
017: /**
018: * Implements the EJB transaction API since the generic speedo implementation.
019: *
020: * @author S.Chassande-Barrioz
021: */
022: public class EJBTransactionImpl extends AbstractTransaction implements
023: EJBTransactionItf {
024:
025: public EJBTransactionImpl() {
026: super ();
027: }
028:
029: public void beforeWSPrepare()
030: throws org.objectweb.perseus.persistence.api.PersistenceException {
031: try {
032: super .beforeWSPrepare();
033: } catch (SpeedoRuntimeException e) {
034: Exception ne;
035: if (e.getCause() != null) {
036: ne = new PersistenceException(e.getMessage(), e
037: .getCause());
038: } else {
039: ne = new PersistenceException(e.getMessage());
040: }
041: throw new org.objectweb.perseus.persistence.api.PersistenceException(
042: ne);
043: }
044: }
045:
046: public void onWSEnd() {
047: try {
048: super .onWSEnd();
049: } catch (SpeedoRuntimeException e) {
050: if (e.getCause() != null) {
051: throw new PersistenceException(e.getMessage(), e
052: .getCause());
053: } else {
054: throw new PersistenceException(e.getMessage());
055: }
056: }
057: }
058:
059: public void begin() {
060: logger.log(BasicLevel.INFO, "Begin the transaction");
061: try {
062: super .begin();
063: } catch (SpeedoRuntimeException e) {
064: throw new PersistenceException(e.getMessage(), e.getCause());
065: }
066: }
067:
068: public void commit() {
069: try {
070: super .commit();
071: } catch (SpeedoRuntimeException e) {
072: throw new PersistenceException(e.getMessage(), e);
073: }
074: }
075:
076: public void rollback() {
077: try {
078: super .commit();
079: } catch (SpeedoRuntimeException e) {
080: throw new PersistenceException(e.getMessage(), e);
081: }
082: }
083:
084: public RuntimeException rollBackOnInternalError(Exception _e) {
085: Exception ie = ExceptionHelper.getNested(_e);
086: if (isActive()) {
087: if (managedEnv) {
088: logger.log(BasicLevel.INFO, ".");
089: String tmName = ((JDOPOManagerItf) pm)
090: .getPersistenceManagerFactory().getProperties()
091: .getProperty(SpeedoProperties.TM_NAME);
092: if (tmName == null) {
093: return new PersistenceException(
094: "No transaction manager jndi name found in initialisation properties");
095: }
096: try {
097: Object o = new InitialContext().lookup(tmName);
098: if (o == null) {
099: String msg = "The transaction must be marked as rollbackOnly: JNDI retrieves a null transaction manager for the name '"
100: + tmName + "'.";
101: logger.log(BasicLevel.ERROR, msg);
102: return new PersistenceException(msg);
103: }
104: if (!(o instanceof TransactionManager)) {
105: String msg = "The transaction must be marked as rollbackOnly: JNDI retrieves an object which is not a javax.transaction.TransactionManager (JNDI name: "
106: + tmName + "): " + o;
107: logger.log(BasicLevel.ERROR, msg);
108: return new PersistenceException(msg);
109: }
110: javax.transaction.Transaction t = ((TransactionManager) o)
111: .getTransaction();
112: if (t == null) {
113: String msg = "Impossible to rollback an unexisting transaction (TM.getTransaction()=null)";
114: logger.log(BasicLevel.ERROR, msg);
115: return new PersistenceException(msg);
116: }
117: t.setRollbackOnly();
118: } catch (Exception e) {
119: String msg = "The transaction must be marked as rollbackOnly: Error when lookup the transaction manager in JNDI with the name '"
120: + tmName + "'";
121: logger.log(BasicLevel.ERROR, msg, e);
122: return new PersistenceException(msg);
123: }
124: } else {
125: rollback();
126: }
127: return new PersistenceException(
128: "The current transaction has been rolledback, please retry",
129: ie);
130: } else {
131: return new PersistenceException(
132: "The current working set occrurs an error, please retry",
133: ie);
134: }
135: }
136:
137: }
|