Package javax.transaction

Examples of javax.transaction.Transaction


    /**
     * Marks the transaction for rollback.
     */
    protected void markTransactionRollback() {
        // Look if there is a transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }
View Full Code Here


    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        logger.debug("Calling Never TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }
View Full Code Here

     */
    public Connection getConnection(final String username, final String password) throws SQLException {
        IManagedConnection mc = null;

        // Get the current Transaction
        Transaction tx = null;
        try {
            tx = this.tm.getTransaction();
        } catch (NullPointerException n) {
            // current is null: we are not in EasyBeans Server.
            this.logger.error("ConnectionManager: should not be used outside a EasyBeans Server");
        } catch (SystemException e) {
            this.logger.error("ConnectionManager: getTransaction failed", e);
        }
        this.logger.debug("Tx = {0}", tx);

        // Get a ManagedConnection in the pool for this user
        mc = openConnection(username, tx);
        Connection ret = mc.getConnection();

        // Enlist XAResource if we are actually in a transaction
        if (tx != null) {
            if (mc.getOpenCount() == 1) { // Only if first/only thread
                try {
                    this.logger.debug("enlist XAResource on {0}", tx);
                    tx.enlistResource(mc.getXAResource());
                    ret.setAutoCommit(false);
                } catch (RollbackException e) {
                    // Although tx has been marked to be rolled back,
                    // XAResource has been correctly enlisted.
                    this.logger.warn("XAResource enlisted, but tx is marked rollback", e);
View Full Code Here

        } else {
            freeItem(mc);
        }

        // delist Resource if in transaction
        Transaction tx = null;
        try {
            tx = this.tm.getTransaction();
        } catch (NullPointerException n) {
            // current is null: we are not in EasyBeans Server.
            this.logger.error("Pool: should not be used outside a EasyBeans Server", n);
        } catch (SystemException e) {
            this.logger.error("Pool: getTransaction failed:", e);
        }
        if (tx != null && mc.isClosed()) {
            try {
                tx.delistResource(mc.getXAResource(), flag);
            } catch (Exception e) {
                this.logger.error("Pool: Exception while delisting resource:", e);
            }
        }
        return true;
View Full Code Here

    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        logger.debug("Calling Mandatory TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }
View Full Code Here

    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        logger.debug("Calling BMT TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }

        logger.debug("Transaction found = {0}", transaction);

        /*
         * When a client invokes a business method via one of the enterprise
         * bean's client view interfaces, the container suspends any transaction
         * that may be associated with the client request. If there is a
         * transaction associated with the instance (this would happen if a
         * stateful session bean instance started the transaction in some
         * previous business method), the container associates the method
         * execution with this transaction. If there are interceptor methods
         * associated with the bean instances, these actions are taken before
         * the interceptor methods are invoked.
         */

        Transaction suspendedTransaction = null;
        if (transaction != null) {
            try {
                logger.debug("Suspending transaction {0}", transaction);
                suspendedTransaction = getTransactionManager().suspend();
            } catch (SystemException se) {
                throw new EJBException("Cannot call suspend() on the transaction manager.", se);
            }
        }

        boolean gotBusinessException = false;
        try {
            return invocationContext.proceed();
        } catch (Exception e) {
            gotBusinessException = true;
            handleBeanManagedException(invocationContext, e);
            // Shouldn't come here
            return null;
        } finally {
            if (!gotBusinessException) {
                /**
                 * If a stateless session bean instance starts a transaction in a
                 * business method or interceptor method, it must commit the
                 * transaction before the business method (or all its interceptor
                 * methods) returns. The container must detect the case in which a
                 * transaction was started, but not completed, in the business
                 * method or interceptor method for the business method, and handle
                 * it as follows:
                 * <ul>
                 * <li>Log this as an application error to alert the System
                 * Administrator.</li>
                 * <li>Roll back the started transaction</li>
                 * <li>Discard the instance of the session bean</li>
                 * <li>Throw the javax.ejb.EJBException[53]. If the EJB 2.1 client
                 * view is used, the container should throw java.rmi.RemoteException
                 * if the client is a remote client, or throw the
                 * javax.ejb.EJBException if the client is a local client.</li>
                 */
                Transaction transactionAfter = null;
                try {
                    transactionAfter = getTransactionManager().getTransaction();
                } catch (SystemException se) {
                    throw new EJBException("Cannot get the current transaction on transaction manager.", se);
                }
                if (transactionAfter != null) {
                    int transactionStatus = transactionAfter.getStatus();
                    // There is a transaction and it was not committed
                    if (transactionStatus != STATUS_COMMITTED) {
                        String errMsg = "Transaction started by the bean but not committed.";
                        // Log error
                        logger.error(errMsg);
                        // Rollback
                        transactionAfter.rollback();
                        //TODO: discard
                        // Throw Exception
                        throw new EJBException(errMsg);
                    }
                }
View Full Code Here

    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        logger.debug("Calling Required TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }

        logger.debug("Transaction found = {0}", transaction);

        /*
         * If the client invokes the enterprise bean's method while the client
         * is not associated with a transaction context, the container
         * automatically starts a new transaction before delegating a method
         * call to the enterprise bean business method.
         */
        boolean startedTransaction = false;
        if (transaction == null) {
            try {
                getTransactionManager().begin();
                startedTransaction = true;
            } catch (NotSupportedException nse) {
                throw new EJBException("Transaction Manager implementation does not support nested transactions.", nse);
            } catch (SystemException se) {
                throw new EJBException("Cannot call begin() on the transaction manager.", se);
            }
        }
        // else
        /*
         * If a client invokes the enterprise bean's method while the client is
         * associated with a transaction context, the container invokes the
         * enterprise bean's method in the client's transaction context.
         */
        boolean gotBusinessException = false;
        try {
            return invocationContext.proceed();

        } catch (Exception e) {
            gotBusinessException = true;

            // Chapter 14.3.1
            // Runs in the context of the client
            if (!startedTransaction) {
                handleContextClientTransaction(invocationContext, e);
            } else {
                // Container's transaction
                handleContextContainerTransaction(invocationContext, e);
            }
            // Shouldn't come here
            return null;
        } finally {
            if (!gotBusinessException) {

                // only do some operations if transaction has been started
                // before
                // invoking the method.
                if (startedTransaction) {

                    // sanity check.
                    Transaction transactionAfter = null;
                    try {
                        transactionAfter = getTransactionManager().getTransaction();
                    } catch (SystemException se) {
                        throw new EJBException("Cannot get the current transaction on transaction manager.", se);
                    }
View Full Code Here

    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        logger.debug("Calling Supports TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }
View Full Code Here

    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        logger.debug("Calling BMT TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }

        logger.debug("Transaction found = {0}", transaction);

        /*
         * When a client invokes a business method via one of the enterprise
         * bean's client view interfaces, the container suspends any transaction
         * that may be associated with the client request. If there is a
         * transaction associated with the instance (this would happen if a
         * stateful session bean instance started the transaction in some
         * previous business method), the container associates the method
         * execution with this transaction. If there are interceptor methods
         * associated with the bean instances, these actions are taken before
         * the interceptor methods are invoked.
         */

        Transaction suspendedTransaction = null;
        if (transaction != null) {
            try {
                logger.debug("Suspending transaction {0}", transaction);
                suspendedTransaction = getTransactionManager().suspend();
            } catch (SystemException se) {
                throw new EJBException("Cannot call suspend() on the transaction manager.", se);
            }
        }

        /*
         * In the case of a stateful session bean, it is possible that the
         * business method that started a transaction completes without
         * committing or rolling back the transaction. In such a case, the
         * container must retain the association between the transaction and the
         * instance across multiple client calls until the instance commits or
         * rolls back the transaction. When the client invokes the next business
         * method, the container must invoke the business method (and any
         * applicable interceptor methods for the bean) in this transaction
         * context.
         */

        // Get Bean and context
        EasyBeansSFSB statefulBean = (EasyBeansSFSB) invocationContext.getTarget();
        EZBSessionContext sessionContext = (EZBSessionContext) statefulBean.getEasyBeansContext();
        // Get bean transaction (if any)
        Transaction beanTransaction = sessionContext.getBeanTransaction();
        // resume transaction for the call
        if (beanTransaction != null) {
            logger.debug("Resuming bean transaction {0}", beanTransaction);
            try {
                getTransactionManager().resume(beanTransaction);
            } catch (InvalidTransactionException ite) {
                throw new EJBException(
                        "Cannot call resume() on the previous bean transaction. There is an invalid transaction", ite);
            } catch (IllegalStateException ise) {
                throw new EJBException(
                        "Cannot call resume() on the previous bean transaction. There is another associated transaction",
                        ise);
            } catch (SystemException se) {
                throw new EJBException(
                        "Cannot call resume() on the previous bean transaction. Unexpected error condition", se);
            }
        }

        try {
            return invocationContext.proceed();
        } catch (Exception e) {
            handleBeanManagedException(invocationContext, e);
            // Shouldn't come here
            return null;
        } finally {

            /*
             * Saves the current bean transaction if the business method that
             * started a transaction completes without committing or rolling
             * back the transaction.
             */
            Transaction transactionAfter = null;
            try {
                transactionAfter = getTransactionManager().getTransaction();
            } catch (SystemException se) {
                throw new EJBException("Cannot get the current transaction on transaction manager.", se);
            }
            if (transactionAfter != null) {
                int transactionStatus = transactionAfter.getStatus();
                // not yet committed or rollbacked --> save transaction for the next call.
                if (transactionStatus != STATUS_COMMITTED && transactionStatus != STATUS_ROLLEDBACK) {
                    logger.debug("Saving bean transaction {0}", transactionAfter);
                    sessionContext.setBeanTransaction(transactionAfter);
                } else {
View Full Code Here

    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        this.logger.debug("Calling Required TX interceptor");

        // Get current transaction
        Transaction transaction;
        try {
            transaction = getTransactionManager().getTransaction();
        } catch (SystemException se) {
            throw new EJBException("Cannot get the current transaction on transaction manager.", se);
        }

        this.logger.debug("Transaction found = {0}", transaction);

        /*
         * If the client invokes the enterprise bean's method while the client
         * is not associated with a transaction context, the container
         * automatically starts a new transaction before delegating a method
         * call to the enterprise bean business method.
         */
        boolean startedTransaction = false;
        if (transaction == null) {
            try {
                getTransactionManager().begin();
                startedTransaction = true;
            } catch (NotSupportedException nse) {
                throw new EJBException("Transaction Manager implementation does not support nested transactions.", nse);
            } catch (SystemException se) {
                throw new EJBException("Cannot call begin() on the transaction manager.", se);
            }
        }


        // Get XA Resource
        Object target = invocationContext.getTarget();
        EasyBeansMDB mdbInstance = null;
        XAResource xaResource = null;
        if (target instanceof EasyBeansMDB) {
            mdbInstance = (EasyBeansMDB) target;
            if (mdbInstance != null) {
                xaResource = mdbInstance.getXaResource();
            }
        }

        // Enlist XA Resource
        if (xaResource != null) {
            getTransactionManager().getTransaction().enlistResource(xaResource);
        }


        // else
        /*
         * If a client invokes the enterprise bean's method while the client is
         * associated with a transaction context, the container invokes the
         * enterprise bean's method in the client's transaction context.
         */
        boolean gotBusinessException = false;
        try {
            return invocationContext.proceed();

        } catch (Exception e) {
            gotBusinessException = true;

            // Chapter 14.3.1
            // Runs in the context of the client
            if (!startedTransaction) {
                handleContextClientTransaction(invocationContext, e);
            } else {
                // Container's transaction
                handleContextContainerTransaction(invocationContext, e);
            }
            // Shouldn't come here
            return null;
        } finally {
            if (!gotBusinessException) {

                // only do some operations if transaction has been started
                // before
                // invoking the method.
                if (startedTransaction) {

                    // sanity check.
                    Transaction transactionAfter = null;
                    try {
                        transactionAfter = getTransactionManager().getTransaction();
                    } catch (SystemException se) {
                        throw new EJBException("Cannot get the current transaction on transaction manager.", se);
                    }

                    if (transactionAfter == null) {
                        throw new RuntimeException("Transaction disappeared.");
                    }

                    /*
                     * The container attempts to commit the transaction when the
                     * business method has completed. The container performs the
                     * commit protocol before the method result is sent to the
                     * client.
                     */
                    try {
                        switch (getTransactionManager().getStatus()) {
                        case STATUS_ACTIVE:
                            // Delist XA Resource
                            if (xaResource != null) {
                                transactionAfter.delistResource(xaResource, XAResource.TMSUCCESS);
                            }
                            getTransactionManager().commit();
                            break;
                        case STATUS_MARKED_ROLLBACK:
                            // Delist XA Resource
                            if (xaResource != null) {
                                transactionAfter.delistResource(xaResource, XAResource.TMFAIL);
                            }
                            getTransactionManager().rollback();
                            break;
                        default:
                            throw new RuntimeException("Unexpected transaction status" + getTransactionManager().getStatus());
View Full Code Here

TOP

Related Classes of javax.transaction.Transaction

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.