Search in sources :

Example 26 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project wildfly by wildfly.

the class TransactionJndiBindingProcessor method bindServices.

/**
 * Binds the java:comp/UserTransaction service and the java:comp/TransactionSynchronizationRegistry
 *
 * @param deploymentUnit The deployment unit
 * @param serviceTarget The service target
 * @param contextServiceName The service name of the context to bind to
 */
private void bindServices(DeploymentUnit deploymentUnit, ServiceTarget serviceTarget, ServiceName contextServiceName) {
    final ServiceName userTransactionServiceName = contextServiceName.append("UserTransaction");
    final UserTransactionBindingService userTransactionBindingService = new UserTransactionBindingService("UserTransaction");
    serviceTarget.addService(userTransactionServiceName, userTransactionBindingService).addDependency(UserTransactionAccessControlService.SERVICE_NAME, UserTransactionAccessControlService.class, userTransactionBindingService.getUserTransactionAccessControlServiceInjector()).addDependency(UserTransactionService.INTERNAL_SERVICE_NAME, UserTransaction.class, new ManagedReferenceInjector<UserTransaction>(userTransactionBindingService.getManagedObjectInjector())).addDependency(contextServiceName, ServiceBasedNamingStore.class, userTransactionBindingService.getNamingStoreInjector()).install();
    final Map<ServiceName, Set<ServiceName>> jndiComponentDependencies = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPONENT_JNDI_DEPENDENCIES);
    Set<ServiceName> jndiDependencies = jndiComponentDependencies.get(contextServiceName);
    if (jndiDependencies == null) {
        jndiComponentDependencies.put(contextServiceName, jndiDependencies = new HashSet<>());
    }
    jndiDependencies.add(userTransactionServiceName);
    final ServiceName transactionSynchronizationRegistryName = contextServiceName.append("TransactionSynchronizationRegistry");
    BinderService transactionSyncBinderService = new BinderService("TransactionSynchronizationRegistry");
    serviceTarget.addService(transactionSynchronizationRegistryName, transactionSyncBinderService).addDependency(TransactionSynchronizationRegistryService.INTERNAL_SERVICE_NAME, TransactionSynchronizationRegistry.class, new ManagedReferenceInjector<TransactionSynchronizationRegistry>(transactionSyncBinderService.getManagedObjectInjector())).addDependency(contextServiceName, ServiceBasedNamingStore.class, transactionSyncBinderService.getNamingStoreInjector()).install();
    jndiDependencies.add(transactionSynchronizationRegistryName);
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashSet(java.util.HashSet) Set(java.util.Set) UserTransactionBindingService(org.jboss.as.txn.service.UserTransactionBindingService) BinderService(org.jboss.as.naming.service.BinderService) ServiceName(org.jboss.msc.service.ServiceName) ServiceBasedNamingStore(org.jboss.as.naming.ServiceBasedNamingStore) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) UserTransactionAccessControlService(org.jboss.as.txn.service.UserTransactionAccessControlService) HashSet(java.util.HashSet)

Example 27 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project tomee by apache.

the class Assembler method createTransactionManager.

