Search in sources :

Example 21 with RollbackException

use of javax.transaction.RollbackException in project tomee by apache.

the class BeanTxStatelessBean method openAccount.

public void openAccount(final Account acct, final Boolean rollback) throws RemoteException, RollbackException {
    try {
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
        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());
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) RollbackException(javax.transaction.RollbackException) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) RollbackException(javax.transaction.RollbackException) CreateException(javax.ejb.CreateException) DataSource(javax.sql.DataSource)

Example 22 with RollbackException

use of javax.transaction.RollbackException in project tomee by apache.

the class ContainerTxStatelessBean method openAccount.

public void openAccount(final Account acct, final Boolean rollback) throws RollbackException {
    try {
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
        final Connection con = ds.getConnection();
        try {
            /*[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();
            }
        } finally {
            con.close();
        }
    } catch (final Exception e) {
    //throw new RemoteException("[Bean] "+e.getClass().getName()+" : "+e.getMessage());
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) EJBException(javax.ejb.EJBException) RollbackException(javax.transaction.RollbackException) CreateException(javax.ejb.CreateException) RemoteException(java.rmi.RemoteException) DataSource(javax.sql.DataSource)

Example 23 with RollbackException

use of javax.transaction.RollbackException in project tomee by apache.

the class BeanTxSingletonBean method openAccount.

public void openAccount(final Account acct, final Boolean rollback) throws RemoteException, RollbackException {
    try {
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
        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());
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) RollbackException(javax.transaction.RollbackException) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) RollbackException(javax.transaction.RollbackException) CreateException(javax.ejb.CreateException) DataSource(javax.sql.DataSource)

Example 24 with RollbackException

use of javax.transaction.RollbackException in project tomee by apache.

the class StatefulBeanTxTests 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());
    }
}
Also used : Account(org.apache.openejb.test.object.Account) RollbackException(javax.transaction.RollbackException) RollbackException(javax.transaction.RollbackException)

Example 25 with RollbackException

use of javax.transaction.RollbackException in project hazelcast by hazelcast.

the class ClientXATest method testRollbackOnTimeout.

@Test
public void testRollbackOnTimeout() throws Exception {
    Hazelcast.newHazelcastInstance();
    HazelcastInstance client = HazelcastClient.newHazelcastClient();
    String name = randomString();
    IQueue<Object> queue = client.getQueue(name);
    queue.offer(randomString());
    HazelcastXAResource xaResource = client.getXAResource();
    tm.setTransactionTimeout(3);
    tm.begin();
    Transaction transaction = tm.getTransaction();
    transaction.enlistResource(xaResource);
    TransactionContext context = xaResource.getTransactionContext();
    try {
        context.getQueue(name).take();
        sleepAtLeastSeconds(5);
        tm.commit();
        fail();
    } catch (RollbackException ignored) {
    // Transaction already rolled-back due to timeout, no need to call tm.rollback explicitly
    }
    assertEquals("Queue size should be 1", 1, queue.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Transaction(javax.transaction.Transaction) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) RollbackException(javax.transaction.RollbackException) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Aggregations

RollbackException (javax.transaction.RollbackException)57 SystemException (javax.transaction.SystemException)25 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)15 Transaction (javax.transaction.Transaction)14 UserTransaction (javax.transaction.UserTransaction)14 HeuristicMixedException (javax.transaction.HeuristicMixedException)12 Test (org.junit.Test)11 Connection (java.sql.Connection)8 XAException (javax.transaction.xa.XAException)8 PreparedStatement (java.sql.PreparedStatement)7 NotSupportedException (javax.transaction.NotSupportedException)7 IOException (java.io.IOException)6 EJBException (javax.ejb.EJBException)6 Account (org.apache.openejb.test.object.Account)6 RemoteException (java.rmi.RemoteException)5 CreateException (javax.ejb.CreateException)5 DataSource (javax.sql.DataSource)5 SQLException (java.sql.SQLException)4 Synchronization (javax.transaction.Synchronization)4 XAResource (javax.transaction.xa.XAResource)4