我在类中使用 Bean 管理的事务@ManagedBean
来手动管理事务,
@ManagedBean(name = "clients")
@ViewScoped
@URLMapping(id = "sousc", pattern = "/ccf_sicavs_customers", viewId = "/customers/PgSouscripteurs.jsf")
@TransactionManagement(TransactionManagementType.BEAN)
public class SouscripteursBean extends Referentiel implements Serializable {
@Resource
private UserTransaction ut;
@EJB
private CustomerDaoLocal ejbCust;
public void createCustomer() {
try {
LOGGER.info("Debut de la transaction");
ut.begin();
LOGGER.info("begin() succeded");
currentMorale.setCodeSouscripteur("25");
saved=ejbCust.createCustomer(currentMorale);
ut.commit();
LOGGER.info("commit() succeded");
} catch (Exception e) {
saved=false;
try {
ut.rollback();
LOGGER.info("rollback() succeded");
} catch (Exception ex) {
LOGGER.log(Level.SEVERE,null,ex);
}
}
}
}
但是如果exception
在 之前抛出an commit()
,则rollback()
失败并抛出这个java.lang.IllegalStateException
:
SEVERE: java.lang.IllegalStateException: Transaction is not active in the current thread.
at com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate.validateTransactionManager(JavaEETransactionManagerJTSDelegate.java:447)
at com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate.rollbackDistributedTransaction(JavaEETransactionManagerJTSDelegate.java:208)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.rollback(JavaEETransactionManagerSimplified.java:899)
at com.sun.enterprise.transaction.UserTransactionImpl.rollback(UserTransactionImpl.java:234)