use of javax.transaction.RollbackException in project tomee by apache.
the class BeanTxStatefulBean method openAccount.
public void openAccount(final Account acct, final Boolean rollback) throws RemoteException, RollbackException {
try {
final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/datasource");
final Connection con = ds.getConnection();
try {
final UserTransaction ut = ejbContext.getUserTransaction();
/*[1] Begin the transaction */
ut.begin();
/*[2] Update the table */
final PreparedStatement stmt = con.prepareStatement("insert into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
try {
stmt.setString(1, acct.getSsn());
stmt.setString(2, acct.getFirstName());
stmt.setString(3, acct.getLastName());
stmt.setInt(4, acct.getBalance());
stmt.executeUpdate();
} finally {
stmt.close();
}
/*[3] Commit or Rollback the transaction */
if (rollback.booleanValue())
ut.setRollbackOnly();
/*[4] Commit or Rollback the transaction */
ut.commit();
} finally {
con.close();
}
} catch (final RollbackException re) {
throw re;
} catch (final Exception e) {
e.printStackTrace();
throw new RemoteException("[Bean] " + e.getClass().getName() + " : " + e.getMessage());
}
}
use of javax.transaction.RollbackException in project tomee by apache.
the class SingletonBeanTxTests method test05_singleTransactionCommit.
/**
*
*/
public void test05_singleTransactionCommit() {
try {
final Account expected = new Account("123-45-6789", "Joe", "Cool", 40000);
Account actual = new Account();
ejbObject.openAccount(expected, Boolean.FALSE);
actual = ejbObject.retreiveAccount(expected.getSsn());
assertNotNull("The transaction was not commited. The record is null", actual);
assertEquals("The transaction was not commited cleanly.", expected, actual);
} catch (final RollbackException re) {
fail("Transaction was rolledback. Received Exception " + re.getClass() + " : " + re.getMessage());
} catch (final Exception e) {
fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
}
Aggregations