Search in sources :

Example 1 with TransactionRolledBackException

use of org.osgi.service.transaction.control.TransactionRolledBackException in project aries by apache.

the class XAJPATransactionTest method testTwoPhaseRollback.

@Test
public void testTwoPhaseRollback() throws Exception {
    Object m1 = getMessageEntityFrom(XA_TEST_UNIT_1);
    Object m2 = getMessageEntityFrom(XA_TEST_UNIT_2);
    Object m3 = getMessageEntityFrom(XA_TEST_UNIT_2);
    try {
        txControl.required(() -> {
            setMessage(m1, "Hello World!");
            em1.persist(m1);
            setMessage(m2, "Hello 1!");
            em2.persist(m2);
            txControl.requiresNew(() -> {
                setMessage(m3, "Hello 2!");
                em2.persist(m3);
                return null;
            });
            txControl.getCurrentContext().registerXAResource(new PoisonResource(), null);
            return null;
        });
        fail("Should roll back");
    } catch (TransactionRolledBackException trbe) {
    }
    assertEquals(0, (int) txControl.notSupported(() -> {
        CriteriaBuilder cb = em1.getCriteriaBuilder();
        CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
        countQuery.select(cb.count(countQuery.from(m1.getClass())));
        return em1.createQuery(countQuery).getSingleResult().intValue();
    }));
    assertEquals(Arrays.asList("Hello 2!"), txControl.notSupported(() -> {
        CriteriaBuilder cb = em2.getCriteriaBuilder();
        CriteriaQuery<String> query = cb.createQuery(String.class);
        query.select(query.from(m2.getClass()).get("message"));
        return em2.createQuery(query).getResultList();
    }));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TransactionRolledBackException(org.osgi.service.transaction.control.TransactionRolledBackException) Test(org.junit.Test)

Example 2 with TransactionRolledBackException

use of org.osgi.service.transaction.control.TransactionRolledBackException in project aries by apache.

the class XATransactionTest method testTwoPhaseRollback.

@Test
public void testTwoPhaseRollback() {
    try {
        txControl.required(() -> {
            connection1.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
            connection2.createStatement().execute("Insert into TEST_TABLE values ( 'Hello 1' )");
            txControl.requiresNew(() -> {
                connection2.createStatement().execute("Insert into TEST_TABLE values ( 'Hello 2' )");
                return null;
            });
            txControl.getCurrentContext().registerXAResource(new PoisonResource(), null);
            return null;
        });
        fail("Should roll back");
    } catch (TransactionRolledBackException trbe) {
    }
    assertEquals(0, (int) txControl.notSupported(() -> {
        ResultSet rs = connection1.createStatement().executeQuery("Select count(*) from TEST_TABLE");
        rs.next();
        return rs.getInt(1);
    }));
    assertEquals("1: Hello 2", txControl.notSupported(() -> {
        Statement s = connection2.createStatement();
        ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
        rs.next();
        int count = rs.getInt(1);
        rs = s.executeQuery("Select idValue from TEST_TABLE ORDER BY idValue");
        rs.next();
        return "" + count + ": " + rs.getString(1);
    }));
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) TransactionRolledBackException(org.osgi.service.transaction.control.TransactionRolledBackException) Test(org.junit.Test)

Example 3 with TransactionRolledBackException

use of org.osgi.service.transaction.control.TransactionRolledBackException in project aries by apache.

the class TransactionLogTest method doRecoveryRequired.

public void doRecoveryRequired(BiConsumer<XAResource, XAResource> ordering, TransactionStatus expectedFinalState) throws Exception {
    //Register the recoverable resource
    ArgumentCaptor<ServiceListener> captor = ArgumentCaptor.forClass(ServiceListener.class);
    Mockito.verify(ctx).addServiceListener(captor.capture(), Mockito.anyString());
    Mockito.when(ctx.getService(serviceRef)).thenReturn(new TestRecoverableResource("foo", dataSource));
    captor.getValue().serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
    XAConnection xaConn = dataSource.getXAConnection();
    AtomicReference<TransactionStatus> ref = new AtomicReference<TransactionStatus>();
    try {
        txControl.required(() -> {
            txControl.getCurrentContext().postCompletion(ref::set);
            Connection conn = xaConn.getConnection();
            // conn.setAutoCommit(false);
            XAResource dsResource = xaConn.getXAResource();
            XAResource poison = Mockito.mock(XAResource.class);
            Mockito.when(poison.prepare(Mockito.any())).thenAnswer(i -> {
                conn.createStatement().execute("shutdown immediately");
                Thread.sleep(1000);
                return XA_OK;
            });
            ordering.accept(dsResource, poison);
            return conn.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )");
        });
    } catch (TransactionException te) {
        assertEquals(expectedFinalState, ref.get());
        assertEquals(expectedFinalState == ROLLED_BACK, te instanceof TransactionRolledBackException);
    } finally {
        try {
            xaConn.close();
        } catch (SQLException sqle) {
        }
    }
    setupServerAndDataSource();
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) AtomicReference(java.util.concurrent.atomic.AtomicReference) XAResource(javax.transaction.xa.XAResource) RecoverableXAResource(org.osgi.service.transaction.control.recovery.RecoverableXAResource) TransactionException(org.osgi.service.transaction.control.TransactionException) ServiceEvent(org.osgi.framework.ServiceEvent) TransactionRolledBackException(org.osgi.service.transaction.control.TransactionRolledBackException) XAConnection(javax.sql.XAConnection)

Aggregations

TransactionRolledBackException (org.osgi.service.transaction.control.TransactionRolledBackException)3 Test (org.junit.Test)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 XAConnection (javax.sql.XAConnection)1 XAResource (javax.transaction.xa.XAResource)1 ServiceEvent (org.osgi.framework.ServiceEvent)1 ServiceListener (org.osgi.framework.ServiceListener)1 TransactionException (org.osgi.service.transaction.control.TransactionException)1 TransactionStatus (org.osgi.service.transaction.control.TransactionStatus)1 RecoverableXAResource (org.osgi.service.transaction.control.recovery.RecoverableXAResource)1