KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > JDBCConnectionFactory


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool.jdbc;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.DriverManager JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.apache.log4j.Logger;
12 import org.ofbiz.minerva.pool.ObjectPool;
13 import org.ofbiz.minerva.pool.PoolObjectFactory;
14 import org.ofbiz.minerva.pool.cache.ObjectCache;
15
16 /**
17  * Object factory that creates java.sql.Connections. This is meant for use
18  * outside a J2EE/JTA environment - servlets alone, client/server, etc. If
19  * you're interested in creating transactional-aware connections, see
20  * XAConnectionFactory, which complies with the JDBC 2.0 standard extension.
21  * @see org.ofbiz.minerva.pool.jdbc.xa.XAConnectionFactory
22  *
23  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
24  */

25 public class JDBCConnectionFactory extends PoolObjectFactory {
26
27     private String JavaDoc url;
28     private Properties JavaDoc props;
29     private String JavaDoc userName;
30     private String JavaDoc password;
31     private int psCacheSize = 10;
32     private ObjectPool pool;
33
34     private static Logger log = Logger.getLogger(JDBCConnectionFactory.class);
35
36     /**
37      * Creates a new factory. You must configure it with JDBC properties
38      * before you can use it.
39      */

40     public JDBCConnectionFactory() {
41     }
42
43     /**
44      * Sets the JDBC URL used to create new connections.
45      */

46     public void setConnectURL(String JavaDoc url) {
47         this.url = url;
48     }
49
50     /**
51      * Gets the JDBC URL used to create new connections.
52      */

53     public String JavaDoc getConnectURL() {
54         return url;
55     }
56
57     /**
58      * Sets the JDBC Propeties used to create new connections.
59      * This is optional, and will only be used if present.
60      */

61     public void setConnectProperties(Properties JavaDoc props) {
62         this.props = props;
63     }
64
65     /**
66      * Gets the JDBC Properties used to create new connections.
67      */

68     public Properties JavaDoc getConnectProperties() {
69         return props;
70     }
71
72     /**
73      * Sets the JDBC user name used to create new connections.
74      * This is optional, and will only be used if present.
75      */

76     public void setUser(String JavaDoc userName) {
77         this.userName = userName;
78     }
79
80     /**
81      * Gets the JDBC user name used to create new connections.
82      */

83     public String JavaDoc getUser() {
84         return userName;
85     }
86
87     /**
88      * Sets the JDBC password used to create new connections.
89      * This is optional, and will only be used if present.
90      */

91     public void setPassword(String JavaDoc password) {
92         this.password = password;
93     }
94
95     /**
96      * Gets the JDBC password used to create new connections.
97      */

98     public String JavaDoc getPassword() {
99         return password;
100     }
101
102     /**
103      * Sets the number of PreparedStatements to be cached for each
104      * Connection. Your DB product may impose a limit on the number
105      * of open PreparedStatements. The default value is 10.
106      */

107     public void setPSCacheSize(int size) {
108         psCacheSize = size;
109     }
110
111     /**
112      * Gets the number of PreparedStatements to be cached for each
113      * Connection. The default value is 10.
114      */

115     public int getPSCacheSize() {
116         return psCacheSize;
117     }
118
119     /**
120      * Validates that connection properties were set (at least a URL).
121      */

122     public void poolStarted(ObjectPool pool) {
123         if (log.isDebugEnabled())
124             log.debug("Starting");
125
126         super.poolStarted(pool);
127         if (url == null) {
128             log.error("Must specify JDBC connection URL");
129             throw new IllegalStateException JavaDoc("Must specify JDBC connection URL to " + getClass().getName());
130         }
131         this.pool = pool;
132     }
133
134     /**
135      * Cleans up.
136      */

137     public void poolClosing(ObjectPool pool) {
138         if (log.isDebugEnabled())
139             log.debug("Stopping");
140
141         super.poolClosing(pool);
142         this.pool = null;
143     }
144
145     /**
146      * Creates a new JDBC Connection.
147      */

148     public Object JavaDoc createObject(Object JavaDoc parameters) throws Exception JavaDoc {
149
150         log.debug("Opening new connection");
151
152         try {
153             if (userName != null && userName.length() > 0)
154                 return DriverManager.getConnection(url, userName, password);
155             else if (props != null)
156                 return DriverManager.getConnection(url, props);
157             else
158                 return DriverManager.getConnection(url);
159         } catch (SQLException JavaDoc e) {
160             log.error("SQL Error", e);
161             throw e;
162         }
163     }
164
165     /**
166      * Wraps the connection with a ConnectionInPool.
167      * @see org.ofbiz.minerva.pool.jdbc.ConnectionInPool
168      */

169     public Object JavaDoc prepareObject(Object JavaDoc pooledObject) {
170         Connection JavaDoc con = (Connection JavaDoc) pooledObject;
171         ConnectionInPool wrapper = new ConnectionInPool(con);
172         wrapper.setPSCacheSize(psCacheSize);
173         return wrapper;
174     }
175
176     /**
177      * Returns the original connection from a ConnectionInPool.
178      * @see org.ofbiz.minerva.pool.jdbc.ConnectionInPool
179      */

180     public Object JavaDoc translateObject(Object JavaDoc clientObject) {
181         return ((ConnectionInPool) clientObject).getUnderlyingConnection();
182     }
183
184     /**
185      * Closes all outstanding work for the connection, rolls it back, and
186      * returns the underlying connection to the pool.
187      */

188     public Object JavaDoc returnObject(Object JavaDoc clientObject) {
189         ConnectionInPool wrapper = (ConnectionInPool) clientObject;
190         Connection JavaDoc con = wrapper.getUnderlyingConnection();
191         try {
192             wrapper.reset();
193         } catch (SQLException JavaDoc e) {
194             pool.markObjectAsInvalid(clientObject);
195         }
196         return con;
197     }
198
199     /**
200      * Closes a connection.
201      */

202     public void deleteObject(Object JavaDoc pooledObject) {
203         Connection JavaDoc con = (Connection JavaDoc) pooledObject;
204         try {
205             con.rollback();
206         } catch (SQLException JavaDoc ignored) {
207         }
208
209         // Removed all the cached PreparedStatements for this Connection
210
ObjectCache cache = (ObjectCache) ConnectionInPool.psCaches.remove(con);
211         if (cache != null)
212             cache.close();
213
214         try {
215             con.close();
216         } catch (SQLException JavaDoc ignored) {
217         }
218     }
219 }
220
221 /*
222 vim:tabstop=3:et:shiftwidth=3
223 */

224
Popular Tags