KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > connection > ProxoolConnectionProvider


1 //$Id: ProxoolConnectionProvider.java,v 1.7 2005/04/19 15:39:06 steveebersole Exp $
2
package org.hibernate.connection;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.DriverManager JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.cfg.Environment;
11 import org.hibernate.util.PropertiesHelper;
12 import org.hibernate.util.StringHelper;
13 import org.hibernate.util.ConfigHelper;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import org.logicalcobwebs.proxool.ProxoolException;
19 import org.logicalcobwebs.proxool.ProxoolFacade;
20 import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
21 import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
22
23 /**
24  * A connection provider that uses a Proxool connection pool. Hibernate will use this by
25  * default if the <tt>hibernate.proxool.*</tt> properties are set.
26  * @see ConnectionProvider
27  */

28 public class ProxoolConnectionProvider implements ConnectionProvider {
29
30
31     private static final String JavaDoc PROXOOL_JDBC_STEM = "proxool.";
32
33     private static final Log log = LogFactory.getLog(ProxoolConnectionProvider.class);
34
35     private String JavaDoc proxoolAlias;
36
37     // TRUE if the pool is borrowed from the outside, FALSE if we used to create it
38
private boolean existingPool;
39
40     // Not null if the Isolation level has been specified in the configuration file.
41
// Otherwise, it is left to the Driver's default value.
42
private Integer JavaDoc isolation;
43     
44     private boolean autocommit;
45
46     /**
47      * Grab a connection
48      * @return a JDBC connection
49      * @throws SQLException
50      */

51     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
52         // get a connection from the pool (thru DriverManager, cfr. Proxool doc)
53
Connection JavaDoc c = DriverManager.getConnection(proxoolAlias);
54
55         // set the Transaction Isolation if defined
56
if (isolation!=null) c.setTransactionIsolation( isolation.intValue() );
57
58         // toggle autoCommit to false if set
59
if ( c.getAutoCommit()!=autocommit ) c.setAutoCommit(autocommit);
60
61         // return the connection
62
return c;
63     }
64
65     /**
66      * Dispose of a used connection.
67      * @param conn a JDBC connection
68      * @throws SQLException
69      */

70     public void closeConnection(Connection JavaDoc conn) throws SQLException JavaDoc {
71         conn.close();
72     }
73
74     /**
75      * Initialize the connection provider from given properties.
76      * @param props <tt>SessionFactory</tt> properties
77      */

78     public void configure(Properties JavaDoc props) throws HibernateException {
79
80         // Get the configurator files (if available)
81
String JavaDoc jaxpFile = props.getProperty(Environment.PROXOOL_XML);
82         String JavaDoc propFile = props.getProperty(Environment.PROXOOL_PROPERTIES);
83         String JavaDoc externalConfig = props.getProperty(Environment.PROXOOL_EXISTING_POOL);
84
85         // Default the Proxool alias setting
86
proxoolAlias = props.getProperty(Environment.PROXOOL_POOL_ALIAS);
87
88         // Configured outside of Hibernate (i.e. Servlet container, or Java Bean Container
89
// already has Proxool pools running, and this provider is to just borrow one of these
90
if ( "true".equals(externalConfig) ) {
91
92             // Validate that an alias name was provided to determine which pool to use
93
if ( !StringHelper.isNotEmpty(proxoolAlias) ) {
94                 String JavaDoc msg = "Cannot configure Proxool Provider to use an existing in memory pool without the " + Environment.PROXOOL_POOL_ALIAS + " property set.";
95                 log.fatal(msg);
96                 throw new HibernateException(msg);
97             }
98             // Append the stem to the proxool pool alias
99
proxoolAlias = PROXOOL_JDBC_STEM + proxoolAlias;
100
101             // Set the existing pool flag to true
102
existingPool = true;
103
104             log.info("Configuring Proxool Provider using existing pool in memory: " + proxoolAlias);
105
106             // Configured using the JAXP Configurator
107
}
108         else if ( StringHelper.isNotEmpty(jaxpFile) ) {
109
110             log.info("Configuring Proxool Provider using JAXPConfigurator: " + jaxpFile);
111
112             // Validate that an alias name was provided to determine which pool to use
113
if ( !StringHelper.isNotEmpty(proxoolAlias) ) {
114                 String JavaDoc msg = "Cannot configure Proxool Provider to use JAXP without the " + Environment.PROXOOL_POOL_ALIAS + " property set.";
115                 log.fatal(msg);
116                 throw new HibernateException(msg);
117             }
118
119             try {
120                 JAXPConfigurator.configure( ConfigHelper.getConfigStreamReader(jaxpFile), false );
121             }
122             catch (ProxoolException e) {
123                 String JavaDoc msg = "Proxool Provider unable to load JAXP configurator file: " + jaxpFile;
124                 log.fatal(msg, e);
125                 throw new HibernateException(msg, e);
126             }
127
128             // Append the stem to the proxool pool alias
129
proxoolAlias = PROXOOL_JDBC_STEM + proxoolAlias;
130             log.info("Configuring Proxool Provider to use pool alias: " + proxoolAlias);
131
132             // Configured using the Properties File Configurator
133
}
134         else if ( StringHelper.isNotEmpty(propFile) ) {
135
136             log.info("Configuring Proxool Provider using Properties File: " + propFile);
137
138             // Validate that an alias name was provided to determine which pool to use
139
if ( !StringHelper.isNotEmpty(proxoolAlias) ) {
140                 String JavaDoc msg = "Cannot configure Proxool Provider to use Properties File without the " + Environment.PROXOOL_POOL_ALIAS + " property set.";
141                 log.fatal(msg);
142                 throw new HibernateException(msg);
143             }
144
145             try {
146                 PropertyConfigurator.configure( ConfigHelper.getConfigProperties(propFile) );
147             }
148             catch (ProxoolException e) {
149                 String JavaDoc msg = "Proxool Provider unable to load load Property configurator file: " + propFile;
150                 log.fatal(msg, e);
151                 throw new HibernateException(msg, e);
152             }
153
154             // Append the stem to the proxool pool alias
155
proxoolAlias = PROXOOL_JDBC_STEM + proxoolAlias;
156             log.info("Configuring Proxool Provider to use pool alias: " + proxoolAlias);
157         }
158
159         // Remember Isolation level
160
isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props);
161         if (isolation!=null) {
162             log.info("JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
163         }
164         
165         autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
166         log.info("autocommit mode: " + autocommit);
167     }
168
169     /**
170      * Release all resources held by this provider. JavaDoc requires a second sentence.
171      * @throws HibernateException
172      */

173     public void close() throws HibernateException {
174
175         // If the provider was leeching off an existing pool don't close it
176
if (existingPool) {
177             return;
178         }
179
180         // We have created the pool ourselves, so shut it down
181
try {
182             ProxoolFacade.shutdown(0);
183         }
184         catch (Exception JavaDoc e) {
185             // If you're closing down the ConnectionProvider chances are an
186
// is not a real big deal, just warn
187
log.warn("Exception occured when closing the Proxool pool", e);
188             throw new HibernateException("Exception occured when closing the Proxool pool", e);
189         }
190     }
191
192     /**
193      * @see ConnectionProvider#supportsAggressiveRelease()
194      */

195     public boolean supportsAggressiveRelease() {
196         return false;
197     }
198
199 }
200
Popular Tags