1 package org.hibernate.connection; 3 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 import java.util.Properties ; 7 8 import javax.sql.DataSource ; 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 26 public class C3P0ConnectionProvider implements ConnectionProvider { 27 28 private DataSource ds; 29 private Integer isolation; 30 private boolean autocommit; 31 32 private static final Log log = LogFactory.getLog(C3P0ConnectionProvider.class); 33 34 public Connection getConnection() throws SQLException { 35 final Connection 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 conn) throws SQLException { 42 conn.close(); 43 } 44 45 public void configure(Properties props) throws HibernateException { 46 String jdbcDriverClass = props.getProperty(Environment.DRIVER); 47 String jdbcUrl = props.getProperty(Environment.URL); 48 Properties 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 cnfe) { 64 try { 65 ReflectHelper.classForName(jdbcDriverClass); 66 } 67 catch (ClassNotFoundException e) { 68 String 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 96 DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl, connectionProps); 97 ds = DataSources.pooledDataSource(unpooled, pcfg); 98 99 } 100 catch (Exception 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 i = props.getProperty(Environment.ISOLATION); 106 if (i==null) { 107 isolation=null; 108 } 109 else { 110 isolation = new Integer (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 sqle) { 121 log.warn("could not destroy C3P0 connection pool", sqle); 122 } 123 } 124 125 128 public boolean supportsAggressiveRelease() { 129 return false; 130 } 131 132 } 133 134 135 136 137 138 139 140 | Popular Tags |