public void createTransactionManager(final TransactionServiceInfo serviceInfo) throws OpenEJBException {
    Object service = SystemInstance.get().getComponent(TransactionManager.class);
    if (service == null) {
        final ObjectRecipe serviceRecipe = createRecipe(Collections.<ServiceInfo>emptyList(), serviceInfo);
        service = serviceRecipe.create();
        logUnusedProperties(serviceRecipe, serviceInfo);
    } else {
        logger.info("Reusing provided TransactionManager " + service);
    }
    final Class interfce = serviceInterfaces.get(serviceInfo.service);
    checkImplementation(interfce, service.getClass(), serviceInfo.service, serviceInfo.id);
    try {
        this.containerSystem.getJNDIContext().bind(JAVA_OPENEJB_NAMING_CONTEXT + serviceInfo.service, service);
        this.containerSystem.getJNDIContext().bind("comp/UserTransaction", new CoreUserTransaction((TransactionManager) service));
        this.containerSystem.getJNDIContext().bind("comp/TransactionManager", service);
    } catch (final NamingException e) {
        throw new OpenEJBException("Cannot bind " + serviceInfo.service + " with id " + serviceInfo.id, e);
    }
    setSystemInstanceComponent(interfce, service);
    getContext().put(interfce.getName(), service);
    props.put(interfce.getName(), service);
    props.put(serviceInfo.service, service);
    props.put(serviceInfo.id, service);
    this.transactionManager = (TransactionManager) service;
    // Update the config tree
    config.facilities.transactionService = serviceInfo;
    // todo find a better place for this
    // TransactionSynchronizationRegistry
    final TransactionSynchronizationRegistry synchronizationRegistry;
    if (transactionManager instanceof TransactionSynchronizationRegistry) {
        synchronizationRegistry = (TransactionSynchronizationRegistry) transactionManager;
    } else {
        // todo this should be built
        synchronizationRegistry = new SimpleTransactionSynchronizationRegistry(transactionManager);
    }
    Assembler.getContext().put(TransactionSynchronizationRegistry.class.getName(), synchronizationRegistry);
    SystemInstance.get().setComponent(TransactionSynchronizationRegistry.class, synchronizationRegistry);
    try {
        this.containerSystem.getJNDIContext().bind("comp/TransactionSynchronizationRegistry", new TransactionSynchronizationRegistryWrapper());
    } catch (final NamingException e) {
        throw new OpenEJBException("Cannot bind java:comp/TransactionSynchronizationRegistry", e);
    }
    // JtaEntityManagerRegistry
    // todo this should be built
    final JtaEntityManagerRegistry jtaEntityManagerRegistry = new JtaEntityManagerRegistry(synchronizationRegistry);
    Assembler.getContext().put(JtaEntityManagerRegistry.class.getName(), jtaEntityManagerRegistry);
    SystemInstance.get().setComponent(JtaEntityManagerRegistry.class, jtaEntityManagerRegistry);
    logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) JtaEntityManagerRegistry(org.apache.openejb.persistence.JtaEntityManagerRegistry) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) SimpleTransactionSynchronizationRegistry(org.apache.openejb.core.SimpleTransactionSynchronizationRegistry) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) CoreUserTransaction(org.apache.openejb.core.CoreUserTransaction) NamingException(javax.naming.NamingException) SimpleTransactionSynchronizationRegistry(org.apache.openejb.core.SimpleTransactionSynchronizationRegistry) TransactionSynchronizationRegistryWrapper(org.apache.openejb.core.TransactionSynchronizationRegistryWrapper)

Example 28 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project tomee by apache.

the class BaseEjbProxyHandler method getLiveHandleRegistry.

public ConcurrentMap getLiveHandleRegistry() {
    final BeanContext beanContext = getBeanContext();
    final ThreadContext tc = ThreadContext.getThreadContext();
    if (tc != null && tc.getBeanContext() != beanContext && /* parent bean */
    tc.getCurrentOperation() == Operation.BUSINESS) {
        ProxyRegistry registry = tc.get(ProxyRegistry.class);
        if (registry == null) {
            registry = new ProxyRegistry();
            tc.set(ProxyRegistry.class, registry);
        }
        return registry.liveHandleRegistry;
    } else {
        // use the tx if there
        final SystemInstance systemInstance = SystemInstance.get();
        final TransactionManager txMgr = systemInstance.getComponent(TransactionManager.class);
        try {
            final Transaction tx = txMgr.getTransaction();
            if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
                final TransactionSynchronizationRegistry registry = systemInstance.getComponent(TransactionSynchronizationRegistry.class);
                final String resourceKey = ProxyRegistry.class.getName();
                ConcurrentMap map = ConcurrentMap.class.cast(registry.getResource(resourceKey));
                if (map == null) {
                    map = new ConcurrentHashMap();
                    registry.putResource(resourceKey, map);
                    try {
                        final ConcurrentMap tmp = map;
                        tx.registerSynchronization(new Synchronization() {

                            @Override
                            public void beforeCompletion() {
                            // no-op
                            }

                            @Override
                            public void afterCompletion(final int status) {
                                tmp.clear();
                            }
                        });
                    } catch (final RollbackException e) {
                    // not really possible since we check the status
                    // let it go to default
                    }
                }
                return map;
            }
        } catch (final SystemException e) {
        // let it go to default
        }
        // back to default but it doesnt release the memory
        ProxyRegistry proxyRegistry = beanContext.get(ProxyRegistry.class);
        if (proxyRegistry == null) {
            proxyRegistry = new ProxyRegistry();
            beanContext.set(ProxyRegistry.class, proxyRegistry);
        }
        return proxyRegistry.liveHandleRegistry;
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) ConcurrentMap(java.util.concurrent.ConcurrentMap) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) BeanContext(org.apache.openejb.BeanContext) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) SystemInstance(org.apache.openejb.loader.SystemInstance) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 29 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project tomee by apache.

