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 // PoolableObjectFactory methods
074 // -------------------------------------------------------------------------
075 public Object makeObject() throws Exception {
076 return new PooledSession(createSession(), this);
077 }
078
079 public void destroyObject(Object o) throws Exception {
080 PooledSession session = (PooledSession)o;
081 session.getSession().close();
082 }
083
084 public boolean validateObject(Object o) {
085 return true;
086 }
087
088 public void activateObject(Object o) throws Exception {
089 }
090
091 public void passivateObject(Object o) throws Exception {
092 }
093
094 // Implemention methods
095 // -------------------------------------------------------------------------
096 protected ObjectPool getSessionPool() throws AlreadyClosedException {
097 if (sessionPool == null) {
098 throw new AlreadyClosedException();
099 }
100 return sessionPool;
101 }
102
103 protected ActiveMQConnection getConnection() throws JMSException {
104 return connectionPool.getConnection();
105 }
106
107 protected ActiveMQSession createSession() throws JMSException {
108 return (ActiveMQSession)getConnection().createSession(key.isTransacted(), key.getAckMode());
109 }
110
111 }