Search in sources :

Example 1 with TransactionManagerWrapper

use of org.apache.openejb.resource.TransactionManagerWrapper in project tomee by apache.

the class PoolDataSourceCreator method poolManagedWithRecovery.

@Override
public DataSource poolManagedWithRecovery(final String name, final XAResourceWrapper xaResourceWrapper, final String driver, final Properties properties) {
    final TransactionManager transactionManager = new TransactionManagerWrapper(OpenEJB.getTransactionManager(), name, xaResourceWrapper);
    final CommonDataSource ds = pool(name, driver, properties);
    if (ds instanceof XADataSource) {
        return new ManagedXADataSource(ds, transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class));
    }
    return new ManagedDataSource(DataSource.class.cast(ds), transactionManager, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class));
}
Also used : XADataSource(javax.sql.XADataSource) ManagedXADataSource(org.apache.openejb.resource.jdbc.managed.xa.ManagedXADataSource) TransactionManager(javax.transaction.TransactionManager) TransactionManagerWrapper(org.apache.openejb.resource.TransactionManagerWrapper) ManagedXADataSource(org.apache.openejb.resource.jdbc.managed.xa.ManagedXADataSource) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) CommonDataSource(javax.sql.CommonDataSource) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) XADataSource(javax.sql.XADataSource) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource) ManagedXADataSource(org.apache.openejb.resource.jdbc.managed.xa.ManagedXADataSource)

Example 2 with TransactionManagerWrapper

use of org.apache.openejb.resource.TransactionManagerWrapper in project tomee by apache.

the class ManagedConnectionBehaviorTest method run.

@Test
public void run() throws Exception {
    final GeronimoTransactionManager geronimoTransactionManager = new GeronimoTransactionManager((int) TimeUnit.MINUTES.toMillis(10));
    final TransactionManager mgr = new TransactionManagerWrapper(geronimoTransactionManager, "ManagedConnectionBehaviorTest", new GeronimoTransactionManagerFactory.GeronimoXAResourceWrapper());
    final MyDs myDs = new MyDs();
    final DataSource ds = new ManagedDataSource(myDs, geronimoTransactionManager, geronimoTransactionManager);
    {
        // no tx
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        connection.close();
        assertTrue(myDs.connections.iterator().next().closed);
        myDs.connections.clear();
    }
    {
        // tx
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        mgr.commit();
        assertTrue(myDs.connections.iterator().next().closed);
        assertTrue(myDs.connections.iterator().next().commit);
        assertFalse(myDs.connections.iterator().next().rollback);
        myDs.connections.clear();
    }
    {
        // tx already init
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        for (int i = 0; i < 5; i++) {
            // here the connection is already created, ensure we dont leak other connections
            connection.createBlob();
        }
        assertEquals(1, myDs.connections.size());
        mgr.commit();
        assertTrue(myDs.connections.iterator().next().closed);
        assertTrue(myDs.connections.iterator().next().commit);
        assertFalse(myDs.connections.iterator().next().rollback);
        myDs.connections.clear();
    }
    {
        // multiple tx
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        final Transaction previous = mgr.suspend();
        mgr.begin();
        final Connection connection2 = ds.getConnection();
        connection2.createBlob();
        assertEquals(2, myDs.connections.size());
        mgr.commit();
        mgr.resume(previous);
        mgr.commit();
        final Iterator<MyConn> iterator = myDs.connections.iterator();
        final MyConn first = iterator.next();
        assertTrue(first.closed);
        assertTrue(first.commit);
        assertTrue(myDs.connections.iterator().next().commit);
        myDs.connections.clear();
    }
}
Also used : Transaction(javax.transaction.Transaction) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionManagerWrapper(org.apache.openejb.resource.TransactionManagerWrapper) Connection(java.sql.Connection) Iterator(java.util.Iterator) GeronimoTransactionManagerFactory(org.apache.openejb.resource.GeronimoTransactionManagerFactory) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) DataSource(javax.sql.DataSource) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)2 TransactionManager (javax.transaction.TransactionManager)2 TransactionManagerWrapper (org.apache.openejb.resource.TransactionManagerWrapper)2 ManagedDataSource (org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource)2 Connection (java.sql.Connection)1 Iterator (java.util.Iterator)1 CommonDataSource (javax.sql.CommonDataSource)1 XADataSource (javax.sql.XADataSource)1 Transaction (javax.transaction.Transaction)1 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)1 GeronimoTransactionManager (org.apache.geronimo.transaction.manager.GeronimoTransactionManager)1 GeronimoTransactionManagerFactory (org.apache.openejb.resource.GeronimoTransactionManagerFactory)1 ManagedXADataSource (org.apache.openejb.resource.jdbc.managed.xa.ManagedXADataSource)1 Test (org.junit.Test)1