1 package org.hibernate.connection; 3 4 import java.sql.Connection ; 5 import java.sql.DriverManager ; 6 import java.sql.SQLException ; 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 import java.util.Properties ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.hibernate.HibernateException; 14 import org.hibernate.cfg.Environment; 15 import org.hibernate.util.PropertiesHelper; 16 import org.hibernate.util.ReflectHelper; 17 18 24 public class DriverManagerConnectionProvider implements ConnectionProvider { 25 26 private String url; 27 private Properties connectionProps; 28 private Integer isolation; 29 private final ArrayList pool = new ArrayList (); 30 private int poolSize; 31 private int checkedOut = 0; 32 private boolean autocommit; 33 34 private static final Log log = LogFactory.getLog(DriverManagerConnectionProvider.class); 35 36 public void configure(Properties props) throws HibernateException { 37 38 String driverClass = props.getProperty(Environment.DRIVER); 39 40 poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); log.info("Using Hibernate built-in connection pool (not for production use!)"); 42 log.info("Hibernate connection pool size: " + poolSize); 43 44 autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props); 45 log.info("autocommit mode: " + autocommit); 46 47 isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props); 48 if (isolation!=null) 49 log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) ); 50 51 if (driverClass==null) { 52 log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER); 53 } 54 else { 55 try { 56 Class.forName(driverClass); 58 } 59 catch (ClassNotFoundException cnfe) { 60 try { 61 ReflectHelper.classForName(driverClass); 62 } 63 catch (ClassNotFoundException e) { 64 String msg = "JDBC Driver class not found: " + driverClass; 65 log.fatal(msg, e); 66 throw new HibernateException(msg, e); 67 } 68 } 69 } 70 71 url = props.getProperty(Environment.URL); 72 if (url==null) { 73 String msg = "JDBC URL was not specified by property " + Environment.URL; 74 log.fatal(msg); 75 throw new HibernateException(msg); 76 } 77 78 connectionProps = ConnectionProviderFactory.getConnectionProperties(props); 79 80 log.info( "using driver: " + driverClass + " at URL: " + url ); 81 if ( log.isDebugEnabled() ) { 83 log.info( "connection properties: " + connectionProps ); 84 } 85 else if ( log.isInfoEnabled() ) { 86 log.info( "connection properties: " + PropertiesHelper.maskOut(connectionProps, "password") ); 87 } 88 89 } 90 91 public Connection getConnection() throws SQLException { 92 93 if ( log.isTraceEnabled() ) log.trace( "total checked-out connections: " + checkedOut ); 94 95 synchronized (pool) { 96 if ( !pool.isEmpty() ) { 97 int last = pool.size() - 1; 98 if ( log.isTraceEnabled() ) { 99 log.trace("using pooled JDBC connection, pool size: " + last); 100 checkedOut++; 101 } 102 Connection pooled = (Connection ) pool.remove(last); 103 if (isolation!=null) pooled.setTransactionIsolation( isolation.intValue() ); 104 if ( pooled.getAutoCommit()!=autocommit ) pooled.setAutoCommit(autocommit); 105 return pooled; 106 } 107 } 108 109 log.debug("opening new JDBC connection"); 110 Connection conn = DriverManager.getConnection(url, connectionProps); 111 if (isolation!=null) conn.setTransactionIsolation( isolation.intValue() ); 112 if ( conn.getAutoCommit() ) conn.setAutoCommit(false); 113 114 if ( log.isDebugEnabled() ) { 115 log.debug( "created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation() ); 116 } 117 if ( log.isTraceEnabled() ) checkedOut++; 118 119 return conn; 120 } 121 122 public void closeConnection(Connection conn) throws SQLException { 123 124 if ( log.isDebugEnabled() ) checkedOut--; 125 126 synchronized (pool) { 127 int currentSize = pool.size(); 128 if ( currentSize < poolSize ) { 129 if ( log.isTraceEnabled() ) log.trace("returning connection to pool, pool size: " + (currentSize + 1) ); 130 pool.add(conn); 131 return; 132 } 133 } 134 135 log.debug("closing JDBC connection"); 136 137 conn.close(); 138 139 } 140 141 protected void finalize() { 142 close(); 143 } 144 145 public void close() { 146 147 log.info("cleaning up connection pool: " + url); 148 149 Iterator iter = pool.iterator(); 150 while ( iter.hasNext() ) { 151 try { 152 ( (Connection ) iter.next() ).close(); 153 } 154 catch (SQLException sqle) { 155 log.warn("problem closing pooled connection", sqle); 156 } 157 } 158 pool.clear(); 159 160 } 161 162 165 public boolean supportsAggressiveRelease() { 166 return false; 167 } 168 169 } 170 171 172 173 174 175 176 177 | Popular Tags |