the class JavaLookupTest method test.

public void test() throws Exception {
    final Assembler assembler = new Assembler();
    final ConfigurationFactory config = new ConfigurationFactory();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final InitialContext context = new InitialContext();
    assertTrue(context.lookup("java:openejb/TransactionManager") instanceof TransactionManager);
    assertTrue(context.lookup("java:comp/TransactionManager") instanceof TransactionManager);
    assertTrue(context.lookup("java:comp/UserTransaction") instanceof UserTransaction);
    assertTrue(context.lookup("java:comp/TransactionSynchronizationRegistry") instanceof TransactionSynchronizationRegistry);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) InitialContext(javax.naming.InitialContext)

Example 30 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project tomee by apache.

the class JavaLookupScopesTest method test.

public void test() throws Exception {
    final AppContext app;
    {
        final ConfigurationFactory config = new ConfigurationFactory();
        final Assembler assembler = new Assembler();
        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
        // Setup the descriptor information
        final EjbJar ejbJar = new EjbJar("testmodule");
        ejbJar.addEnterpriseBean(new SingletonBean(Bean.class));
        // Deploy the bean a second time to simulate situations
        // where the same java:module java:app java:global names
        // are re-declared in a compatible way
        ejbJar.addEnterpriseBean(new SingletonBean("Other", Bean.class));
        final EjbModule ejbModule = new EjbModule(ejbJar);
        final AppModule module = new AppModule(ejbModule);
        app = assembler.createApplication(config.configureApplication(module));
    }
    final BeanContext bean = app.getBeanContexts().get(0);
    final ModuleContext module = bean.getModuleContext();
    {
        // app context lookups
        final Context context = app.getAppJndiContext();
        assertTrue(context.lookup("app") instanceof Context);
        assertTrue(context.lookup("app/AppName") instanceof String);
        assertTrue(context.lookup("app/green") instanceof DataSource);
        assertTrue(context.lookup("app/testmodule") instanceof Context);
        assertTrue(context.lookup("app/testmodule/Bean") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
        assertEquals("testmodule", context.lookup("app/AppName"));
    }
    {
        // module context lookups
        final Context context = module.getModuleJndiContext();
        assertTrue(context.lookup("module") instanceof Context);
        assertTrue(context.lookup("module/ModuleName") instanceof String);
        assertTrue(context.lookup("module/blue") instanceof DataSource);
        assertTrue(context.lookup("module/Bean") instanceof Bean);
        assertTrue(context.lookup("module/Bean!" + Bean.class.getName()) instanceof Bean);
        assertEquals("testmodule", context.lookup("module/ModuleName"));
    // TODO the Module JNDI context *should* be able to see the App context
    }
    {
        final Context context = bean.getJndiContext();
        assertTrue(context.lookup("comp") instanceof Context);
        assertTrue(context.lookup("comp/EJBContext") instanceof EJBContext);
        assertTrue(context.lookup("comp/TimerService") instanceof TimerService);
        assertTrue(context.lookup("comp/TransactionManager") instanceof TransactionManager);
        assertTrue(context.lookup("comp/TransactionSynchronizationRegistry") instanceof TransactionSynchronizationRegistry);
        assertTrue(context.lookup("comp/WebServiceContext") instanceof WebServiceContext);
        assertTrue(context.lookup("comp/env") instanceof Context);
        assertTrue(context.lookup("comp/env") instanceof Context);
        assertTrue(context.lookup("comp/env/orange") instanceof DataSource);
        assertTrue(context.lookup("comp/env/" + Bean.class.getName()) instanceof Context);
        assertTrue(context.lookup("comp/env/" + Bean.class.getName() + "/red") instanceof DataSource);
        assertTrue(context.lookup("module") instanceof Context);
        assertTrue(context.lookup("module/ModuleName") instanceof String);
        assertTrue(context.lookup("module/blue") instanceof DataSource);
        assertTrue(context.lookup("module/Bean") instanceof Bean);
        assertTrue(context.lookup("module/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("module/Other") instanceof Bean);
        assertTrue(context.lookup("module/Other!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("app") instanceof Context);
        assertTrue(context.lookup("app/AppName") instanceof String);
        assertTrue(context.lookup("app/green") instanceof DataSource);
        assertTrue(context.lookup("app/testmodule") instanceof Context);
        assertTrue(context.lookup("app/testmodule/Bean") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global") instanceof Context);
        assertTrue(context.lookup("global/yellow") instanceof DataSource);
        assertTrue(context.lookup("global/testmodule") instanceof Context);
        assertTrue(context.lookup("global/testmodule/Bean") instanceof Bean);
        assertTrue(context.lookup("global/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global/testmodule/Other") instanceof Bean);
        assertTrue(context.lookup("global/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
        assertEquals("testmodule", context.lookup("app/AppName"));
        assertEquals("testmodule", context.lookup("module/ModuleName"));
    }
}
Also used : InitialContext(javax.naming.InitialContext) BeanContext(org.apache.openejb.BeanContext) ModuleContext(org.apache.openejb.ModuleContext) WebServiceContext(javax.xml.ws.WebServiceContext) AppContext(org.apache.openejb.AppContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) EJBContext(javax.ejb.EJBContext) AppModule(org.apache.openejb.config.AppModule) AppContext(org.apache.openejb.AppContext) EjbModule(org.apache.openejb.config.EjbModule) WebServiceContext(javax.xml.ws.WebServiceContext) TimerService(javax.ejb.TimerService) DataSource(javax.sql.DataSource) SingletonBean(org.apache.openejb.jee.SingletonBean) BeanContext(org.apache.openejb.BeanContext) SingletonBean(org.apache.openejb.jee.SingletonBean) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) ModuleContext(org.apache.openejb.ModuleContext) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)45 TransactionManager (javax.transaction.TransactionManager)20 Test (org.junit.Test)13 InitialContext (javax.naming.InitialContext)8 ContextTransactionManager (org.wildfly.transaction.client.ContextTransactionManager)8 Synchronization (javax.transaction.Synchronization)7 ServiceName (org.jboss.msc.service.ServiceName)7 NamingException (javax.naming.NamingException)6 TransactionSynchronizationRegistryImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)5 HashMap (java.util.HashMap)5 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)5 UserTransaction (javax.transaction.UserTransaction)4 ValidatorFactory (javax.validation.ValidatorFactory)4 BinderService (org.jboss.as.naming.service.BinderService)4 BeanManager (javax.enterprise.inject.spi.BeanManager)3 DataSource (javax.sql.DataSource)3 BeanManagerAfterDeploymentValidation (org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation)3 PersistenceUnitServiceImpl (org.jboss.as.jpa.service.PersistenceUnitServiceImpl)3 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)3 PersistenceUnitService (org.jboss.as.jpa.spi.PersistenceUnitService)3