KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: DriverManagerConnectionProvider.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.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Properties JavaDoc;
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 /**
19  * A connection provider that uses <tt>java.sql.DriverManager</tt>. This provider
20  * also implements a very rudimentary connection pool.
21  * @see ConnectionProvider
22  * @author Gavin King
23  */

24 public class DriverManagerConnectionProvider implements ConnectionProvider {
25
26     private String JavaDoc url;
27     private Properties JavaDoc connectionProps;
28     private Integer JavaDoc isolation;
29     private final ArrayList JavaDoc pool = new ArrayList JavaDoc();
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 JavaDoc props) throws HibernateException {
37
38         String JavaDoc driverClass = props.getProperty(Environment.DRIVER);
39
40         poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20
41
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                 // trying via forName() first to be as close to DriverManager's semantics
57
Class.forName(driverClass);
58             }
59             catch (ClassNotFoundException JavaDoc cnfe) {
60                 try {
61                     ReflectHelper.classForName(driverClass);
62                 }
63                 catch (ClassNotFoundException JavaDoc e) {
64                     String JavaDoc 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 JavaDoc 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 debug level is enabled, then log the password, otherwise mask it
82
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 JavaDoc getConnection() throws SQLException JavaDoc {
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 JavaDoc pooled = (Connection JavaDoc) 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 JavaDoc 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 JavaDoc conn) throws SQLException JavaDoc {
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 JavaDoc iter = pool.iterator();
150         while ( iter.hasNext() ) {
151             try {
152                 ( (Connection JavaDoc) iter.next() ).close();
153             }
154             catch (SQLException JavaDoc sqle) {
155                 log.warn("problem closing pooled connection", sqle);
156             }
157         }
158         pool.clear();
159
160     }
161
162     /**
163      * @see ConnectionProvider#supportsAggressiveRelease()
164      */

165     public boolean supportsAggressiveRelease() {
166         return false;
167     }
168
169 }
170
171
172
173
174
175
176
177
Popular Tags