final boolean wrapInTransaction = wrapInTransaction();
if ( wrapInTransaction ) {
TransactionManager transactionManager = factory.getTransactionManager();
try {
final Session session;
final StatelessSession statelessSession;
if ( sessionAwareRunnable != null ) {
session = factory.openSession();
statelessSession = null;
}
else {
session = null;
statelessSession = factory.openStatelessSession();
}
transactionManager.begin();
if ( sessionAwareRunnable != null ) {
sessionAwareRunnable.run( session );
}
else {
statelessSessionAwareRunnable.run( statelessSession );
}
transactionManager.commit();
if ( sessionAwareRunnable != null ) {
session.close();
}
else {
statelessSession.close();
}
}
catch (Throwable e) {
//TODO exception handling seems messy-ish
log.error( "Error while executing runnable wrapped in a JTA transaction", e );