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 021 import org.apache.activemq.ActiveMQConnection; 022 import org.apache.activemq.ActiveMQSession; 023 import org.apache.activemq.AlreadyClosedException; 024 import org.apache.activemq.util.JMSExceptionSupport; 025 import org.apache.commons.pool.ObjectPool; 026 import org.apache.commons.pool.PoolableObjectFactory; 027 028 /** 029 * Represents the session pool for a given JMS connection. 030 * 031 * @version $Revision: 1.1 $ 032 */ 033 public class SessionPool implements PoolableObjectFactory { 034 private ConnectionPool connectionPool; 035 private SessionKey key; 036 private ObjectPool sessionPool; 037 038 public SessionPool(ConnectionPool connectionPool, SessionKey key, ObjectPool sessionPool) { 039 this.connectionPool = connectionPool; 040 this.key = key; 041 this.sessionPool = sessionPool; 042 sessionPool.setFactory(this); 043 } 044 045 public void close() throws Exception { 046 if (sessionPool != null) { 047 sessionPool.close(); 048 } 049 sessionPool = null; 050 } 051 052 public PooledSession borrowSession() throws JMSException { 053 try { 054 Object object = getSessionPool().borrowObject(); 055 return (PooledSession)object; 056 } catch (JMSException e) { 057 throw e; 058 } catch (Exception e) { 059 throw JMSExceptionSupport.create(e); 060 } 061 } 062 063 public void returnSession(PooledSession session) throws JMSException { 064 // lets check if we are already closed 065 getConnection(); 066 try { 067 getSessionPool().returnObject(session); 068 } catch (Exception e) { 069 throw JMSExceptionSupport.create("Failed to return session to pool: " + e, e); 070 } 071 } 072 073 public void invalidateSession(PooledSession session) throws JMSException { 074 try { 075 getSessionPool().invalidateObject(session); 076 } catch (Exception e) { 077 throw JMSExceptionSupport.create("Failed to invalidate session: " + e, e); 078 } 079 } 080 081 082 // PoolableObjectFactory methods 083 // ------------------------------------------------------------------------- 084 public Object makeObject() throws Exception { 085 return new PooledSession(createSession(), this); 086 } 087 088 public void destroyObject(Object o) throws Exception { 089 PooledSession session = (PooledSession)o; 090 session.getInternalSession().close(); 091 } 092 093 public boolean validateObject(Object o) { 094 return true; 095 } 096 097 public void activateObject(Object o) throws Exception { 098 } 099 100 public void passivateObject(Object o) throws Exception { 101 } 102 103 // Implemention methods 104 // ------------------------------------------------------------------------- 105 protected ObjectPool getSessionPool() throws AlreadyClosedException { 106 if (sessionPool == null) { 107 throw new AlreadyClosedException(); 108 } 109 return sessionPool; 110 } 111 112 protected ActiveMQConnection getConnection() throws JMSException { 113 return connectionPool.getConnection(); 114 } 115 116 protected ActiveMQSession createSession() throws JMSException { 117 return (ActiveMQSession)getConnection().createSession(key.isTransacted(), key.getAckMode()); 118 } 119 120 }