Search in sources :

Example 61 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method basicThreadCheckingUsage.

@Test
public void basicThreadCheckingUsage() throws Exception {
    JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, JtaPlatformStandardTestingImpl.INSTANCE, preferUserTransactions(), true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction
    tm.begin();
    transactionCoordinator.explicitJoin();
    assertEquals(SynchronizationCallbackCoordinatorTrackingImpl.class, transactionCoordinator.getSynchronizationCallbackCoordinator().getClass());
    tm.commit();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isJoined());
    tm.begin();
    transactionCoordinator.explicitJoin();
    assertEquals(SynchronizationCallbackCoordinatorTrackingImpl.class, transactionCoordinator.getSynchronizationCallbackCoordinator().getClass());
    tm.rollback();
}
Also used : TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 62 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method rollbackCmtUsageTest.

@Test
public void rollbackCmtUsageTest() throws Exception {
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction
    tm.begin();
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(true);
    // NOTE : because of auto-join
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    // create and add a local Synchronization
    SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(localSync);
    // rollback the transaction
    tm.rollback();
    // post conditions
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertEquals(0, localSync.getBeforeCompletionCount());
    assertEquals(0, localSync.getSuccessfulCompletionCount());
    assertEquals(1, localSync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 63 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class DdlInWildFlyUsingBmtAndEmfTest method testCreateThenDrop.

@Test
public void testCreateThenDrop() throws Exception {
    URL persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource(PERSISTENCE_XML_RESOURCE_NAME);
    if (persistenceXmlUrl == null) {
        persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource('/' + PERSISTENCE_XML_RESOURCE_NAME);
    }
    assertNotNull(persistenceXmlUrl);
    ParsedPersistenceXmlDescriptor persistenceUnit = PersistenceXmlParser.locateIndividualPersistenceUnit(persistenceXmlUrl);
    // creating the EMF causes SchemaCreator to be run...
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, Collections.emptyMap()).build();
    // closing the EMF causes the delayed SchemaDropper to be run...
    //		wrap in a transaction just to see if we can get this to fail in the way the WF report says;
    //		in my experience however this succeeds with or without the transaction
    final TransactionManager tm = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager();
    tm.begin();
    Transaction txn = tm.getTransaction();
    emf.close();
    txn.commit();
}
Also used : ParsedPersistenceXmlDescriptor(org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor) JBossAppServerJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform) JtaPlatform(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) URL(java.net.URL) Test(org.junit.Test)

Example 64 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class JOTMJtaPlatform method locateTransactionManager.

@Override
protected TransactionManager locateTransactionManager() {
    try {
        final Class tmClass = serviceRegistry().getService(ClassLoaderService.class).classForName(TM_CLASS_NAME);
        final Method getTransactionManagerMethod = tmClass.getMethod("getTransactionManager");
        return (TransactionManager) getTransactionManagerMethod.invoke(null, (Object[]) null);
    } catch (Exception e) {
        throw new JtaPlatformException("Could not obtain JOTM transaction manager instance", e);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) Method(java.lang.reflect.Method) JtaPlatformException(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException) JtaPlatformException(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 65 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class JOnASJtaPlatform method locateTransactionManager.

@Override
protected TransactionManager locateTransactionManager() {
    try {
        final Class clazz = Class.forName(TM_CLASS_NAME);
        final Method getTransactionManagerMethod = clazz.getMethod("getTransactionManager");
        return (TransactionManager) getTransactionManagerMethod.invoke(null);
    } catch (Exception e) {
        throw new JtaPlatformException("Could not obtain JOnAS transaction manager instance", e);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) Method(java.lang.reflect.Method) JtaPlatformException(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException) JtaPlatformException(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException)

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