KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > database > ConnectionPool


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12
13 package org.openbravo.database;
14 import java.sql.*;
15 import javax.sql.DataSource JavaDoc;
16 import javax.naming.Context JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18 import javax.naming.NamingException JavaDoc;
19 import javax.servlet.*;
20 import org.apache.log4j.Logger;
21 import org.apache.commons.dbcp.ConnectionFactory;
22 import org.apache.commons.dbcp.PoolingDataSource;
23 import org.apache.commons.dbcp.PoolableConnectionFactory;
24 import org.apache.commons.pool.KeyedObjectPoolFactory;
25 import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory;
26 import org.apache.commons.pool.impl.GenericObjectPool;
27 import org.openbravo.database.OpenBravoDriverManagerConnectionFactory;
28
29 /**
30  * ConnectionPool
31  * Pool of database connections with internal pool of PreparedStatements.
32  * Creates and manages a pool of database connections and a pool of PreparedStatements.
33  * @version 1.0.1 26/01/2006
34  * @author Openbravo S.L.
35 */

36 public class ConnectionPool {
37   static Logger log4j = Logger.getLogger(ConnectionPool.class);
38   private DataSource JavaDoc myPool;
39   private String JavaDoc rdbms = "";
40
41   public ConnectionPool(String JavaDoc namespace) throws ServletException {
42     try {
43       Context JavaDoc initContext = new InitialContext JavaDoc();
44       Context JavaDoc envContext = (Context JavaDoc)initContext.lookup("java:/comp/env");
45       myPool = (DataSource JavaDoc)envContext.lookup(namespace);
46     } catch (NamingException JavaDoc nex) {
47       nex.printStackTrace();
48       throw new ServletException(nex.toString());
49     }
50     log4j.info("Starting ConnectionPool Version 1.0.1:");
51   }//End ConnectionPool()
52

53   public ConnectionPool(String JavaDoc _driver, String JavaDoc _server, String JavaDoc _login, String JavaDoc _password, int _minConns, int _maxConns, double _maxConnTime, String JavaDoc _dbSessionConfig, String JavaDoc _rdbms) throws ServletException {
54     rdbms = _rdbms;
55     if (log4j.isDebugEnabled()) log4j.debug("Loading underlying JDBC driver.");
56     try {
57       Class.forName(_driver);
58     } catch (ClassNotFoundException JavaDoc e) {
59       throw new ServletException(e.toString());
60     }
61     if (log4j.isDebugEnabled()) log4j.debug("Done.");
62
63
64     GenericObjectPool connectionPool = new GenericObjectPool(null);
65     connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
66     connectionPool.setMaxActive(_maxConns);
67     connectionPool.setTestOnBorrow(false);
68     connectionPool.setTestOnReturn(false);
69     connectionPool.setTestWhileIdle(false);
70
71     KeyedObjectPoolFactory keyedObject = new StackKeyedObjectPoolFactory();
72     ConnectionFactory connectionFactory = new OpenBravoDriverManagerConnectionFactory(_server, _login, _password, _dbSessionConfig);
73     PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,keyedObject,
74         null,false,true);
75
76     PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
77
78     myPool = dataSource;
79   }
80
81
82   /**
83    * getConnection
84    * Return the first free connection of the pool.
85    * Loop over the pool searching for the first free connection.
86    */

87   public Connection getConnection() {
88     Connection conn=null;
89     try {
90       conn = myPool.getConnection();
91     } catch (SQLException ex) {
92       ex.printStackTrace();
93     }
94     return conn;
95   }
96
97   public String JavaDoc getRDBMS() {
98     return rdbms;
99   }
100
101   /**
102    * releaseConnection
103    * Free a specific connection but get them alive (unclosed).
104    */

105   public boolean releaseConnection(Connection conn) {
106     try {
107       conn.setAutoCommit(true);
108       conn.close();
109     } catch (SQLException e) {
110       e.printStackTrace();
111     }
112     return true;
113   }
114
115   public PreparedStatement getPreparedStatement(String JavaDoc SQLPreparedStatement) {
116     if (log4j.isDebugEnabled()) log4j.debug("connection requested");
117     Connection conn = getConnection();
118     if (log4j.isDebugEnabled()) log4j.debug("connection established");
119     return getPreparedStatement(conn, SQLPreparedStatement);
120   }
121
122   public PreparedStatement getPreparedStatement(Connection conn, String JavaDoc SQLPreparedStatement) {
123     try {
124       PreparedStatement ps;
125       if (log4j.isDebugEnabled()) log4j.debug("preparedStatement requested");
126       ps = conn.prepareStatement(SQLPreparedStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
127       if (log4j.isDebugEnabled()) log4j.debug("preparedStatement received");
128       return(ps);
129     } catch(SQLException e) {
130       log4j.error("getPreparedStatement: " + SQLPreparedStatement);
131       releaseConnection(conn);
132       e.printStackTrace();
133       return null;
134     }
135   }
136
137   public CallableStatement getCallableStatement(String JavaDoc SQLCallableStatement) {
138     Connection conn = getConnection();
139     return getCallableStatement(conn, SQLCallableStatement);
140   }
141
142   public CallableStatement getCallableStatement(Connection conn, String JavaDoc SQLCallableStatement) {
143     try {
144       CallableStatement cs;
145       cs = conn.prepareCall(SQLCallableStatement);
146       return(cs);
147     } catch(SQLException e) {
148       log4j.error("getCallableStatement: " + SQLCallableStatement);
149       releaseConnection(conn);
150       e.printStackTrace();
151       return null;
152     }
153   }
154
155   public Statement getStatement() {
156     Connection conn = getConnection();
157     return getStatement(conn);
158   }
159
160   public Statement getStatement(Connection conn) {
161     try {
162       return(conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
163     } catch(SQLException e) {
164       releaseConnection(conn);
165       e.printStackTrace();
166       return null;
167     }
168   }
169
170   public void releasePreparedStatement(PreparedStatement preparedStatement) {
171     try {
172       Connection conn = preparedStatement.getConnection();
173       releaseConnection(conn);
174     } catch (SQLException e) {
175       e.printStackTrace();
176     }
177   }
178
179   public void releaseCallableStatement(CallableStatement callableStatement) {
180     try {
181       Connection conn = callableStatement.getConnection();
182       releaseConnection(conn);
183     } catch (SQLException e) {
184       e.printStackTrace();
185     }
186   }
187
188   public void releaseStatement(Statement statement) {
189     try {
190       Connection conn = statement.getConnection();
191       statement.close();
192       releaseConnection(conn);
193     } catch (SQLException e) {
194       e.printStackTrace();
195     }
196   }
197
198
199   /**
200    * destroy
201    * Destroy de thread releasing every connections to the database
202    */

203   public void destroy() {
204     myPool = null;
205   }
206
207   /**
208    * Returns the actual status of the dynamic pool.
209    */

210   public String JavaDoc getStatus() {
211     StringBuffer JavaDoc strResultado = new StringBuffer JavaDoc();
212     strResultado.append("Not implemented yet");
213     return strResultado.toString();
214   }//End getStatus()
215
}
216
Popular Tags