Search in sources :

Example 11 with ScopedWorkException

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

the class ExceptionManagementTransactionTest method testNoRollbackForException.

@Test
public void testNoRollbackForException() {
    RuntimeException toThrow = new RuntimeException("Bang!");
    try {
        txControl.build().noRollbackFor(RuntimeException.class).required(() -> {
            PreparedStatement ps = connection.prepareStatement("Insert into TEST_TABLE values ( ? )");
            ps.setString(1, "Hello World!");
            ps.executeUpdate();
            throw toThrow;
        });
        fail("An exception should occur!");
    // We have to catch Exception as the compiler complains
    // otherwise
    } catch (ScopedWorkException swe) {
        assertSame(toThrow, swe.getCause());
    }
    assertEquals("1: Hello World!", txControl.notSupported(() -> {
        Statement s = connection.createStatement();
        ResultSet rs = s.executeQuery("Select count(*) from TEST_TABLE");
        rs.next();
        int count = rs.getInt(1);
        rs = s.executeQuery("Select message from TEST_TABLE ORDER BY message");
        rs.next();
        return "" + count + ": " + rs.getString(1);
    }));
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 12 with ScopedWorkException

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

the class JPALifecycleTest method testDeleteOfConfig.

@Test
public void testDeleteOfConfig() throws Exception {
    Message m = new Message();
    m.message = "Hello World";
    txControl.required(() -> {
        em.persist(m);
        return null;
    });
    assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
    ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
    Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jpa.*)");
    assertNotNull(configurations);
    assertEquals(1, configurations.length);
    configurations[0].delete();
    Thread.sleep(2000);
    try {
        assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
        fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
    } catch (ScopedWorkException swe) {
        assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
        assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
    }
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionException(org.osgi.service.transaction.control.TransactionException) Message(org.apache.aries.tx.control.itests.entity.Message) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 13 with ScopedWorkException

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

the class JPALifecycleTest method testUpdateOfConfig.

@Test
public void testUpdateOfConfig() throws Exception {
    Message m = new Message();
    m.message = "Hello World";
    txControl.required(() -> {
        em.persist(m);
        return null;
    });
    assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
    ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
    Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jpa.*)");
    assertNotNull(configurations);
    assertEquals(1, configurations.length);
    configurations[0].update();
    Thread.sleep(2000);
    try {
        assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
        fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
    } catch (ScopedWorkException swe) {
        assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
        assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
    }
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionException(org.osgi.service.transaction.control.TransactionException) Message(org.apache.aries.tx.control.itests.entity.Message) Configuration(org.osgi.service.cm.Configuration) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 14 with ScopedWorkException

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

the class JPALifecycleTest method doBundleStoppingTest.

private void doBundleStoppingTest(Predicate<Bundle> p, String exceptionMessage) {
    Message m = new Message();
    m.message = "Hello World";
    txControl.required(() -> {
        em.persist(m);
        return null;
    });
    assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
    List<Bundle> toStop = Arrays.stream(context.getBundles()).filter(p).collect(toList());
    System.out.println(toStop);
    try {
        toStop.stream().forEach(b -> {
            System.out.println("Stopping " + b.getSymbolicName());
            try {
                b.stop();
            } catch (BundleException e) {
            }
        });
        try {
            assertEquals(m.message, txControl.notSupported(() -> em.find(Message.class, m.id).message));
            fail("Should not be accessible " + (Boolean.getBoolean(IS_XA) ? "xa" : "local"));
        } catch (ScopedWorkException swe) {
            assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
            assertEquals(exceptionMessage, swe.getCause().getMessage());
        } catch (TransactionException te) {
            assertEquals(exceptionMessage, te.getMessage());
        }
    } finally {
        toStop.stream().forEach(b -> {
            System.out.println("Restarting " + b.getSymbolicName());
            try {
                b.start();
            } catch (BundleException e) {
            }
        });
        getService(JPAEntityManagerProvider.class, 5000);
    }
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionException(org.osgi.service.transaction.control.TransactionException) Message(org.apache.aries.tx.control.itests.entity.Message) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) BundleException(org.osgi.framework.BundleException)

Example 15 with ScopedWorkException

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

the class TransactionControlRunningTest method testTwoRequiredsNestedOuterThrowsException.

@Test
public void testTwoRequiredsNestedOuterThrowsException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new Exception("Bang!");
    try {
        txControl.required(() -> {
            assertTrue(txControl.activeTransaction());
            Object key = txControl.getCurrentContext().getTransactionKey();
            txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
            txControl.setRollbackOnly();
            txControl.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                return null;
            });
            throw userEx;
        });
        fail("Should not be reached");
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(ROLLED_BACK, finalStatusOuter.get());
    assertEquals(COMMITTED, finalStatusInner.get());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Aggregations

ScopedWorkException (org.osgi.service.transaction.control.ScopedWorkException)20 Test (org.junit.Test)19 BindException (java.net.BindException)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 TransactionStatus (org.osgi.service.transaction.control.TransactionStatus)12 TransactionException (org.osgi.service.transaction.control.TransactionException)6 ResultSet (java.sql.ResultSet)4 Configuration (org.osgi.service.cm.Configuration)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 Message (org.apache.aries.tx.control.itests.entity.Message)3 URISyntaxException (java.net.URISyntaxException)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 CoreOptions.mavenBundle (org.ops4j.pax.exam.CoreOptions.mavenBundle)1 Bundle (org.osgi.framework.Bundle)1 BundleException (org.osgi.framework.BundleException)1 JDBCConnectionProviderFactory (org.osgi.service.transaction.control.jdbc.JDBCConnectionProviderFactory)1