/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.workingset.jdo.lib;
import org.objectweb.perseus.persistence.api.PersistenceException;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.api.SpeedoProperties;
import org.objectweb.speedo.api.SpeedoRuntimeException;
import org.objectweb.speedo.pm.jdo.api.JDOPOManagerItf;
import org.objectweb.speedo.workingset.jdo.api.JDOTransactionItf;
import org.objectweb.speedo.workingset.lib.AbstractTransaction;
import org.objectweb.util.monolog.api.BasicLevel;
import javax.jdo.JDOException;
import javax.jdo.JDOFatalException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.naming.InitialContext;
import javax.transaction.Synchronization;
import javax.transaction.TransactionManager;
/**
* Is a working set assocaited to a POManagerItf. The working set can be
* transactional or not. It contains the list of reached instances.
*
* @see javax.jdo.Transaction
* @see org.objectweb.speedo.workingset.jdo.api.JDOTransactionItf
* @see org.objectweb.perseus.persistence.api.WorkingSet
* @see org.objectweb.perseus.persistence.lib.BasicWorkingSet
*
* @author S.Chassande-Barrioz
*/
public class JDOTransactionImpl
extends AbstractTransaction
implements JDOTransactionItf {
// IMPLEMENTATION OF THE JDOTransactionItf INTERFACE //
//-----------------------------------------------------//
public RuntimeException rollBackOnInternalError(Exception _e) {
Exception ie = ExceptionHelper.getNested(_e);
if (isActive()) {
if (managedEnv) {
logger.log(BasicLevel.INFO, ".");
String tmName = ((JDOPOManagerItf) pm).getPersistenceManagerFactory().getProperties()
.getProperty(SpeedoProperties.TM_NAME);
if (tmName == null) {
return new JDOFatalException("No transaction manager jndi name found in initialisation properties");
}
try {
Object o = new InitialContext().lookup(tmName);
if (o == null) {
String msg = "The transaction must be marked as rollbackOnly: JNDI retrieves a null transaction manager for the name '" + tmName + "'.";
logger.log(BasicLevel.ERROR, msg);
return new JDOFatalException(msg);
}
if (!(o instanceof TransactionManager)) {
String msg = "The transaction must be marked as rollbackOnly: JNDI retrieves an object which is not a javax.transaction.TransactionManager (JNDI name: " + tmName + "): " + o;
logger.log(BasicLevel.ERROR, msg);
return new JDOFatalException(msg);
}
javax.transaction.Transaction t = ((TransactionManager) o).getTransaction();
if (t == null) {
String msg = "Impossible to rollback an unexisting transaction (TM.getTransaction()=null)";
logger.log(BasicLevel.ERROR, msg);
return new JDOFatalException(msg);
}
t.setRollbackOnly();
} catch (Exception e) {
String msg = "The transaction must be marked as rollbackOnly: Error when lookup the transaction manager in JNDI with the name '"
+ tmName + "'";
logger.log(BasicLevel.ERROR, msg, e);
return new JDOFatalException(msg);
}
} else {
rollback();
}
return new JDOFatalException("The current transaction has been rolledback, please retry", ie);
} else {
return new JDOFatalException("The current working set occrurs an error, please retry", ie);
}
}
public void beforeWSPrepare() throws PersistenceException {
try {
super.beforeWSPrepare();
} catch(SpeedoRuntimeException e) {
Exception ne;
if (e.causes != null) {
ne = new JDOUserException(e.getMessage(), e.causes);
} else if (e.getCause() != null) {
ne = new JDOUserException(e.getMessage(), e.getCause());
} else {
ne = new JDOUserException(e.getMessage());
}
throw new PersistenceException(ne);
}
}
public void onWSEnd() {
try {
super.onWSEnd();
} catch(SpeedoRuntimeException e) {
if (e.causes != null) {
throw new JDOUserException(e.getMessage(), e.causes);
} else if (e.getCause() != null) {
throw new JDOUserException(e.getMessage(), e.getCause());
} else {
throw new JDOUserException(e.getMessage());
}
}
}
// IMPLEMENTATION OF THE javax.jdo.Transactionn INTERFACE //
//--------------------------------------------------------//
public void begin() {
logger.log(BasicLevel.INFO, "Begin the transaction");
try {
super.begin();
} catch (SpeedoRuntimeException e) {
throw new JDOException(e.getMessage(), e.getCause());
}
}
public void commit() {
try {
super.commit();
} catch (SpeedoRuntimeException e) {
if (e.causes != null && e.causes.length == 1
&& e.causes[0] instanceof JDOException) {
throw (JDOException) e.causes[0];
}
throw new JDOFatalException(e.getMessage(), e);
}
}
public void rollback() {
try {
super.rollback();
} catch (SpeedoRuntimeException e) {
throw new JDOException(e.getMessage(), e);
}
}
public void setNontransactionalRead(boolean b) {
nontransactionalRead = b;
}
public boolean getNontransactionalRead() {
return nontransactionalRead;
}
public void setNontransactionalWrite(boolean b) {
nontransactionalWrite = b;
}
public boolean getNontransactionalWrite() {
return nontransactionalWrite;
}
public void setRetainValues(boolean b) {
setWSRetainValues(b);
}
public boolean getRetainValues() {
return getWSRetainValues();
}
public void setRestoreValues(boolean b) {
setWSRestoreValues(b);
}
public boolean getRestoreValues() {
return getWSRestoreValues();
}
public void setOptimistic(boolean b) {
optimistic = b;
}
public boolean getOptimistic() {
return optimistic;
}
public void setSynchronization(Synchronization s) {
synchronization = s;
}
public Synchronization getSynchronization() {
return synchronization;
}
public PersistenceManager getPersistenceManager() {
return (JDOPOManagerItf) pm;
}
}