KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.jfox.ioc.common.AbstractComponent;
16 import org.jfox.pool.AbstractObjectPool;
17 import org.jfox.pool.ObjectFactory;
18 import org.jfox.pool.ObjectPool;
19 import org.jfox.pool.PoolableObject;
20 import org.jfox.pool.SimpleObjectPool;
21
22 /**
23  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
24  */

25
26 public class ConnectionPool extends AbstractComponent implements ObjectPool {
27
28     private AbstractObjectPool pool = null;
29     private ConnectionFactory factory = null;
30
31     private int initNum = 2;
32     private int maxRest = 10;
33
34     // Connection <=> starting use time
35
private Map JavaDoc workings = new HashMap JavaDoc();
36
37     private long timeout = 60 * 60 * 1000L; // one hour
38

39     public ConnectionPool(String JavaDoc dbDriver, String JavaDoc dbURL, String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
40         factory = new ConnectionFactory(PoolableConnection.class, dbDriver, dbURL, user, password);
41     }
42
43     public Connection JavaDoc getConnection() throws Exception JavaDoc {
44         PoolableConnection pconn = (PoolableConnection) retrieveObject();
45         return pconn;
46     }
47
48     public int getInitNum() {
49         return initNum;
50     }
51
52     public void setInitNum(int initNum) {
53         this.initNum = initNum;
54     }
55
56     public void clear() {
57         try {
58             for(Iterator JavaDoc it = workings.keySet().iterator(); it.hasNext();) {
59                 ((PoolableConnection) it.next()).getConnection().close();
60             }
61         }
62         catch(SQLException JavaDoc e) {
63             e.printStackTrace();
64         }
65     }
66
67     public int getMaxRest() {
68         return maxRest;
69     }
70
71     public void setMaxRest(int maxRest) {
72         this.maxRest = maxRest;
73     }
74
75     public ObjectFactory getObjectFactory() {
76         return factory;
77     }
78
79     public boolean removeObject(PoolableObject obj) {
80         workings.remove(obj);
81         try {
82             factory.destroyObject(obj);
83             return true;
84         }
85         catch(Exception JavaDoc e) {
86             e.printStackTrace();
87             return false;
88         }
89     }
90
91     public int getWorking() {
92         return workings.size();
93     }
94
95     public String JavaDoc getObjectClass() {
96         return factory.getObjectClass().getName();
97     }
98
99     public int getRest() {
100         return pool.getRest();
101     }
102
103     public PoolableObject retrieveObject() throws Exception JavaDoc {
104         PoolableConnection pobj = (PoolableConnection) pool.retrieveObject();
105         pobj.setPool(this);
106         workings.put(pobj, new Long JavaDoc(System.currentTimeMillis()));
107         return pobj;
108     }
109
110     public boolean restoreObject(PoolableObject obj) {
111         workings.remove(obj);
112         return pool.restoreObject(obj);
113     }
114
115     protected void doInit() throws Exception JavaDoc {
116         pool = new SimpleObjectPool(factory, initNum, maxRest);
117         pool.init();
118         new LifecyleThread().start();
119     }
120
121     protected void doDestroy() throws Exception JavaDoc {
122         clear();
123         pool.destroy();
124     }
125
126     /**
127      * 检查连接的超时
128      */

129     private class LifecyleThread extends Thread JavaDoc {
130         public LifecyleThread() {
131             this.setPriority(Thread.MIN_PRIORITY);
132             this.setDaemon(true);
133         }
134
135         public void run() {
136             while(true) {
137                 try {
138                     Thread.sleep(timeout / 10);
139                 }
140                 catch(Exception JavaDoc e) {
141                 }
142                 for(Iterator JavaDoc it = workings.entrySet().iterator(); it.hasNext();) {
143                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
144                     long time = ((Long JavaDoc) entry.getValue()).longValue();
145                     if(System.currentTimeMillis() - time > timeout) {
146                         try {
147                             factory.destroyObject((PoolableObject) entry.getKey());
148                         }
149                         catch(Exception JavaDoc e) {
150                             e.printStackTrace();
151                         }
152                     }
153                 }
154             }
155         }
156     }
157
158     public static void main(String JavaDoc[] args) {
159
160     }
161 }
Popular Tags