public Transaction getUserTransaction() throws RemoteException { UserTransaction ut = null; try { ut = ejbContext.getUserTransaction(); } catch (IllegalStateException ise) { throw new RemoteException(ise.getMessage()); } if (ut == null) return null; return new Transaction(ut); }
public void openAccount(Account acct, Boolean rollback) throws RemoteException, RollbackException { try { DataSource ds = (DataSource) javax.rmi.PortableRemoteObject.narrow( jndiContext.lookup("java:comp/env/database"), DataSource.class); Connection con = ds.getConnection(); try { UserTransaction ut = ejbContext.getUserTransaction(); /*[1] Begin the transaction */ ut.begin(); /*[2] Update the table */ 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 (RollbackException re) { throw re; } catch (Exception e) { e.printStackTrace(); throw new RemoteException("[Bean] " + e.getClass().getName() + " : " + e.getMessage()); } }