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, ObjectPoolFactory poolFactory, TransactionManager transactionManager) {
041            super(connection, poolFactory);
042            this.transactionManager = transactionManager;
043        }
044    
045        public Session createSession(boolean transacted, int ackMode) throws JMSException {
046            try {
047                boolean isXa = (transactionManager != null && transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION);
048                if (isXa) {
049                    transacted = true;
050                    ackMode = Session.SESSION_TRANSACTED;
051                }
052                PooledSession session = (PooledSession) super.createSession(transacted, ackMode);
053                if (isXa) {
054                    session.setIgnoreClose(true);
055                    session.setIsXa(true);
056                    transactionManager.getTransaction().registerSynchronization(new Synchronization(session));
057                    incrementReferenceCount();
058                    transactionManager.getTransaction().enlistResource(createXaResource(session));
059                }
060                return session;
061            } catch (RollbackException e) {
062                final JMSException jmsException = new JMSException("Rollback Exception");
063                jmsException.initCause(e);
064                throw jmsException;
065            } catch (SystemException e) {
066                final JMSException jmsException = new JMSException("System Exception");
067                jmsException.initCause(e);
068                throw jmsException;
069            }
070        }
071    
072        protected XAResource createXaResource(PooledSession session) throws JMSException {
073            return session.getXAResource();
074        }
075        
076        
077        protected class Synchronization implements javax.transaction.Synchronization {
078            private final PooledSession session;
079    
080            private Synchronization(PooledSession session) {
081                this.session = session;
082            }
083    
084            public void beforeCompletion() {
085            }
086            
087            public void afterCompletion(int status) {
088                try {
089                    // This will return session to the pool.
090                    session.setIgnoreClose(false);
091                    session.close();
092                    session.setIsXa(false);
093                    decrementReferenceCount();
094                } catch (JMSException e) {
095                    throw new RuntimeException(e);
096                }
097            }
098        }
099        
100    }