KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jdbc > datasource > PoolDataSource


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jdbc.datasource;
8
9 import java.io.PrintWriter JavaDoc;
10 import java.sql.Connection JavaDoc;
11 import java.sql.DriverManager JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import javax.naming.Reference JavaDoc;
15 import javax.naming.Referenceable JavaDoc;
16 import javax.naming.StringRefAddr JavaDoc;
17 import javax.sql.DataSource JavaDoc;
18
19 import org.jfox.ioc.common.AbstractService;
20 import org.jfox.ioc.ext.ActiveComponent;
21 import org.jfox.jndi.InitialContextHelper;
22
23 /**
24  * @author <a HREF="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
25  */

26 public class PoolDataSource extends AbstractService implements DataSource JavaDoc, Referenceable JavaDoc, ActiveComponent {
27     protected ConnectionPool pool = null; // The connection pool used the get connections.
28
private String JavaDoc dbDriver; // database driver class name.
29
private String JavaDoc dbURL; // database url
30
private String JavaDoc user; // database user
31
private String JavaDoc password; //database password
32
private int initNum = 2;
33     private int maxRest = 10;
34
35     private String JavaDoc dsName;
36
37
38     /**
39      * Constructor specify database connection parameters to generate a valid and usable datasource.
40      *
41      * @param dbDriver database class name according your dabase
42      * @param dbURL database url used to access database
43      * @param user valid database user
44      * @param password // corresponding database password
45      * @throws Exception any exception who cares?
46      */

47     public PoolDataSource(String JavaDoc dsName, String JavaDoc dbDriver, String JavaDoc dbURL, String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
48         this.dsName = dsName;
49         this.dbDriver = dbDriver;
50         this.dbURL = dbURL;
51         this.user = user;
52         this.password = password;
53     }
54
55     /**
56      * Get usable connection from datasource
57      *
58      * @return Connection the valid database connection can used by user
59      * @throws SQLException
60      */

61     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
62         // since the pool.getConnection() throws out exception, so I have to catch it.
63
// Please Young consider about the excpetion handler meshiasm.
64
try {
65             return pool.getConnection();
66         }
67         catch(SQLException JavaDoc sqle) {
68             throw sqle;
69         }
70         catch(Exception JavaDoc e) {
71             e.printStackTrace();
72             throw new SQLException JavaDoc(e.getMessage());
73         }
74     }
75
76     /**
77      * Get connection by specific user name and password, the connection is not pooled by
78      * Connection Pool
79      *
80      * @param username
81      * @param password
82      * @return
83      * @throws SQLException
84      */

85     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password)
86             throws SQLException JavaDoc {
87         return DriverManager.getConnection(dbURL, username, password);
88
89     }
90
91     /**
92      * Get logger writer
93      *
94      * @return
95      * @throws SQLException
96      */

97     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
98         return DriverManager.getLogWriter();
99     }
100
101     /**
102      * @param out
103      * @throws SQLException
104      */

105     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
106         DriverManager.setLogWriter(out);
107     }
108
109     /**
110      * @param seconds
111      * @throws SQLException
112      */

113     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
114         DriverManager.setLoginTimeout(seconds);
115     }
116
117     /**
118      * @return
119      * @throws SQLException
120      */

121     public int getLoginTimeout() throws SQLException JavaDoc {
122         return DriverManager.getLoginTimeout();
123     }
124
125     /**
126      * Method that inherited from Referenceable.
127      *
128      * @return
129      * @throws javax.naming.NamingException
130      */

131     public Reference JavaDoc getReference() throws NamingException JavaDoc {
132         Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), DataSourceObjectFactory.class.getName(), null);
133         ref.add(new StringRefAddr JavaDoc("dsName", dsName));
134         ref.add(new StringRefAddr JavaDoc("dbDriver", dbDriver));
135         ref.add(new StringRefAddr JavaDoc("dbURL", dbURL));
136         ref.add(new StringRefAddr JavaDoc("user", user));
137         ref.add(new StringRefAddr JavaDoc("password", password));
138         ref.add(new StringRefAddr JavaDoc("initNum", "" + initNum));
139         ref.add(new StringRefAddr JavaDoc("maxRest", "" + maxRest));
140         return ref;
141     }
142
143     public String JavaDoc getDbDriver() {
144         return dbDriver;
145     }
146
147     public String JavaDoc getDbURL() {
148         return dbURL;
149     }
150
151     public String JavaDoc getUser() {
152         return user;
153     }
154
155     public String JavaDoc getPassword() {
156         return password;
157     }
158
159     public int getMaxRest() {
160         return maxRest;
161     }
162
163     public int getInitNum() {
164         return initNum;
165     }
166
167     public void setInitNum(int initNum) {
168         this.initNum = initNum;
169     }
170
171     public void setMaxRest(int maxRest) {
172         this.maxRest = maxRest;
173     }
174
175     protected void doInit() throws Exception JavaDoc {
176         pool = new ConnectionPool(dbDriver, dbURL, user, password);
177         pool.setInitNum(initNum);
178         pool.setMaxRest(maxRest);
179         pool.init();
180     }
181
182     public String JavaDoc getDsName() {
183         return dsName;
184     }
185
186     protected void doDestroy() throws Exception JavaDoc {
187         pool.destroy();
188         DataSourceObjectFactory.removeDataSource(dsName);
189     }
190
191     protected void doStart() throws Exception JavaDoc {
192         InitialContextHelper.getInitialContext().bind(getDsName(), this);
193     }
194
195     protected void doStop() throws Exception JavaDoc {
196         InitialContextHelper.getInitialContext().unbind(getDsName());
197     }
198
199     public void run() {
200     }
201
202 }
203
Popular Tags