Search in sources :

Example 91 with TransactionManager

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

the class TransactionSubsystemAdd method performBoottime.

@Override
protected void performBoottime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException {
    checkIfNodeIdentifierIsDefault(context, model);
    boolean jts = model.hasDefined(JTS) && model.get(JTS).asBoolean();
    final Resource subsystemResource = context.readResourceFromRoot(PathAddress.pathAddress(TransactionExtension.SUBSYSTEM_PATH));
    final List<ServiceName> deps = new LinkedList<>();
    for (Resource.ResourceEntry re : subsystemResource.getChildren(CM_RESOURCE)) {
        deps.add(TxnServices.JBOSS_TXN_CMR.append(re.getName()));
    }
    //recovery environment
    performRecoveryEnvBoottime(context, model, jts, deps);
    //core environment
    performCoreEnvironmentBootTime(context, model);
    //coordinator environment
    performCoordinatorEnvBoottime(context, model, jts);
    //object store
    performObjectStoreBoottime(context, model);
    //always propagate the transaction context
    //TODO: need a better way to do this, but this value gets cached in a static
    //so we need to make sure we set it before anything tries to read it
    jtsPropertyManager.getJTSEnvironmentBean().setAlwaysPropagateContext(true);
    context.addStep(new AbstractDeploymentChainStep() {

        protected void execute(final DeploymentProcessorTarget processorTarget) {
            processorTarget.addDeploymentProcessor(TransactionExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_TRANSACTION_ROLLBACK_ACTION, new TransactionLeakRollbackProcessor());
            processorTarget.addDeploymentProcessor(TransactionExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_TRANSACTIONS_EE_CONCURRENCY, new EEConcurrencyContextHandleFactoryProcessor());
            processorTarget.addDeploymentProcessor(TransactionExtension.SUBSYSTEM_NAME, Phase.INSTALL, Phase.INSTALL_TRANSACTION_BINDINGS, new TransactionJndiBindingProcessor());
            processorTarget.addDeploymentProcessor(TransactionExtension.SUBSYSTEM_NAME, Phase.DEPENDENCIES, Phase.DEPENDENCIES_TRANSACTIONS, new TransactionDependenciesProcessor());
            processorTarget.addDeploymentProcessor(TransactionExtension.SUBSYSTEM_NAME, Phase.DEPENDENCIES, Phase.DEPENDENCIES_TRANSACTIONS, new CompensationsDependenciesDeploymentProcessor());
        }
    }, OperationContext.Stage.RUNTIME);
    //bind the TransactionManger and the TSR into JNDI
    final BinderService tmBinderService = new BinderService("TransactionManager");
    final ServiceBuilder<ManagedReferenceFactory> tmBuilder = context.getServiceTarget().addService(ContextNames.JBOSS_CONTEXT_SERVICE_NAME.append("TransactionManager"), tmBinderService);
    tmBuilder.addDependency(ContextNames.JBOSS_CONTEXT_SERVICE_NAME, ServiceBasedNamingStore.class, tmBinderService.getNamingStoreInjector());
    tmBuilder.addDependency(TransactionManagerService.SERVICE_NAME, javax.transaction.TransactionManager.class, new Injector<javax.transaction.TransactionManager>() {

        @Override
        public void inject(final javax.transaction.TransactionManager value) throws InjectionException {
            tmBinderService.getManagedObjectInjector().inject(new ValueManagedReferenceFactory(new ImmediateValue<Object>(value)));
        }

        @Override
        public void uninject() {
            tmBinderService.getManagedObjectInjector().uninject();
        }
    });
    tmBuilder.install();
    final BinderService tmLegacyBinderService = new BinderService("TransactionManager");
    final ServiceBuilder<ManagedReferenceFactory> tmLegacyBuilder = context.getServiceTarget().addService(ContextNames.JAVA_CONTEXT_SERVICE_NAME.append("TransactionManager"), tmLegacyBinderService);
    tmLegacyBuilder.addDependency(ContextNames.JAVA_CONTEXT_SERVICE_NAME, ServiceBasedNamingStore.class, tmLegacyBinderService.getNamingStoreInjector());
    tmLegacyBuilder.addDependency(TransactionManagerService.SERVICE_NAME, javax.transaction.TransactionManager.class, new Injector<javax.transaction.TransactionManager>() {

        @Override
        public void inject(final javax.transaction.TransactionManager value) throws InjectionException {
            tmLegacyBinderService.getManagedObjectInjector().inject(new ValueManagedReferenceFactory(new ImmediateValue<Object>(value)));
        }

        @Override
        public void uninject() {
            tmLegacyBinderService.getManagedObjectInjector().uninject();
        }
    });
    tmLegacyBuilder.install();
    final BinderService tsrBinderService = new BinderService("TransactionSynchronizationRegistry");
    final ServiceBuilder<ManagedReferenceFactory> tsrBuilder = context.getServiceTarget().addService(ContextNames.JBOSS_CONTEXT_SERVICE_NAME.append("TransactionSynchronizationRegistry"), tsrBinderService);
    tsrBuilder.addDependency(ContextNames.JBOSS_CONTEXT_SERVICE_NAME, ServiceBasedNamingStore.class, tsrBinderService.getNamingStoreInjector());
    tsrBuilder.addDependency(TransactionSynchronizationRegistryService.SERVICE_NAME, TransactionSynchronizationRegistry.class, new Injector<TransactionSynchronizationRegistry>() {

        @Override
        public void inject(final TransactionSynchronizationRegistry value) throws InjectionException {
            tsrBinderService.getManagedObjectInjector().inject(new ValueManagedReferenceFactory(new ImmediateValue<Object>(value)));
        }

        @Override
        public void uninject() {
            tsrBinderService.getManagedObjectInjector().uninject();
        }
    });
    tsrBuilder.install();
    // Install the UserTransactionAccessControlService
    final UserTransactionAccessControlService lookupControlService = new UserTransactionAccessControlService();
    context.getServiceTarget().addService(UserTransactionAccessControlService.SERVICE_NAME, lookupControlService).install();
    // Bind the UserTransaction into JNDI
    final UserTransactionBindingService userTransactionBindingService = new UserTransactionBindingService("UserTransaction");
    final ServiceBuilder<ManagedReferenceFactory> utBuilder = context.getServiceTarget().addService(ContextNames.JBOSS_CONTEXT_SERVICE_NAME.append("UserTransaction"), userTransactionBindingService);
    utBuilder.addDependency(ContextNames.JBOSS_CONTEXT_SERVICE_NAME, ServiceBasedNamingStore.class, userTransactionBindingService.getNamingStoreInjector()).addDependency(UserTransactionAccessControlService.SERVICE_NAME, UserTransactionAccessControlService.class, userTransactionBindingService.getUserTransactionAccessControlServiceInjector()).addDependency(UserTransactionService.SERVICE_NAME, UserTransaction.class, new ManagedReferenceInjector<UserTransaction>(userTransactionBindingService.getManagedObjectInjector()));
    utBuilder.install();
    // install the EE Concurrency transaction setup provider's service
    final TransactionSetupProviderService transactionSetupProviderService = new TransactionSetupProviderService();
    context.getServiceTarget().addService(ConcurrentServiceNames.TRANSACTION_SETUP_PROVIDER_SERVICE_NAME, transactionSetupProviderService).addDependency(TransactionManagerService.SERVICE_NAME, TransactionManager.class, transactionSetupProviderService.getTransactionManagerInjectedValue()).install();
}
Also used : TransactionLeakRollbackProcessor(org.jboss.as.txn.deployment.TransactionLeakRollbackProcessor) EEConcurrencyContextHandleFactoryProcessor(org.jboss.as.txn.ee.concurrency.EEConcurrencyContextHandleFactoryProcessor) TransactionDependenciesProcessor(org.jboss.as.txn.deployment.TransactionDependenciesProcessor) BinderService(org.jboss.as.naming.service.BinderService) InjectionException(org.jboss.msc.inject.InjectionException) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ValueManagedReferenceFactory(org.jboss.as.naming.ValueManagedReferenceFactory) UserTransaction(javax.transaction.UserTransaction) Resource(org.jboss.as.controller.registry.Resource) UserTransactionBindingService(org.jboss.as.txn.service.UserTransactionBindingService) LinkedList(java.util.LinkedList) TransactionSetupProviderService(org.jboss.as.txn.ee.concurrency.TransactionSetupProviderService) DeploymentProcessorTarget(org.jboss.as.server.DeploymentProcessorTarget) ServiceName(org.jboss.msc.service.ServiceName) ValueManagedReferenceFactory(org.jboss.as.naming.ValueManagedReferenceFactory) ContextTransactionManager(org.wildfly.transaction.client.ContextTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) AbstractDeploymentChainStep(org.jboss.as.server.AbstractDeploymentChainStep) UserTransactionAccessControlService(org.jboss.as.txn.service.UserTransactionAccessControlService) TransactionManager(javax.transaction.TransactionManager) TransactionJndiBindingProcessor(org.jboss.as.txn.deployment.TransactionJndiBindingProcessor)

