Search in sources :

Example 31 with UserTransaction

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

the class WebSphereUowTransactionManagerTests method uowManagerAndUserTransactionFoundInJndi.

@Test
public void uowManagerAndUserTransactionFoundInJndi() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    MockUOWManager manager = new MockUOWManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut);
    jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionStatus ts = ptm.getTransaction(definition);
    ptm.commit(ts);
    assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            return "result";
        }
    }));
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
    verify(ut).begin();
    verify(ut).commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 32 with UserTransaction

use of javax.transaction.UserTransaction in project aries by apache.

the class AdvancedEJBBundleTest method testTransactionalEJB.

@Test
@Ignore
public void testTransactionalEJB() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    addToZip(zos, "TX_MANIFEST.MF", "META-INF/MANIFEST.MF");
    addToZip(zos, "beans/integration/impl/TxSingleton.class");
    addToZip(zos, "beans/integration/Tx.class");
    zos.close();
    Bundle test = context().installBundle("", new ByteArrayInputStream(baos.toByteArray()));
    try {
        test.start();
        Object bean = context().getService(context().getServiceReference(Tx.class.getName()));
        UserTransaction ut = context().getService(UserTransaction.class);
        Method no = bean.getClass().getMethod("getNoTransactionId");
        Method maybe = bean.getClass().getMethod("getMaybeTransactionId");
        Method tx = bean.getClass().getMethod("getTransactionId");
        Method newTx = bean.getClass().getMethod("getNewTransactionId");
        assertNull(no.invoke(bean));
        assertNull(maybe.invoke(bean));
        assertNotNull(tx.invoke(bean));
        assertNotNull(newTx.invoke(bean));
        ut.begin();
        try {
            Object key = context().getService(TransactionSynchronizationRegistry.class).getTransactionKey();
            assertNotNull(key);
            assertNull(no.invoke(bean));
            assertSame(key, maybe.invoke(bean));
            assertSame(key, tx.invoke(bean));
            Object o = newTx.invoke(bean);
            assertNotNull(o);
            assertNotSame(key, o);
        } finally {
            ut.commit();
        }
        test.stop();
    } finally {
        test.uninstall();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) Bundle(org.osgi.framework.Bundle) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 33 with UserTransaction

use of javax.transaction.UserTransaction in project aries by apache.

the class AdvancedEJBBundleTest method testJPAContextSharing.

@Test
@Ignore
public void testJPAContextSharing() throws Exception {
    System.setProperty("openejb.validation.output.level", "VERBOSE");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    addToZip(zos, "JPA_MANIFEST.MF", "META-INF/MANIFEST.MF");
    addToZip(zos, "persistence.xml", "META-INF/persistence.xml");
    addToZip(zos, "beans/integration/impl/JPASingleton.class");
    addToZip(zos, "beans/jpa/Laptop.class");
    zos.close();
    Bundle test = context().installBundle("", new ByteArrayInputStream(baos.toByteArray()));
    try {
        test.start();
        PersistenceContextProvider provider = context().getService(PersistenceContextProvider.class);
        HashMap<String, Object> props = new HashMap<String, Object>();
        props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.TRANSACTION);
        provider.registerContext("ejb-test", context().getBundle(), props);
        Object bean = context().getService(context().getServiceReference(JPASingleton.class.getName()));
        UserTransaction ut = context().getService(UserTransaction.class);
        Method m = bean.getClass().getMethod("editEntity", String.class);
        EntityManager em = context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=ejb-test)(" + PersistenceUnitConstants.CONTAINER_MANAGED_PERSISTENCE_UNIT + "=true)" + "(" + PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE + "=*))").createEntityManager();
        ut.begin();
        try {
            Object e = test.loadClass(Laptop.class.getName()).newInstance();
            e.getClass().getMethod("setSerialNumber", String.class).invoke(e, "ABC123");
            e.getClass().getMethod("setNumberOfCores", int.class).invoke(e, 1);
            em.persist(e);
            m.invoke(bean, "ABC123");
            assertEquals(4, e.getClass().getMethod("getNumberOfCores").invoke(e));
            assertEquals(Integer.MAX_VALUE, e.getClass().getMethod("getHardDiskSize").invoke(e));
        } finally {
            ut.commit();
        }
        test.stop();
    } finally {
        test.uninstall();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) PersistenceContextProvider(org.apache.aries.jpa.container.context.PersistenceContextProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) EntityManager(javax.persistence.EntityManager) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with UserTransaction

use of javax.transaction.UserTransaction in project deltaspike by apache.

the class UserTransactionResolver method resolveUserTransaction.

public UserTransaction resolveUserTransaction() {
    UserTransaction userTransaction;
    try {
        DependentProvider<ManagedUserTransactionResolver> provider = BeanProvider.getDependent(this.beanManager, ManagedUserTransactionResolver.class);
        userTransaction = provider.get().resolveUserTransaction();
        provider.destroy();
    } catch (Throwable t) {
        //it was just a try
        userTransaction = null;
    }
    if (userTransaction != null) {
        return userTransaction;
    }
    String jndiName = JpaBaseConfig.UserTransaction.JNDI_NAME;
    String[] jndiNames = jndiName.split(",");
    for (String currentJndiName : jndiNames) {
        try {
            userTransaction = JndiUtils.lookup(currentJndiName, UserTransaction.class);
            if (userTransaction != null) {
                break;
            }
        } catch (Exception e) {
            userTransaction = null;
        }
    }
    return userTransaction;
}
Also used : UserTransaction(javax.transaction.UserTransaction)

Example 35 with UserTransaction

use of javax.transaction.UserTransaction in project geode by apache.

the class QueryAndJtaJUnitTest method testFailedIndexUpdateOnJTACommitForPut.

/*
   * Enable this test when indexes are made transactional.
   */
@Ignore
@Test
public void testFailedIndexUpdateOnJTACommitForPut() throws Exception {
    Person.THROW_ON_INDEX = true;
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    Region region = cache.createRegion("sample", af.create());
    qs.createIndex("foo", IndexType.FUNCTIONAL, "index", "/sample");
    Context ctx = cache.getJNDIContext();
    UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
    Integer x = new Integer(0);
    utx.begin();
    region.create(x, new Person("xyz", 45));
    try {
        utx.commit();
        fail("Commit should have thrown an exception because the index update threw");
    } catch (Exception e) {
    // this is desired
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) AttributesFactory(org.apache.geode.cache.AttributesFactory) Region(org.apache.geode.cache.Region) RollbackException(javax.transaction.RollbackException) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

UserTransaction (javax.transaction.UserTransaction)224 Test (org.junit.Test)105 Context (javax.naming.Context)47 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)46 Node (javax.jcr.Node)42 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)42 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)42 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)35 DataSource (javax.sql.DataSource)30 Connection (java.sql.Connection)27 NamingException (javax.naming.NamingException)25 IOException (java.io.IOException)24 Statement (java.sql.Statement)23 Session (javax.jcr.Session)23 InitialContext (javax.naming.InitialContext)22 SQLException (java.sql.SQLException)20 SystemException (javax.transaction.SystemException)17 RollbackException (javax.transaction.RollbackException)16 Region (org.apache.geode.cache.Region)13 Lock (javax.jcr.lock.Lock)12