001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.workingset.jdo.lib;
018:
019: import org.objectweb.perseus.persistence.api.PersistenceException;
020: import org.objectweb.speedo.api.ExceptionHelper;
021: import org.objectweb.speedo.api.SpeedoProperties;
022: import org.objectweb.speedo.api.SpeedoRuntimeException;
023: import org.objectweb.speedo.pm.jdo.api.JDOPOManagerItf;
024: import org.objectweb.speedo.workingset.jdo.api.JDOTransactionItf;
025: import org.objectweb.speedo.workingset.lib.AbstractTransaction;
026: import org.objectweb.util.monolog.api.BasicLevel;
027:
028: import javax.jdo.JDOException;
029: import javax.jdo.JDOFatalException;
030: import javax.jdo.JDOUserException;
031: import javax.jdo.PersistenceManager;
032: import javax.naming.InitialContext;
033: import javax.transaction.Synchronization;
034: import javax.transaction.TransactionManager;
035:
036: /**
037: * Is a working set assocaited to a POManagerItf. The working set can be
038: * transactional or not. It contains the list of reached instances.
039: *
040: * @see javax.jdo.Transaction
041: * @see org.objectweb.speedo.workingset.jdo.api.JDOTransactionItf
042: * @see org.objectweb.perseus.persistence.api.WorkingSet
043: * @see org.objectweb.perseus.persistence.lib.BasicWorkingSet
044: *
045: * @author S.Chassande-Barrioz
046: */
047: public class JDOTransactionImpl extends AbstractTransaction implements
048: JDOTransactionItf {
049:
050: // IMPLEMENTATION OF THE JDOTransactionItf INTERFACE //
051: //-----------------------------------------------------//
052:
053: public RuntimeException rollBackOnInternalError(Exception _e) {
054: Exception ie = ExceptionHelper.getNested(_e);
055: if (isActive()) {
056: if (managedEnv) {
057: logger.log(BasicLevel.INFO, ".");
058: String tmName = ((JDOPOManagerItf) pm)
059: .getPersistenceManagerFactory().getProperties()
060: .getProperty(SpeedoProperties.TM_NAME);
061: if (tmName == null) {
062: return new JDOFatalException(
063: "No transaction manager jndi name found in initialisation properties");
064: }
065: try {
066: Object o = new InitialContext().lookup(tmName);
067: if (o == null) {
068: String msg = "The transaction must be marked as rollbackOnly: JNDI retrieves a null transaction manager for the name '"
069: + tmName + "'.";
070: logger.log(BasicLevel.ERROR, msg);
071: return new JDOFatalException(msg);
072: }
073: if (!(o instanceof TransactionManager)) {
074: String msg = "The transaction must be marked as rollbackOnly: JNDI retrieves an object which is not a javax.transaction.TransactionManager (JNDI name: "
075: + tmName + "): " + o;
076: logger.log(BasicLevel.ERROR, msg);
077: return new JDOFatalException(msg);
078: }
079: javax.transaction.Transaction t = ((TransactionManager) o)
080: .getTransaction();
081: if (t == null) {
082: String msg = "Impossible to rollback an unexisting transaction (TM.getTransaction()=null)";
083: logger.log(BasicLevel.ERROR, msg);
084: return new JDOFatalException(msg);
085: }
086: t.setRollbackOnly();
087: } catch (Exception e) {
088: String msg = "The transaction must be marked as rollbackOnly: Error when lookup the transaction manager in JNDI with the name '"
089: + tmName + "'";
090: logger.log(BasicLevel.ERROR, msg, e);
091: return new JDOFatalException(msg);
092: }
093: } else {
094: rollback();
095: }
096: return new JDOFatalException(
097: "The current transaction has been rolledback, please retry",
098: ie);
099: } else {
100: return new JDOFatalException(
101: "The current working set occrurs an error, please retry",
102: ie);
103: }
104: }
105:
106: public void beforeWSPrepare() throws PersistenceException {
107: try {
108: super .beforeWSPrepare();
109: } catch (SpeedoRuntimeException e) {
110: Exception ne;
111: if (e.causes != null) {
112: ne = new JDOUserException(e.getMessage(), e.causes);
113: } else if (e.getCause() != null) {
114: ne = new JDOUserException(e.getMessage(), e.getCause());
115: } else {
116: ne = new JDOUserException(e.getMessage());
117: }
118: throw new PersistenceException(ne);
119: }
120: }
121:
122: public void onWSEnd() {
123: try {
124: super .onWSEnd();
125: } catch (SpeedoRuntimeException e) {
126: if (e.causes != null) {
127: throw new JDOUserException(e.getMessage(), e.causes);
128: } else if (e.getCause() != null) {
129: throw new JDOUserException(e.getMessage(), e.getCause());
130: } else {
131: throw new JDOUserException(e.getMessage());
132: }
133: }
134: }
135:
136: // IMPLEMENTATION OF THE javax.jdo.Transactionn INTERFACE //
137: //--------------------------------------------------------//
138:
139: public void begin() {
140: logger.log(BasicLevel.INFO, "Begin the transaction");
141: try {
142: super .begin();
143: } catch (SpeedoRuntimeException e) {
144: throw new JDOException(e.getMessage(), e.getCause());
145: }
146: }
147:
148: public void commit() {
149: try {
150: super .commit();
151: } catch (SpeedoRuntimeException e) {
152: if (e.causes != null && e.causes.length == 1
153: && e.causes[0] instanceof JDOException) {
154: throw (JDOException) e.causes[0];
155: }
156: throw new JDOFatalException(e.getMessage(), e);
157: }
158: }
159:
160: public void rollback() {
161: try {
162: super .rollback();
163: } catch (SpeedoRuntimeException e) {
164: throw new JDOException(e.getMessage(), e);
165: }
166: }
167:
168: public void setNontransactionalRead(boolean b) {
169: nontransactionalRead = b;
170: }
171:
172: public boolean getNontransactionalRead() {
173: return nontransactionalRead;
174: }
175:
176: public void setNontransactionalWrite(boolean b) {
177: nontransactionalWrite = b;
178: }
179:
180: public boolean getNontransactionalWrite() {
181: return nontransactionalWrite;
182: }
183:
184: public void setRetainValues(boolean b) {
185: setWSRetainValues(b);
186: }
187:
188: public boolean getRetainValues() {
189: return getWSRetainValues();
190: }
191:
192: public void setRestoreValues(boolean b) {
193: setWSRestoreValues(b);
194: }
195:
196: public boolean getRestoreValues() {
197: return getWSRestoreValues();
198: }
199:
200: public void setOptimistic(boolean b) {
201: optimistic = b;
202: }
203:
204: public boolean getOptimistic() {
205: return optimistic;
206: }
207:
208: public void setSynchronization(Synchronization s) {
209: synchronization = s;
210: }
211:
212: public Synchronization getSynchronization() {
213: return synchronization;
214: }
215:
216: public PersistenceManager getPersistenceManager() {
217: return (JDOPOManagerItf) pm;
218: }
219: }
|