Package javax.transaction

Examples of javax.transaction.UserTransaction


                 emf );
        env.set( EnvironmentName.TRANSACTION_MANAGER,
                 TransactionManagerServices.getTransactionManager() );
        env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );

        UserTransaction ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );
        ut.commit();

        //      EntityManager em = emf.createEntityManager();
        //      SessionInfo sInfo = em.find( SessionInfo.class, 1 );
        //      assertNotNull( sInfo );
        //      //System.out.println( "session creation : " + sInfo.getVersion() );
        //      em.close();

        List<?> list = new ArrayList<Object>();

        // insert and commit
        ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        ksession.setGlobal( "list",
                            list );
        ksession.insert( 1 );
        ksession.insert( 2 );
        ut.commit();
//
        // insert and rollback
        ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        ksession.insert( 3 );
        ut.rollback();

        // check we rolled back the state changes from the 3rd insert
        ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();       
        ksession.fireAllRules();
        ut.commit();
        assertEquals( 2,
                      list.size() );

//        // insert and commit
        ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        ksession.insert( 3 );
        ksession.insert( 4 );
        ut.commit();

        // rollback again, this is testing that we can do consecutive rollbacks and commits without issue
        ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        ksession.insert( 5 );
        ksession.insert( 6 );
        ut.rollback();

        ksession.fireAllRules();

        assertEquals( 4,
                      list.size() );
       
        // now load the ksession
        ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( ksession.getId(), kbase, null, env );
       
        ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        ksession.insert( 7 );
        ksession.insert( 8 );
        ut.commit();

        ksession.fireAllRules();

        assertEquals( 6,
                      list.size() );
View Full Code Here


        env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );

        StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );
        int id = ksession.getId();
       
        UserTransaction ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
       
        ProcessInstance processInstance = ksession.startProcess( "org.drools.test.TestProcess" );
        ksession.insert( "TestString" );
        System.out.println( "Started process instance " + processInstance.getId() );

        TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
        WorkItem workItem = handler.getWorkItem();
        assertNotNull( workItem );

        ksession.getWorkItemManager().completeWorkItem( workItem.getId(),
                                                       null );
       
        workItem = handler.getWorkItem();
        assertNotNull( workItem );

        ut.commit();

        ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( id, kbase, null, env );
        processInstance = ksession.getProcessInstance( processInstance.getId() );
        assertNotNull( processInstance );
View Full Code Here

        SingleSessionCommandService service = new SingleSessionCommandService( kbase,
                                                                               config,
                                                                               env );
        int sessionId = service.getSessionId();

        UserTransaction ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
        ut.begin();
        StartProcessCommand startProcessCommand = new StartProcessCommand();
        startProcessCommand.setProcessId( "org.drools.test.TestProcess" );
        ProcessInstance processInstance = service.execute( startProcessCommand );
        System.out.println( "Started process instance " + processInstance.getId() );
        ut.commit();

        TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
        WorkItem workItem = handler.getWorkItem();
        assertNotNull( workItem );
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
        getProcessInstanceCommand.setProcessInstanceId( processInstance.getId() );
        processInstance = service.execute( getProcessInstanceCommand );
        assertNotNull( processInstance );
        ut.commit();
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
        completeWorkItemCommand.setWorkItemId( workItem.getId() );
        service.execute( completeWorkItemCommand );
        ut.commit();

        workItem = handler.getWorkItem();
        assertNotNull( workItem );
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        getProcessInstanceCommand = new GetProcessInstanceCommand();
        getProcessInstanceCommand.setProcessInstanceId( processInstance.getId() );
        processInstance = service.execute( getProcessInstanceCommand );
        ut.commit();
        assertNotNull( processInstance );
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        completeWorkItemCommand = new CompleteWorkItemCommand();
        completeWorkItemCommand.setWorkItemId( workItem.getId() );
        service.execute( completeWorkItemCommand );
        ut.commit();

        workItem = handler.getWorkItem();
        assertNotNull( workItem );
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        getProcessInstanceCommand = new GetProcessInstanceCommand();
        getProcessInstanceCommand.setProcessInstanceId( processInstance.getId() );
        processInstance = service.execute( getProcessInstanceCommand );
        ut.commit();
        assertNotNull( processInstance );
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        completeWorkItemCommand = new CompleteWorkItemCommand();
        completeWorkItemCommand.setWorkItemId( workItem.getId() );
        service.execute( completeWorkItemCommand );
        ut.commit();

        workItem = handler.getWorkItem();
        assertNull( workItem );
        service.dispose();

        service = new SingleSessionCommandService( sessionId,
                                                   kbase,
                                                   config,
                                                   env );
        ut.begin();
        getProcessInstanceCommand = new GetProcessInstanceCommand();
        getProcessInstanceCommand.setProcessInstanceId( processInstance.getId() );
        processInstance = service.execute( getProcessInstanceCommand );
        ut.commit();
        assertNull( processInstance );
        service.dispose();
    }
