KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: C3P0ConnectionProvider.java,v 1.6 2005/04/19 15:39:05 steveebersole Exp $
2
package org.hibernate.connection;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import javax.sql.DataSource JavaDoc;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import com.mchange.v2.c3p0.PoolConfig;
13 import com.mchange.v2.c3p0.DataSources;
14
15 import org.hibernate.HibernateException;
16 import org.hibernate.cfg.Environment;
17 import org.hibernate.util.PropertiesHelper;
18 import org.hibernate.util.ReflectHelper;
19
20 /**
21  * A connection provider that uses a C3P0 connection pool. Hibernate will use this by
22  * default if the <tt>hibernate.c3p0.*</tt> properties are set.
23  * @see ConnectionProvider
24  * @author various people
25  */

26 public class C3P0ConnectionProvider implements ConnectionProvider {
27
28     private DataSource JavaDoc ds;
29     private Integer JavaDoc isolation;
30     private boolean autocommit;
31
32     private static final Log log = LogFactory.getLog(C3P0ConnectionProvider.class);
33
34     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
35         final Connection JavaDoc c = ds.getConnection();
36         if (isolation!=null) c.setTransactionIsolation( isolation.intValue() );
37         if ( c.getAutoCommit()!=autocommit ) c.setAutoCommit(autocommit);
38         return c;
39     }
40
41     public void closeConnection(Connection JavaDoc conn) throws SQLException JavaDoc {
42         conn.close();
43     }
44
45     public void configure(Properties JavaDoc props) throws HibernateException {
46         String JavaDoc jdbcDriverClass = props.getProperty(Environment.DRIVER);
47         String JavaDoc jdbcUrl = props.getProperty(Environment.URL);
48         Properties JavaDoc connectionProps = ConnectionProviderFactory.getConnectionProperties(props);
49
50         log.info( "C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl );
51         log.info( "Connection properties: " + PropertiesHelper.maskOut(connectionProps, "password") );
52         
53         autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props);
54         log.info("autocommit mode: " + autocommit);
55
56         if (jdbcDriverClass==null) {
57             log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
58         }
59         else {
60             try {
61                 Class.forName(jdbcDriverClass);
62             }
63             catch (ClassNotFoundException JavaDoc cnfe) {
64                 try {
65                     ReflectHelper.classForName(jdbcDriverClass);
66                 }
67                 catch (ClassNotFoundException JavaDoc e) {
68                     String JavaDoc msg = "JDBC Driver class not found: " + jdbcDriverClass;
69                     log.fatal(msg, e);
70                     throw new HibernateException(msg, e);
71                 }
72             }
73         }
74
75         try {
76
77             int minPoolSize = PropertiesHelper.getInt(Environment.C3P0_MIN_SIZE, props, 1);
78             int maxPoolSize = PropertiesHelper.getInt(Environment.C3P0_MAX_SIZE, props, 100);
79             int maxIdleTime = PropertiesHelper.getInt(Environment.C3P0_TIMEOUT, props, 0);
80             int maxStatements = PropertiesHelper.getInt(Environment.C3P0_MAX_STATEMENTS, props, 0);
81             int acquireIncrement = PropertiesHelper.getInt(Environment.C3P0_ACQUIRE_INCREMENT, props, 1);
82             int idleTestPeriod = PropertiesHelper.getInt(Environment.C3P0_IDLE_TEST_PERIOD, props, 0);
83
84             PoolConfig pcfg = new PoolConfig();
85             pcfg.setInitialPoolSize(minPoolSize);
86             pcfg.setMinPoolSize(minPoolSize);
87             pcfg.setMaxPoolSize(maxPoolSize);
88             pcfg.setAcquireIncrement(acquireIncrement);
89             pcfg.setMaxIdleTime(maxIdleTime);
90             pcfg.setMaxStatements(maxStatements);
91             pcfg.setIdleConnectionTestPeriod(idleTestPeriod);
92
93             /*DataSource unpooled = DataSources.unpooledDataSource(
94                 jdbcUrl, props.getProperty(Environment.USER), props.getProperty(Environment.PASS)
95             );*/

96             DataSource JavaDoc unpooled = DataSources.unpooledDataSource(jdbcUrl, connectionProps);
97             ds = DataSources.pooledDataSource(unpooled, pcfg);
98
99         }
100         catch (Exception JavaDoc e) {
101             log.fatal("could not instantiate C3P0 connection pool", e);
102             throw new HibernateException( "Could not instantiate C3P0 connection pool", e );
103         }
104
105         String JavaDoc i = props.getProperty(Environment.ISOLATION);
106         if (i==null) {
107             isolation=null;
108         }
109         else {
110             isolation = new Integer JavaDoc(i);
111             log.info("JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
112         }
113
114     }
115
116     public void close() {
117         try {
118             DataSources.destroy(ds);
119         }
120         catch (SQLException JavaDoc sqle) {
121             log.warn("could not destroy C3P0 connection pool", sqle);
122         }
123     }
124
125     /**
126      * @see ConnectionProvider#supportsAggressiveRelease()
127      */

128     public boolean supportsAggressiveRelease() {
129         return false;
130     }
131
132 }
133
134
135
136
137
138
139
140
Popular Tags