Example 92 with TransactionManager

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

the class TransactionRollbackSetupAction method changeDepth.

private boolean changeDepth(int increment) {
    Holder holder = depth.get();
    if (holder == null) {
        //if there is a transaction active initially we just track the depth
        //and don't actually close it, because we don't 'own' the transaction
        //this can happen when running async listeners outside the context of a request
        holder = new Holder();
        try {
            final TransactionManager tm = transactionManager.getOptionalValue();
            if (tm != null) {
                holder.actuallyCleanUp = !isTransactionActive(tm, tm.getStatus());
            }
            depth.set(holder);
        } catch (Exception e) {
            TransactionLogger.ROOT_LOGGER.unableToGetTransactionStatus(e);
        }
    }
    holder.depth += increment;
    if (holder.depth == 0) {
        depth.set(null);
        return holder.actuallyCleanUp;
    }
    return false;
}
Also used : TransactionManager(javax.transaction.TransactionManager) SystemException(javax.transaction.SystemException) StartException(org.jboss.msc.service.StartException)

Example 93 with TransactionManager

use of javax.transaction.TransactionManager in project aries by apache.

the class TxDBServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    pw = response.getWriter();
    pw.write(LOGO_HEADER);
    // Get the bundle context from ServletContext attributes
    BundleContext ctx = (BundleContext) getServletContext().getAttribute(OSGI_BUNDLECONTEXT_ATTRIBUTE);
    pw.println("<html>");
    pw.println("<head>");
    pw.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"tableTemplate.css\"/>");
    pw.println("<body>");
    pw.println("<h2 align=center><p><font face=\"Tahoma\">Sample Application for JDBC usage in JTA Transactions.</font></h2>");
    pw.println("<p><p>");
    ServiceReference tmServiceRef = ctx.getServiceReference("javax.transaction.TransactionManager");
    ServiceReference derbyServiceRef = ctx.getServiceReference(DataSourceFactory.class.getName());
    if (tmServiceRef == null || derbyServiceRef == null) {
        pw.println("<font face=\"Tahoma\">TransactionManager or Derby driver are not available in the OSGI registry.</font><br>");
    } else {
        TransactionManager tm = (TransactionManager) ctx.getService(tmServiceRef);
        DataSourceFactory derbyRegistry = (DataSourceFactory) ctx.getService(derbyServiceRef);
        try {
            // Set the needed properties for the database connection
            Properties props = new Properties();
            props.put(DataSourceFactory.JDBC_URL, "jdbc:derby:txDemo");
            props.put(DataSourceFactory.JDBC_DATABASE_NAME, "txDemo");
            props.put(DataSourceFactory.JDBC_USER, "");
            props.put(DataSourceFactory.JDBC_PASSWORD, "");
            XADataSource xaDataSource = derbyRegistry.createXADataSource(props);
            pw.println("<center><form><table><tr><td align='right'>Value: </td><td align=left><input type='text' name='value' value='' size='12'/><input type='submit' name='action' value='InsertAndCommit' size='100'/></td></tr>");
            pw.println("<tr><td align='right'>Value: </td><td align=left><input type='text' name='value' value='' size='12'/><input type='submit' name='action' value='InsertAndRollback' size='100'/></center></td></tr>");
            pw.println("<tr colspan='2' align='center'><td>&nbsp;</td><td><input type='submit' name='action' value='cleanTable' size='100' />&nbsp;<input type='submit' name='action' value='printTable' size='100'/></td><tr></table></form></center>");
            String value = request.getParameter("value");
            String action = request.getParameter("action");
            if (action != null && action.equals("InsertAndCommit")) {
                insertIntoTransaction(xaDataSource, tm, value, true);
            } else if (action != null && action.equals("InsertAndRollback")) {
                insertIntoTransaction(xaDataSource, tm, value, false);
            } else if (action != null && action.equals("cleanTable")) {
                cleanTable(xaDataSource);
            }
            printTable(xaDataSource);
        } catch (Exception e) {
            pw.println("<font face=\"Tahoma\">Unexpected exception occurred " + e.toString() + ".</font><br>");
            e.printStackTrace(pw);
        }
    }
    pw.println("</body>");
    pw.println("</html>");
    pw.flush();
}
Also used : XADataSource(javax.sql.XADataSource) DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) TransactionManager(javax.transaction.TransactionManager) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SQLException(java.sql.SQLException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 94 with TransactionManager

use of javax.transaction.TransactionManager in project aries by apache.

the class BaseNameSpaceHandlerSetup method setUp.

@Before
public void setUp() throws InvalidSyntaxException {
    control = EasyMock.createControl();
    b = control.createMock(Bundle.class);
    TransactionManager tm = control.createMock(TransactionManager.class);
    Coordinator coordinator = control.createMock(Coordinator.class);
    control.replay();
    namespaceHandler = new TxNamespaceHandler();
    namespaceHandler.setTm(tm);
    namespaceHandler.setCoordinator(coordinator);
    String[] namespaces = new String[] { "http://aries.apache.org/xmlns/transactions/v2.0.0" };
    nhri = new DummyNamespaceHandlerRegistry();
    nhri.addNamespaceHandlers(namespaces, namespaceHandler);
}
Also used : Bundle(org.osgi.framework.Bundle) TransactionManager(javax.transaction.TransactionManager) TxNamespaceHandler(org.apache.aries.transaction.parsing.TxNamespaceHandler) Coordinator(org.osgi.service.coordinator.Coordinator) Before(org.junit.Before)

Example 95 with TransactionManager

use of javax.transaction.TransactionManager in project aries by apache.

the class InterceptorTest method postCallWithTransaction.

private void postCallWithTransaction(Throwable th, boolean expectRollback, boolean failCoordination) throws Throwable {
    IMocksControl c = EasyMock.createControl();
    TransactionManager tm = c.createMock(TransactionManager.class);
    Coordinator coordinator = c.createMock(Coordinator.class);
    ComponentTxData txData = new ComponentTxData(AnnotatedPojo.class);
    TxInterceptorImpl sut = new TxInterceptorImpl(tm, coordinator, txData);
    Transaction tran = c.createMock(Transaction.class);
    if (expectRollback) {
        tran.setRollbackOnly();
        EasyMock.expectLastCall();
    }
    Coordination coordination = c.createMock(Coordination.class);
    coordination.end();
    if (failCoordination) {
        EasyMock.expectLastCall().andThrow(coordinationException(th));
    } else {
        EasyMock.expectLastCall();
    }
    c.replay();
    TransactionToken tt = new TransactionToken(tran, null, TransactionAttribute.REQUIRED);
    tt.setCoordination(coordination);
    sut.postCallWithException(null, this.getClass().getMethods()[0], th, tt);
    c.verify();
}
Also used : IMocksControl(org.easymock.IMocksControl) Transaction(javax.transaction.Transaction) Coordination(org.osgi.service.coordinator.Coordination) TransactionManager(javax.transaction.TransactionManager) Coordinator(org.osgi.service.coordinator.Coordinator)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5