When involving several calls to your DAO methods in a single transactions, here’s a great place to start: Java Persistence/Transactions
Here’s a small stub for kick-off:
UserTransaction tr = null;
try {
tr = (UserTransaction) new InitialContext().lookup("javax.transaction.UserTransaction");
tr.begin();
bean.op1();
bean.op2();
// ...
bean.opN();
tr.commit();
} catch (Exception e) {
logErrorMessage(e); // log error message
if (tr != null) {
try {
tr.rollback();
} catch (Exception e1) {
logErrorMessage(e); // log error message
}
}
}
try {
tr = (UserTransaction) new InitialContext().lookup("javax.transaction.UserTransaction");
tr.begin();
bean.op1();
bean.op2();
// ...
bean.opN();
tr.commit();
} catch (Exception e) {
logErrorMessage(e); // log error message
if (tr != null) {
try {
tr.rollback();
} catch (Exception e1) {
logErrorMessage(e); // log error message
}
}
}