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();
}
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();
}
}
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();
}
}
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;
}
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
}
}
Aggregations