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