Search in sources :

Example 76 with TransactionManager

use of javax.transaction.TransactionManager in project graphdb by neo4j-attic.

the class TestTxSuspendResume method testMultipleTxSameThread.

@Test
public void testMultipleTxSameThread() throws Exception {
    String storePath = getStorePath("test-neo2");
    deleteFileOrDirectory(storePath);
    EmbeddedGraphDatabase neo2 = new EmbeddedGraphDatabase(storePath);
    TransactionManager tm = neo2.getConfig().getTxModule().getTxManager();
    tm.begin();
    Node refNode = neo2.getReferenceNode();
    Transaction tx1 = tm.suspend();
    tm.begin();
    refNode.setProperty("test2", "test");
    Transaction tx2 = tm.suspend();
    tm.resume(tx1);
    CommitThread thread = new CommitThread(tm, tx2);
    thread.start();
    // would wait for ever since tx2 has write lock but now we have other
    // thread thread that will commit tx2
    refNode.removeProperty("test2");
    assertTrue(thread.success());
    tm.commit();
    neo2.shutdown();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 77 with TransactionManager

use of javax.transaction.TransactionManager in project graphdb by neo4j-attic.

the class EmbeddedGraphDbImpl method beginTx.

/**
     * @throws TransactionFailureException if unable to start transaction
     */
public Transaction beginTx() {
    if (graphDbInstance.transactionRunning()) {
        if (placeboTransaction == null) {
            placeboTransaction = new PlaceboTransaction(graphDbInstance.getTransactionManager());
        }
        return placeboTransaction;
    }
    TransactionManager txManager = graphDbInstance.getTransactionManager();
    Transaction result = null;
    try {
        txManager.begin();
        result = new TopLevelTransaction(txManager);
    } catch (Exception e) {
        throw new TransactionFailureException("Unable to begin transaction", e);
    }
    return result;
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransactionManager(javax.transaction.TransactionManager) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) NoSuchElementException(java.util.NoSuchElementException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 78 with TransactionManager

use of javax.transaction.TransactionManager in project spring-framework by spring-projects.

the class JndiJtaTransactionManagerTests method doTestJtaTransactionManagerWithDefaultJndiLookups.

private void doTestJtaTransactionManagerWithDefaultJndiLookups(String tmName, boolean tmFound, boolean defaultUt) throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    if (defaultUt) {
        given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    } else {
        given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }
    JtaTransactionManager ptm = new JtaTransactionManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    if (defaultUt) {
        jndiTemplate.addObject("java:comp/UserTransaction", ut);
    }
    jndiTemplate.addObject(tmName, tm);
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    if (tmFound) {
        assertEquals(tm, ptm.getTransactionManager());
    } else {
        assertNull(ptm.getTransactionManager());
    }
    if (defaultUt) {
        assertEquals(ut, ptm.getUserTransaction());
    } else {
        assertTrue(ptm.getUserTransaction() instanceof UserTransactionAdapter);
        UserTransactionAdapter uta = (UserTransactionAdapter) ptm.getUserTransaction();
        assertEquals(tm, uta.getTransactionManager());
    }
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
        }
    });
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    if (defaultUt) {
        verify(ut).begin();
        verify(ut).commit();
    } else {
        verify(tm).begin();
        verify(tm).commit();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) UserTransactionAdapter(org.springframework.transaction.jta.UserTransactionAdapter)

Example 79 with TransactionManager

use of javax.transaction.TransactionManager in project spring-framework by spring-projects.

the class JndiJtaTransactionManagerTests method jtaTransactionManagerWithCustomJndiLookups.

@Test
public void jtaTransactionManagerWithCustomJndiLookups() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    TransactionManager tm = mock(TransactionManager.class);
    JtaTransactionManager ptm = new JtaTransactionManager();
    ptm.setUserTransactionName("jndi-ut");
    ptm.setTransactionManagerName("jndi-tm");
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    jndiTemplate.addObject("jndi-ut", ut);
    jndiTemplate.addObject("jndi-tm", tm);
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    assertEquals(ut, ptm.getUserTransaction());
    assertEquals(tm, ptm.getTransactionManager());
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
        }
    });
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    verify(ut).begin();
    verify(ut).commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 80 with TransactionManager

use of javax.transaction.TransactionManager in project spring-framework by spring-projects.

the class WebLogicJtaTransactionManager method retrieveTransactionManager.

@Override
protected TransactionManager retrieveTransactionManager() throws TransactionSystemException {
    loadWebLogicTransactionHelper();
    try {
        logger.debug("Retrieving JTA TransactionManager from WebLogic TransactionHelper");
        Method getTransactionManagerMethod = this.transactionHelper.getClass().getMethod("getTransactionManager");
        return (TransactionManager) getTransactionManagerMethod.invoke(this.transactionHelper);
    } catch (InvocationTargetException ex) {
        throw new TransactionSystemException("WebLogic's TransactionHelper.getTransactionManager() method failed", ex.getTargetException());
    } catch (Exception ex) {
        throw new TransactionSystemException("Could not invoke WebLogic's TransactionHelper.getTransactionManager() method", ex);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) Method(java.lang.reflect.Method) TransactionSystemException(org.springframework.transaction.TransactionSystemException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NotSupportedException(javax.transaction.NotSupportedException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(javax.transaction.SystemException) InvalidTransactionException(javax.transaction.InvalidTransactionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5