use of javax.transaction.TransactionSynchronizationRegistry in project Payara by payara.
the class TransactionScopedBean method afterCompletion.
/**
* Destroy the contextual instance.
*/
@Override
public void afterCompletion(int i) {
try {
TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
// We can't do "getResource" on TransactionSynchronizationRegistry at this stage in completion
if (transactionSynchronizationRegistry != null) {
if (transactionScopedContext != null) {
// Get list of TransactionScopedBeans for this Transaction
Set<TransactionScopedBean> transactionScopedBeanSet = transactionScopedContext.beansPerTransaction.get(transactionSynchronizationRegistry);
if (transactionScopedBeanSet != null) {
// Remove the current TransactionScopedBean from list as we are destroying it now
if (transactionScopedBeanSet.contains(this)) {
transactionScopedBeanSet.remove(this);
}
// entry from main Map
if (transactionScopedBeanSet.size() == 0) {
TransactionScopedCDIUtil.fireEvent(TransactionScopedCDIUtil.DESTORYED_EVENT);
transactionScopedContext.beansPerTransaction.remove(transactionSynchronizationRegistry);
}
// Not updating entry in main Map with leftover TransactionScopedBeans as it should happen by
// reference
}
}
}
} catch (NamingException ne) {
log("Can't get instance of TransactionSynchronizationRegistry to process TransactionScoped Destroyed CDI Event!");
ne.printStackTrace();
} finally {
contextual.destroy(contextualInstance, creationalContext);
}
}
use of javax.transaction.TransactionSynchronizationRegistry in project Payara by payara.
the class TransactionScopedBean method getTransactionSynchronizationRegistry.
private TransactionSynchronizationRegistry getTransactionSynchronizationRegistry() throws NamingException {
TransactionSynchronizationRegistry transactionSynchronizationRegistry;
try {
InitialContext initialContext = new InitialContext();
transactionSynchronizationRegistry = (TransactionSynchronizationRegistry) initialContext.lookup(TRANSACTION_SYNCHRONIZATION_REGISTRY_JNDI_NAME);
} catch (NamingException ne) {
throw ne;
}
// Not checking for transaction status, it would be 6, as its in afterCompletion
return transactionSynchronizationRegistry;
}
use of javax.transaction.TransactionSynchronizationRegistry in project Payara by payara.
the class TransactionScopedContextImplTest method testget.
@Test
@SuppressWarnings("unchecked")
public void testget() throws Exception {
TransactionSynchronizationRegistry transactionSynchronizationRegistry = mockSupport.createMock(TransactionSynchronizationRegistry.class);
Contextual<LocalBean> contextual = mockSupport.createMock(Contextual.class);
CreationalContext<LocalBean> creationalContext = mockSupport.createMock(CreationalContext.class);
ContextualPassivationCapable<LocalPassivationCapableBean> passivationCapableContextual = mockSupport.createMock(ContextualPassivationCapable.class);
CreationalContext<LocalPassivationCapableBean> passivationCapableCreationalContext = mockSupport.createMock(CreationalContext.class);
// test transaction not active
setupMocksForInactiveTransaction(transactionSynchronizationRegistry);
mockSupport.replayAll();
TransactionScopedContextImpl transactionScopedContext = new TransactionScopedContextImpl();
try {
transactionScopedContext.get(contextual, creationalContext);
fail("Should have gotten a ContextNotActiveException.");
} catch (ContextNotActiveException ignore) {
}
mockSupport.verifyAll();
mockSupport.resetAll();
// test active transaction. Create new contextual instance
LocalBean localBean = new LocalBean();
setupMocksForActiveTransaction(transactionSynchronizationRegistry);
setupMocksForGetContextualInstance(transactionSynchronizationRegistry, contextual, null, null);
setupMocksForCreateContextualInstance(transactionSynchronizationRegistry, contextual, creationalContext, contextual, localBean);
mockSupport.replayAll();
LocalBean retrievedLocalBean = transactionScopedContext.get(contextual, creationalContext);
assertSame(localBean, retrievedLocalBean);
mockSupport.verifyAll();
mockSupport.resetAll();
// test active transaction. Get existing contextual instance
TransactionScopedBean<LocalBean> transactionScopedBean = TransactionScopedBeanTest.getTransactionScopedBean(mockSupport, localBean, contextual, creationalContext, transactionScopedContext);
setupMocksForActiveTransaction(transactionSynchronizationRegistry);
setupMocksForGetContextualInstance(transactionSynchronizationRegistry, contextual, transactionScopedBean, localBean);
mockSupport.replayAll();
retrievedLocalBean = transactionScopedContext.get(contextual, creationalContext);
assertSame(localBean, retrievedLocalBean);
mockSupport.verifyAll();
mockSupport.resetAll();
// test active transaction with PassivationCapable
String beanId = "PCCId";
LocalPassivationCapableBean localPassivationCapableBean = new LocalPassivationCapableBean();
TransactionScopedBean<LocalPassivationCapableBean> transactionScopedPassivationCapableBean = TransactionScopedBeanTest.getTransactionScopedBean(mockSupport, localPassivationCapableBean, passivationCapableContextual, passivationCapableCreationalContext, transactionScopedContext);
setupMocksForActiveTransaction(transactionSynchronizationRegistry);
expect(passivationCapableContextual.getId()).andReturn(beanId);
setupMocksForGetContextualInstance(transactionSynchronizationRegistry, beanId, transactionScopedPassivationCapableBean, localPassivationCapableBean);
mockSupport.replayAll();
LocalPassivationCapableBean retrievedLocalPassivationCapableBean = transactionScopedContext.get(passivationCapableContextual, passivationCapableCreationalContext);
assertSame(localPassivationCapableBean, retrievedLocalPassivationCapableBean);
mockSupport.verifyAll();
mockSupport.resetAll();
// test the get(Contextual<T> contextual) method...transaction not active
setupMocksForInactiveTransaction(transactionSynchronizationRegistry);
mockSupport.replayAll();
transactionScopedContext = new TransactionScopedContextImpl();
try {
transactionScopedContext.get(contextual);
fail("Should have gotten a ContextNotActiveException.");
} catch (ContextNotActiveException ignore) {
}
mockSupport.verifyAll();
mockSupport.resetAll();
// test the get(Contextual<T> contextual) method...transaction active
setupMocksForActiveTransaction(transactionSynchronizationRegistry);
setupMocksForGetContextualInstance(transactionSynchronizationRegistry, contextual, transactionScopedBean, localBean);
mockSupport.replayAll();
retrievedLocalBean = transactionScopedContext.get(contextual);
assertSame(localBean, retrievedLocalBean);
mockSupport.verifyAll();
mockSupport.resetAll();
}
use of javax.transaction.TransactionSynchronizationRegistry in project Payara by payara.
the class TransactionScopedContextImplTest method testisActive.
@Test
public void testisActive() throws Exception {
TransactionSynchronizationRegistry transactionSynchronizationRegistry = mockSupport.createMock(TransactionSynchronizationRegistry.class);
expect(initialContext.lookup(TRANSACTION_SYNCHRONIZATION_REGISTRY_JNDI_NAME)).andThrow(new NamingException());
mockSupport.replayAll();
TransactionScopedContextImpl transactionScopedContext = new TransactionScopedContextImpl();
assertFalse(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForInactiveTransaction(transactionSynchronizationRegistry);
mockSupport.replayAll();
assertFalse(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_ACTIVE);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_MARKED_ROLLBACK);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_PREPARED);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_UNKNOWN);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_PREPARING);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_COMMITTING);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
setupMocksForActiveTransaction(transactionSynchronizationRegistry, Status.STATUS_ROLLING_BACK);
mockSupport.replayAll();
assertTrue(transactionScopedContext.isActive());
mockSupport.verifyAll();
mockSupport.resetAll();
}
use of javax.transaction.TransactionSynchronizationRegistry in project wildfly by wildfly.
the class PersistenceUnitServiceHandler method deployPersistenceUnit.
/**
* start the persistence unit in one phase
*
* @param deploymentUnit
* @param eeModuleDescription
* @param serviceTarget
* @param classLoader
* @param pu
* @param provider
* @param adaptor
* @param allowCdiBeanManagerAccess
* @throws DeploymentUnitProcessingException
*/
private static void deployPersistenceUnit(final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, final ServiceTarget serviceTarget, final ModuleClassLoader classLoader, final PersistenceUnitMetadata pu, final PersistenceProvider provider, final PersistenceProviderAdaptor adaptor, final boolean allowCdiBeanManagerAccess) throws DeploymentUnitProcessingException {
pu.setClassLoader(classLoader);
TransactionManager transactionManager = ContextTransactionManager.getInstance();
TransactionSynchronizationRegistry transactionSynchronizationRegistry = deploymentUnit.getAttachment(JpaAttachments.TRANSACTION_SYNCHRONIZATION_REGISTRY);
CapabilityServiceSupport capabilitySupport = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
try {
ValidatorFactory validatorFactory = null;
final HashMap<String, ValidatorFactory> properties = new HashMap<>();
CapabilityServiceSupport css = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
if (!ValidationMode.NONE.equals(pu.getValidationMode()) && css.hasCapability("org.wildfly.bean-validation")) {
// Get the Jakarta Contexts and Dependency Injection enabled ValidatorFactory
validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
}
BeanManagerAfterDeploymentValidation beanManagerAfterDeploymentValidation = registerJPAEntityListenerRegister(deploymentUnit, capabilitySupport);
final PersistenceAdaptorRemoval persistenceAdaptorRemoval = new PersistenceAdaptorRemoval(pu, adaptor);
deploymentUnit.addToAttachmentList(REMOVAL_KEY, persistenceAdaptorRemoval);
// add persistence provider specific properties
adaptor.addProviderProperties(properties, pu);
final ServiceName puServiceName = PersistenceUnitServiceImpl.getPUServiceName(pu);
deploymentUnit.putAttachment(JpaAttachments.PERSISTENCE_UNIT_SERVICE_KEY, puServiceName);
deploymentUnit.addToAttachmentList(Attachments.DEPLOYMENT_COMPLETE_SERVICES, puServiceName);
deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, puServiceName);
final PersistenceUnitServiceImpl service = new PersistenceUnitServiceImpl(properties, classLoader, pu, adaptor, provider, PersistenceUnitRegistryImpl.INSTANCE, deploymentUnit.getServiceName(), validatorFactory, deploymentUnit.getAttachment(org.jboss.as.ee.naming.Attachments.JAVA_NAMESPACE_SETUP_ACTION), beanManagerAfterDeploymentValidation);
ServiceBuilder<PersistenceUnitService> builder = serviceTarget.addService(puServiceName, service);
boolean useDefaultDataSource = Configuration.allowDefaultDataSourceUse(pu);
final String jtaDataSource = adjustJndi(pu.getJtaDataSourceName());
final String nonJtaDataSource = adjustJndi(pu.getNonJtaDataSourceName());
if (jtaDataSource != null && jtaDataSource.length() > 0) {
if (jtaDataSource.equals(EE_DEFAULT_DATASOURCE)) {
// explicit use of default datasource
useDefaultDataSource = true;
} else {
builder.addDependency(ContextNames.bindInfoForEnvEntry(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), false, jtaDataSource).getBinderServiceName(), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getJtaDataSourceInjector()));
useDefaultDataSource = false;
}
}
if (nonJtaDataSource != null && nonJtaDataSource.length() > 0) {
builder.addDependency(ContextNames.bindInfoForEnvEntry(eeModuleDescription.getApplicationName(), eeModuleDescription.getModuleName(), eeModuleDescription.getModuleName(), false, nonJtaDataSource).getBinderServiceName(), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getNonJtaDataSourceInjector()));
useDefaultDataSource = false;
}
// JPA 2.0 8.2.1.5, container provides default Jakarta Transactions datasource
if (useDefaultDataSource) {
// try the default datasource defined in the ee subsystem
String defaultJtaDataSource = null;
if (eeModuleDescription != null) {
defaultJtaDataSource = eeModuleDescription.getDefaultResourceJndiNames().getDataSource();
}
if (defaultJtaDataSource == null || defaultJtaDataSource.isEmpty()) {
// try the datasource defined in the Jakarta Persistence subsystem
defaultJtaDataSource = adjustJndi(JPAService.getDefaultDataSourceName());
}
if (defaultJtaDataSource != null && !defaultJtaDataSource.isEmpty()) {
builder.addDependency(ContextNames.bindInfoFor(defaultJtaDataSource).getBinderServiceName(), ManagedReferenceFactory.class, new ManagedReferenceFactoryInjector(service.getJtaDataSourceInjector()));
ROOT_LOGGER.tracef("%s is using the default data source '%s'", puServiceName, defaultJtaDataSource);
}
}
// JPA 2.1 sections 3.5.1 + 9.1 require the Jakarta Contexts and Dependency Injection bean manager to be passed to the peristence provider
// if the persistence unit is contained in a deployment that is a Jakarta Contexts and Dependency Injection bean archive (has beans.xml).
final CapabilityServiceSupport support = deploymentUnit.getAttachment(Attachments.CAPABILITY_SERVICE_SUPPORT);
if (support.hasCapability(WELD_CAPABILITY_NAME) && allowCdiBeanManagerAccess) {
support.getOptionalCapabilityRuntimeAPI(WELD_CAPABILITY_NAME, WeldCapability.class).get().addBeanManagerService(deploymentUnit, builder, service.getBeanManagerInjector());
}
try {
// save a thread local reference to the builder for setting up the second level cache dependencies
CacheDeploymentListener.setInternalDeploymentSupport(builder, capabilitySupport);
adaptor.addProviderDependencies(pu);
} finally {
CacheDeploymentListener.clearInternalDeploymentSupport();
}
/**
* handle extension that binds a transaction scoped entity manager to specified JNDI location
*/
entityManagerBind(eeModuleDescription, serviceTarget, pu, puServiceName, transactionManager, transactionSynchronizationRegistry);
/**
* handle extension that binds an entity manager factory to specified JNDI location
*/
entityManagerFactoryBind(eeModuleDescription, serviceTarget, pu, puServiceName);
// get async executor from Services.addServerExecutorDependency
addServerExecutorDependency(builder, service.getExecutorInjector());
builder.install();
ROOT_LOGGER.tracef("added PersistenceUnitService for '%s'. PU is ready for injector action.", puServiceName);
addManagementConsole(deploymentUnit, pu, adaptor, persistenceAdaptorRemoval);
} catch (ServiceRegistryException e) {
throw JpaLogger.ROOT_LOGGER.failedToAddPersistenceUnit(e, pu.getPersistenceUnitName());
}
}
Aggregations