001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.pool;
018    
019    import javax.jms.JMSException;
020    import javax.jms.Session;
021    import javax.transaction.RollbackException;
022    import javax.transaction.Status;
023    import javax.transaction.SystemException;
024    import javax.transaction.TransactionManager;
025    import javax.transaction.xa.XAResource;
026    
027    import org.apache.activemq.ActiveMQConnection;
028    import org.apache.commons.pool.ObjectPoolFactory;
029    
030    /**
031     * An XA-aware connection pool.  When a session is created and an xa transaction is active,
032     * the session will automatically be enlisted in the current transaction.
033     *
034     * @author gnodet
035     */
036    public class XaConnectionPool extends ConnectionPool {
037    
038        private TransactionManager transactionManager;
039    
040        public XaConnectionPool(ActiveMQConnection connection, TransactionManager transactionManager) {
041            super(connection);
042            this.transactionManager = transactionManager;
043        }
044    
045        /**
046         * @deprecated
047         */
048        public XaConnectionPool(ActiveMQConnection connection, ObjectPoolFactory poolFactory, TransactionManager transactionManager) {
049            this(connection, transactionManager);
050        }
051    
052        @Override
053        public Session createSession(boolean transacted, int ackMode) throws JMSException {
054            try {
055                boolean isXa = (transactionManager != null && transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION);
056                if (isXa) {
057                    transacted = true;
058                    ackMode = Session.SESSION_TRANSACTED;
059                }
060                PooledSession session = (PooledSession) super.createSession(transacted, ackMode);
061                if (isXa) {
062                    session.setIgnoreClose(true);
063                    session.setIsXa(true);
064                    transactionManager.getTransaction().registerSynchronization(new Synchronization(session));
065                    incrementReferenceCount();
066                    transactionManager.getTransaction().enlistResource(createXaResource(session));
067                } else {
068                    session.setIgnoreClose(false);
069                }
070                return session;
071            } catch (RollbackException e) {
072                final JMSException jmsException = new JMSException("Rollback Exception");
073                jmsException.initCause(e);
074                throw jmsException;
075            } catch (SystemException e) {
076                final JMSException jmsException = new JMSException("System Exception");
077                jmsException.initCause(e);
078                throw jmsException;
079            }
080        }
081    
082        protected XAResource createXaResource(PooledSession session) throws JMSException {
083            return session.getXAResource();
084        }
085    
086        protected class Synchronization implements javax.transaction.Synchronization {
087            private final PooledSession session;
088    
089            private Synchronization(PooledSession session) {
090                this.session = session;
091            }
092    
093            public void beforeCompletion() {
094            }
095    
096            public void afterCompletion(int status) {
097                try {
098                    // This will return session to the pool.
099                    session.setIgnoreClose(false);
100                    session.close();
101                    session.setIgnoreClose(true);
102                    session.setIsXa(false);
103                    decrementReferenceCount();
104                } catch (JMSException e) {
105                    throw new RuntimeException(e);
106                }
107            }
108        }
109    }