View Full Code Here

     * @output the table created by the client must to be correctly commited.
     * @throws Exception if an error during the tests occurs.
     */
    public void testUsingClientTrans() throws Exception {
        // gets the transaction
        UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
        // starts the transaction
        utx.begin();
        // creates a table in the second database
        ItfDatabaseManager slsbDatabaseManager = EJBHelper.getBeanRemoteInstance(SLSBDatabaseManager.class,
                ItfDatabaseManager.class);
        slsbDatabaseManager.insertTable(DATABASE_2, ItfContainerTransaction.TABLE);
        /*
         * Calls a bean method that forces an error and makes a roll back in the
         * method. The bean is using a required new attribute, so the bean
         * cannot use the same transaction that the client.
         */
        try {
            getRuntimeBean().insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
        } catch (EJBException e) {
            logger.debug("The bean threw an expected exception {0}", e);
        }
        // commits the transaction
        utx.commit();
        /*
         * the bean roll back must not to make any influence in the user
         * transaction context. So, the client table must be create correctly,
         * and consequently the "select" query in the table must not to throw
         * exception.
 
View Full Code Here

     * bean and the client, in this case, must to use the same transaction. So,
     * the rollback must to be done in the bean also.
     * @throws Exception if an error during the transaction occurs.
     */
    private void executeErrorTransaction() throws Exception {
        UserTransaction utx = getUserTransaction();
        // starts the transaction
        utx.begin();
        // call the method that must have the same transaction context
        sfsbContainerTransaction.insertCorrectTableInBothDB(DATABASE_1, DATABASE_2);
        // rollback the transaction
        utx.rollback();
    }
View Full Code Here

     * Gets a new transaction.
     * @return the class UserTransaction.
     * @throws Exception if a lookup error occurs.
     */
    protected UserTransaction getUserTransaction() throws Exception {
        UserTransaction utx = TransactionHelper.getInternalUserTransaction();
        logger.debug("Transaction status after get = {0} ", new Integer(utx.getStatus()));
        return utx;
    }
View Full Code Here

    /**
     * Makes a rollback in the transaction that is active.
     * @throws Exception if an error occurs during the rollback.
     */
    public void cleanTransaction() throws Exception {
        UserTransaction utx = TransactionHelper.getInternalUserTransaction();
        try {
            if (transactionIsActive()) {
                utx.rollback();
           }
        } catch (Exception e) {
            throw new Exception("Cannot clean the transaction. The test cannot be started", e);
        }
    }
View Full Code Here

     * Verifies if the transaction in the client side is active.
     * @return true if the transaction is active, false otherwise.
     * @throws Exception id a lookup error occurs
     */
    protected boolean transactionIsActive() throws Exception {
        UserTransaction utx = TransactionHelper.getInternalUserTransaction();
        boolean bolResult = false;
        if (utx != null) {
            if (utx.getStatus() == Status.STATUS_ACTIVE) {
                bolResult = true;
            }
        }
        return bolResult;
    }
View Full Code Here

     * @input -
     * @output an exception when the method verifies if the table exists.
     * @throws Exception if an error occurs during the tests.
     */
    public void testSetRollbackOnlyWithUserTransaction() throws Exception {
        UserTransaction utx = getUserTransaction();
        utx.begin();
        try {
            ItfContainerTransaction sfsbContainerTransaction = getBean();
            // calls the setRollbackOnly
            try {
                sfsbContainerTransaction.setRollbackOnly(DATABASE_1, DATABASE_2);
            } catch (TransactionException e) {
                throw e.getParentException();
            }
            // tries to commit the transaction
            try {
                utx.commit();
                fail("The transaction is marked as rollback. The client cannot make the commit.");
            } catch (Exception e) {
                logger.debug("Expected exception {0}", e);
            }
        } finally {
View Full Code Here

     * @input -
     * @output the method execution without error.
     * @throws Exception if an error occurs during the tests.
     */
    public void testGetRollbackOnlyWithUserTransaction() throws Exception {
        UserTransaction utx = getUserTransaction();

        utx.begin();
        testGetRollbackOnly();
        utx.commit();
    }
View Full Code Here

TOP

Related Classes of javax.transaction.UserTransaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.