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 transactionManager.getTransaction().registerSynchronization(new Synchronization(session)); 056 incrementReferenceCount(); 057 transactionManager.getTransaction().enlistResource(createXaResource(session)); 058 } 059 return session; 060 } catch (RollbackException e) { 061 final JMSException jmsException = new JMSException("Rollback Exception"); 062 jmsException.initCause(e); 063 throw jmsException; 064 } catch (SystemException e) { 065 final JMSException jmsException = new JMSException("System Exception"); 066 jmsException.initCause(e); 067 throw jmsException; 068 } 069 } 070 071 protected XAResource createXaResource(PooledSession session) throws JMSException { 072 return session.getXAResource(); 073 } 074 075 076 protected class Synchronization implements javax.transaction.Synchronization { 077 private final PooledSession session; 078 079 private Synchronization(PooledSession session) { 080 this.session = session; 081 } 082 083 public void beforeCompletion() { 084 } 085 086 public void afterCompletion(int status) { 087 try { 088 // This will return session to the pool. 089 session.setIgnoreClose(false); 090 session.close(); 091 decrementReferenceCount(); 092 } catch (JMSException e) { 093 throw new RuntimeException(e); 094 } 095 } 096 } 097 098 }