Search in sources :

Example 6 with ScopedWorkException

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

the class TransactionControlRunningTest method testTwoRequiredsNestedNoRollbackForInnerException.

@Test
public void testTwoRequiredsNestedNoRollbackForInnerException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new BindException("Bang!");
    try {
        txControl.required(() -> {
            assertTrue(txControl.activeTransaction());
            Object key = txControl.getCurrentContext().getTransactionKey();
            txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
            try {
                txControl.build().noRollbackFor(BindException.class).requiresNew(() -> {
                    assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                    txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                    throw userEx;
                });
                fail("Should not be reached!");
            } catch (ScopedWorkException swe) {
                throw swe.as(BindException.class);
            }
            return null;
        });
        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) BindException(java.net.BindException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 7 with ScopedWorkException

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

the class TransactionControlRunningTest method testTwoRequiredsNestedInnerThrowsException.

@Test
public void testTwoRequiredsNestedInnerThrowsException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new Exception("Bang!");
    txControl.required(() -> {
        assertTrue(txControl.activeTransaction());
        Object key = txControl.getCurrentContext().getTransactionKey();
        txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
        try {
            txControl.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                txControl.setRollbackOnly();
                throw userEx;
            });
            fail("Should not be reached!");
        } catch (ScopedWorkException swe) {
            assertSame(userEx, swe.getCause());
        }
        return null;
    });
    assertEquals(COMMITTED, finalStatusOuter.get());
    assertEquals(ROLLED_BACK, 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)

Example 8 with ScopedWorkException

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

the class TransactionControlRunningTest method testRequiredNoRollbackException.

@Test
public void testRequiredNoRollbackException() {
    AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
    Exception userEx = new BindException("Bang!");
    try {
        txControl.build().noRollbackFor(BindException.class).required(() -> {
            assertTrue(txControl.activeTransaction());
            txControl.getCurrentContext().postCompletion(finalStatus::set);
            throw userEx;
        });
        fail("Should not be reached");
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(COMMITTED, finalStatus.get());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) BindException(java.net.BindException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 9 with ScopedWorkException

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

the class TransactionControlRunningTest method testTwoRequiredsNestedInnerThrowsExceptionOuterDoesNotCatch.

@Test
public void testTwoRequiredsNestedInnerThrowsExceptionOuterDoesNotCatch() {
    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.requiresNew(() -> {
                assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                txControl.setRollbackOnly();
                throw userEx;
            });
            fail("Should not be reached!");
            return null;
        });
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(ROLLED_BACK, finalStatusOuter.get());
    assertEquals(ROLLED_BACK, 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)

Example 10 with ScopedWorkException

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

the class ConnectionLifecycleTest method testUpdateOfConfig.

@Test
public void testUpdateOfConfig() throws Exception {
    Assume.assumeTrue("Not a configuration test", isConfigured());
    txControl.required(() -> connection.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )"));
    assertEquals("Hello World!", txControl.notSupported(() -> {
        ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
        rs.next();
        return rs.getString(1);
    }));
    ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
    Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jdbc.*)");
    assertNotNull(configurations);
    assertEquals(1, configurations.length);
    configurations[0].update();
    Thread.sleep(2000);
    try {
        assertEquals("Hello World!", txControl.notSupported(() -> {
            ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
            rs.next();
            return rs.getString(1);
        }));
        fail("Should not be accessible");
    } 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) Configuration(org.osgi.service.cm.Configuration) ResultSet(java.sql.ResultSet) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) 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