KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > auth > sql > ConnectionPool


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.auth.sqlConnectionPool;
19
20 import java.sql.*;
21 import freecs.Server;
22 import freecs.auth.SQLAuthenticator;
23 import freecs.core.CanceledRequestException;
24 import freecs.core.ConnectionBuffer;
25
26 public class ConnectionPool {
27
28     final SQLAuthenticator authenticator;
29     final DbProperties dbProps;
30     final PoolElement pool[];
31     volatile int p = 0, idCnt=0;
32     
33     /**
34      * Construct a new ConnectionPool for the given authenticator
35      * and the given dbProperties
36      * @param authenticator the authenticator this ConnectionPool will used by.
37      * @param dbProps the dbProperties describing the datastructure
38      */

39     public ConnectionPool (SQLAuthenticator authenticator, DbProperties dbProps) {
40         this.authenticator = authenticator;
41         this.dbProps = dbProps;
42         pool = new PoolElement[dbProps.poolsize];
43         if (Server.TRACE_CREATE_AND_FINALIZE) {
44             Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
45         }
46     }
47
48     /**
49      * close every connection to the jdbc-source and remove the PooleElements
50      */

51     public void shutdown () {
52         synchronized (this.pool) {
53             for (int i = 0; i < pool.length; i++) {
54                 if (pool[i] == null) {
55                     continue;
56                 }
57                 pool[i].cleanup ();
58                 pool[i]=null;
59             }
60         }
61     }
62
63     
64     /**
65      * creates an connectionpool-element
66      * @return the PoolElement connected to the jdbc-datasource
67      * @throws Exception
68      */

69     private PoolElement createPoolElement () throws Exception JavaDoc {
70         idCnt++;
71         if (idCnt == Integer.MAX_VALUE) {
72             idCnt = 0;
73         }
74         Connection con = DriverManager.getConnection (dbProps.url, dbProps.conProps);
75         return new PoolElement (this, con, dbProps, idCnt);
76     }
77
78     public PoolElement getPoolElement (int retrys, ConnectionBuffer cb) throws Exception JavaDoc {
79         // try [retrys] times to retrieve PoolElement
80
for (int i = 0; i<retrys; i++) {
81             if ((cb != null
82                         && !cb.isValid())
83                     || Thread.currentThread().isInterrupted())
84                 throw new CanceledRequestException ("ConnectionBuffer has been invalidated");
85
86             PoolElement el=null;
87             try {
88                 el = this.getPoolElement(cb);
89                 if (el!=null) // if el isn't null, return it (it was checked for validity)
90
return el;
91             } catch (CanceledRequestException cre) {
92                 throw cre;
93             } catch (Exception JavaDoc e) {
94                 Server.debug(Thread.currentThread() , this.toString() + "getPoolElement: ", e, Server.MSG_AUTH, Server.LVL_MAJOR);
95                 el = null;
96             }
97         }
98         throw new Exception JavaDoc ("Unable to get available PoolElement");
99     }
100     
101     /**
102      * Gets the next available and valid poolelement or tryes to create a new one
103      * @return the valid and available PoolElement
104      * @throws Exception If creation of PoolElement failes
105      */

106     private PoolElement getPoolElement (ConnectionBuffer cb) throws Exception JavaDoc {
107         for (int i = 0; i < dbProps.poolsize; i++) {
108             if ((cb != null
109                         && !cb.isValid())
110                     || Thread.currentThread().isInterrupted())
111                 throw new CanceledRequestException ("ConnectionBuffer has been invalidated");
112             p++;
113             if (p > dbProps.poolsize - 1)
114                 p=0;
115             synchronized (this.pool) {
116                 if (pool[p]==null) {
117                     pool[p] = createPoolElement();
118                 }
119                 switch (pool[p].grab()) {
120                     case PoolElement.INVALID:
121                         pool[p].cleanup();
122                         pool[p]=createPoolElement();
123                     case PoolElement.IDLE:
124                         return pool[p];
125                     case PoolElement.ACTIVE:
126                         continue;
127                 }
128             }
129         }
130         return null;
131     }
132     
133     public int size() {
134         return pool.length;
135     }
136
137     public void finalize() {
138         if (Server.TRACE_CREATE_AND_FINALIZE)
139             Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
140     }
141
142     private ConnectionPool() throws Exception JavaDoc { throw new Exception JavaDoc ("not instatiable without arguments"); }
143 }
Popular Tags