use of javax.transaction.Transaction in project tomee by apache.
the class EntityManagerFactoryCallable method call.
@Override
public EntityManagerFactory call() throws Exception {
final ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(appClassLoader);
try {
final Class<?> clazz = appClassLoader.loadClass(persistenceProviderClassName);
final PersistenceProvider persistenceProvider = (PersistenceProvider) clazz.newInstance();
// Create entity manager factories with the validator factory
final Map<String, Object> properties = new HashMap<String, Object>();
if (!ValidationMode.NONE.equals(unitInfo.getValidationMode())) {
properties.put("javax.persistence.validation.factory", new ValidatorFactoryWrapper(potentialValidators));
}
if (cdi && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty("tomee.jpa.cdi", "true")) && "true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jpa.cdi", "true"))) {
properties.put("javax.persistence.bean.manager", Proxy.newProxyInstance(appClassLoader, new Class<?>[] { BeanManager.class }, new BmHandler()));
}
customizeProperties(properties);
// ensure no tx is there cause a managed connection would fail if the provider setAutocCommit(true) and some hib* have this good idea
final Transaction transaction;
if (unitInfo.isLazilyInitialized()) {
transaction = OpenEJB.getTransactionManager().suspend();
} else {
transaction = null;
}
final EntityManagerFactory emf;
try {
emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, properties);
} finally {
if (unitInfo.isLazilyInitialized() && transaction != null) {
OpenEJB.getTransactionManager().resume(transaction);
}
}
if (unitInfo.getProperties() != null && "true".equalsIgnoreCase(unitInfo.getProperties().getProperty(OPENEJB_JPA_INIT_ENTITYMANAGER)) || SystemInstance.get().getOptions().get(OPENEJB_JPA_INIT_ENTITYMANAGER, false)) {
emf.createEntityManager().close();
}
if (unitInfo.getNonJtaDataSource() != null) {
final ImportSql importer = new ImportSql(appClassLoader, unitInfo.getPersistenceUnitName(), unitInfo.getNonJtaDataSource());
if (importer.hasSomethingToImport()) {
// to let OpenJPA create the database if configured this way
emf.createEntityManager().close();
importer.doImport();
}
}
return emf;
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of javax.transaction.Transaction in project tomee by apache.
the class StatefulContainer method getTransaction.
private Transaction getTransaction(final ThreadContext callContext) {
final TransactionPolicy policy = callContext.getTransactionPolicy();
Transaction currentTransaction = null;
if (policy instanceof JtaTransactionPolicy) {
final JtaTransactionPolicy jtaPolicy = (JtaTransactionPolicy) policy;
currentTransaction = jtaPolicy.getCurrentTransaction();
}
return currentTransaction;
}
use of javax.transaction.Transaction in project tomee by apache.
the class StatefulContainer method obtainInstance.
@SuppressWarnings("LockAcquiredButNotSafelyReleased")
private Instance obtainInstance(final Object primaryKey, final ThreadContext callContext, final Method callMethod, final boolean checkOutIfNecessary) throws OpenEJBException {
if (primaryKey == null) {
throw new SystemException(new NullPointerException("Cannot obtain an instance of the stateful session bean with a null session id"));
}
final Transaction currentTransaction = getTransaction(callContext);
// Find the instance
Instance instance;
synchronized (this) {
instance = checkedOutInstances.get(primaryKey);
if (instance == null) {
// no need to check for extended persistence contexts it shouldn't happen
try {
instance = cache.checkOut(primaryKey, checkOutIfNecessary);
} catch (final OpenEJBException e) {
throw e;
} catch (final Exception e) {
throw new SystemException("Unexpected load exception", e);
}
// Did we find the instance?
if (instance == null) {
throw new InvalidateReferenceException(new NoSuchObjectException("Not Found"));
}
// remember instance until it is returned to the cache
checkedOutInstances.put(primaryKey, instance);
}
}
final Duration accessTimeout = getAccessTimeout(instance.beanContext, callMethod);
final LockFactory.StatefulLock currLock = instance.getLock();
final boolean lockAcquired;
if (accessTimeout == null || accessTimeout.getTime() < 0) {
// wait indefinitely for a lock
currLock.lock();
lockAcquired = true;
} else if (accessTimeout.getTime() == 0) {
// concurrent calls are not allowed, lock only once
lockAcquired = currLock.tryLock();
} else {
// try to get a lock within the specified period.
try {
lockAcquired = currLock.tryLock(accessTimeout.getTime(), accessTimeout.getUnit());
} catch (final InterruptedException e) {
throw new ApplicationException("Unable to get lock.", e);
}
}
// Did we acquire the lock to the current execution?
if (!lockAcquired) {
throw new ApplicationException(new ConcurrentAccessTimeoutException("Unable to get lock."));
}
if (instance.getTransaction() != null) {
if (!instance.getTransaction().equals(currentTransaction) && !instance.getLock().tryLock()) {
throw new ApplicationException(new RemoteException("Instance is in a transaction and cannot be invoked outside that transaction. See EJB 3.0 Section 4.4.4"));
}
} else {
instance.setTransaction(currentTransaction);
}
// Mark the instance in use so we can detect reentrant calls
instance.setInUse(true);
return instance;
}
use of javax.transaction.Transaction 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();
}
use of javax.transaction.Transaction in project hibernate-orm by hibernate.
the class DualNodeJtaTransactionManagerImpl method commit.
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
Transaction tx = getCurrentTransaction();
if (tx == null) {
throw new IllegalStateException("no current transaction to commit");
}
tx.commit();
}
Aggregations