01: //** Copyright Statement ***************************************************
02: //The Salmon Open Framework for Internet Applications (SOFIA)
03: //Copyright (C) 1999 - 2004, Salmon LLC
04: //
05: //This program is free software; you can redistribute it and/or
06: //modify it under the terms of the GNU General Public License version 2
07: //as published by the Free Software Foundation;
08: //
09: //This program is distributed in the hope that it will be useful,
10: //but WITHOUT ANY WARRANTY; without even the implied warranty of
11: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: //GNU General Public License for more details.
13: //
14: //You should have received a copy of the GNU General Public License
15: //along with this program; if not, write to the Free Software
16: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: //
18: //For more information please visit http://www.salmonllc.com
19: //** End Copyright Statement ***************************************************
20: package com.salmonllc.hibernate;
21:
22: import java.sql.Connection;
23: import java.sql.SQLException;
24: import java.util.Enumeration;
25: import java.util.Hashtable;
26: import java.util.Properties;
27:
28: import com.salmonllc.sql.DBConnection;
29: import com.salmonllc.util.ApplicationContext;
30:
31: import net.sf.hibernate.HibernateException;
32:
33: /**
34: * A connection provider that allows Hibernate to use SOFIA's connection pool
35: *
36: * To use this provider add this line to the hibernate configuration xml
37: *
38: * <property name="connection.provider_class">com.salmonllc.hibernate.ConnectionProvider</property>
39: */
40: public class ConnectionProvider implements
41: net.sf.hibernate.connection.ConnectionProvider {
42:
43: Hashtable _openConnections = new Hashtable();
44: String _appName, _profile;
45:
46: /**
47: *
48: */
49: public ConnectionProvider() {
50: super ();
51: }
52:
53: /* (non-Javadoc)
54: * @see net.sf.hibernate.connection.ConnectionProvider#configure(java.util.Properties)
55: */
56: public void configure(Properties arg0) throws HibernateException {
57: _appName = arg0.getProperty("sofia.app.name");
58: _profile = arg0.getProperty("sofia.dbprofile");
59: }
60:
61: /* (non-Javadoc)
62: * @see net.sf.hibernate.connection.ConnectionProvider#getConnection()
63: */
64: public Connection getConnection() throws SQLException {
65: DBConnection conn = null;
66: if (_appName != null)
67: conn = DBConnection.getConnection(_appName, _profile);
68: else {
69: ApplicationContext cont = ApplicationContext.getContext();
70: conn = DBConnection
71: .getConnection(cont.getAppID(), _profile);
72: }
73: _openConnections.put(conn.getJDBCConnection(), conn);
74: return conn.getJDBCConnection();
75: }
76:
77: /* (non-Javadoc)
78: * @see net.sf.hibernate.connection.ConnectionProvider#closeConnection(java.sql.Connection)
79: */
80: public void closeConnection(Connection arg0) throws SQLException {
81: DBConnection conn = (DBConnection) _openConnections.get(arg0);
82: if (conn != null) {
83: conn.freeConnection();
84: _openConnections.remove(arg0);
85: }
86: }
87:
88: /* (non-Javadoc)
89: * @see net.sf.hibernate.connection.ConnectionProvider#close()
90: */
91: public void close() throws HibernateException {
92: Enumeration e = _openConnections.elements();
93: while (e.hasMoreElements()) {
94: DBConnection conn = (DBConnection) e.nextElement();
95: conn.freeConnection();
96: }
97: }
98:
99: }
|