KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > dsi > DBConnectionPool


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.dsi;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.DriverManager JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.logging.*;
25 import java.util.logging.Logger JavaDoc;
26
27 import org.openharmonise.commons.pool.*;
28
29
30 /**
31  * A pool of database connections.
32  *
33  * @author Michael Bell
34  * @version $Revision: 1.2 $
35  *
36  */

37 public class DBConnectionPool extends AbstractPool {
38     
39     /**
40      * The database URI
41      */

42     private String JavaDoc dsn;
43     
44     /**
45      * The database user name
46      */

47     private String JavaDoc usr;
48     
49     /**
50      * The database user password
51      */

52     private String JavaDoc pwd;
53     
54     /**
55      * Logger for this class
56      */

57     private static final Logger JavaDoc m_logger = Logger.getLogger(DBConnectionPool.class.getName());
58
59     /**
60      * Constructs a DB connection pool which will pool connections with
61      * the given parameters.
62      *
63      * @param driver the JDBC driver class name
64      * @param dsn the database URI
65      * @param usr the database user name
66      * @param pwd the database user password
67      */

68     public DBConnectionPool(String JavaDoc driver, String JavaDoc dsn, String JavaDoc usr,
69                                 String JavaDoc pwd) {
70         try {
71             Class.forName(driver).newInstance();
72         } catch (Exception JavaDoc e) {
73             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
74         }
75
76         this.dsn = dsn;
77         this.usr = usr;
78         this.pwd = pwd;
79
80         this.setMinPoolSize(5);
81     }
82
83
84     /* (non-Javadoc)
85      * @see org.openharmonise.commons.pool.AbstractPool#create()
86      */

87     public Object JavaDoc create() throws SQLException JavaDoc {
88         return (DriverManager.getConnection(dsn, usr, pwd));
89     }
90
91     /* (non-Javadoc)
92      * @see org.openharmonise.commons.pool.AbstractPool#validate(java.lang.Object)
93      */

94     public boolean validate(Object JavaDoc o) {
95         try {
96             return (!((Connection JavaDoc) o).isClosed());
97         } catch (SQLException JavaDoc e) {
98             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
99
100             return false;
101         }
102     }
103
104     /* (non-Javadoc)
105      * @see org.openharmonise.commons.pool.AbstractPool#expire(java.lang.Object)
106      */

107     public void expire(Object JavaDoc o) {
108         try {
109             ((Connection JavaDoc) o).close();
110         } catch (SQLException JavaDoc e) {
111             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
112         }
113     }
114
115     /**
116      * Returns a connection from the pool.
117      *
118      * @return a connection from the pool
119      * @throws SQLException if an error occurs
120      */

121     public Connection JavaDoc borrowConnection() throws SQLException JavaDoc {
122         try {
123             return (Connection JavaDoc) super.checkOut();
124         } catch (Exception JavaDoc e) {
125             throw (SQLException JavaDoc) e;
126         }
127     }
128
129     /**
130      * Checks the connection back in to the pool.
131      *
132      * @param c the connection to be checked back in
133      */

134     public void returnConnection(Connection JavaDoc c) {
135         super.checkIn(c);
136     }
137 }
Popular Tags