API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.db. JOTDBPool View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

/*
------------------------------------
JavaOnTracks          Thibaut Colar
tcolar-jot AT colar DOT net
Artistic Licence 2.0
http://www.javaontracks.net
------------------------------------
 */
package net.jot.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;

import net.jot.logger.JOTLogger;

/**
 * This is an implementation of a database connection pooling system
 * It manages connections, adds some when needed, and periodically
 * check there status and renew them is they appear corrupt/hung.
 * It also release connections if they aren't used for a while.
 *
 *
 *@author     tcolar
 *@created    August 19, 2003
 */
public class JOTDBPool extends Thread
{
    volatile boolean shutdown = false;
    int statusGap = 15 * 60 * 1000;
    //15 mn

    boolean[] status;
    long[] lastAccess;
    Hashtable cons = new Hashtable();
    JOTDBJDBCSetup setup;
    // 3 a least for maximum, the VF NEEDS at least 3 concurrent connection to work properly (at least 2 aniway :-)
    int maxSize = 10;
    int lastIndex = 0;
    int minSize = 1;
    int initSize = minSize;
    long lastStatus = -1;
    String name="";


    /**
     * Create appol for a particular DB
     *
     *@param  setup  Description of Parameter
     */
    public JOTDBPool(String name, JOTDBJDBCSetup setup) throws Exception
    {
    	this.name=name;
        this.setup = setup;
        maxSize = setup.getMaxConnections();
        status = new boolean[maxSize];
        lastAccess = new long[maxSize];

        lastIndex = initSize;
        for (int i = 0; i != maxSize; i++)
        {
            status[i] = false;
        }
        for (int i = 0; i <= initSize; i++)
        {
            renewConnection(i);
        }
        JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this, "Initializing DB Pool for "+name);
        start();
    }


    /**
     *The run thread method is there as a whatchdog
     *It will try to find out when connections aren't needed anymore
     *And free them if possible.
     * It also frees "locked" connections.
     * This should be threadsafe.
     */
    public void run()
    {
    	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.DEBUG_LEVEL, this, "Starting Pool: "+name);
        while (true && !shutdown)
        {
            boolean done = false;
            long now = new Date().getTime();
            try
            {
                sleep(25000);
            } catch (Exception e)
            {
            }
            //check every 1 mn
            //log(1, "DBPool", "Cleaning up db connections");
            int released = 0;
            int locked = 0;
            synchronized (this)
            {
                // downsizing pool if possible
                for (int i = lastIndex; i >= minSize && !done; i--)
                {
                    // cleaning connections unused for over 5 hours
                    if (now - lastAccess[i] > 60000 * 60 * 5)
                    {
                        released++;
                        try
                        {
                            dropConnection(i);
                        } catch (Exception e)
                        {
                        	JOTLogger.logException(JOTLogger.CAT_DB,JOTLogger.ERROR_LEVEL, this, "Failed releasing connection for "+name , e);
                        }
                        lastIndex--;
                    } else
                    {
                        done = true;
                    }

                }
                //renew locked connection
                for (int i = lastIndex; i >= 0; i--)
                {
                    if (now - lastAccess[i] > 60000 * 60 * 5)
                    {
                        if (status[i] == true)
                        {
                            locked++;
                            try
                            {
                                renewConnection(i);
                            }
                            catch(Exception e)
                            {
                                JOTLogger.logException(JOTLogger.CAT_DB,JOTLogger.ERROR_LEVEL,this,"Failed reneweing a DB Connection !!", e);
                            }
                        }
                    }
                }
            }

            if (released != 0)
            {
            	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this, name+":" + released + " db connections where released because unused down to " + lastIndex);
            }
            if (locked != 0)
            {
            	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this, name+":"  + locked + " db connections where renewed, because they were locked down to" + lastIndex);
            }

            if (now - lastStatus > statusGap)
            {
            	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this, name+": Pool size at " + new Date().toString() + " : " + lastIndex);
                lastStatus = now;
            }
        }
        JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this, "Stopping Pool: "+name);
	
    }


    /**
     * free a connection when not usefull anymore.
     *
     *@param  con  Description of Parameter
     */
    public void releaseConnection(JOTTaggedConnection con)
    {
        releaseConnection(con.getConnection(), con.getId());
    }


    /**
     *  Description of the Method
     *
     *@param  con  Description of the Parameter
     *@param  i    Description of the Parameter
     */
    public void releaseConnection(Connection con, int i)
    {
    	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.DEBUG_LEVEL, this,name+": Freing connection: " + i);
        status[i] = false;
    }



    /**
     * This get a connection from the pool
     * Returns the "first available".
     * This is synchronized since multithread.
     *
     *@return    The connection value
     */
    public synchronized JOTTaggedConnection retrieveConnection() throws Exception
    {
    	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.DEBUG_LEVEL, this,name+": Retrieving a connection");
        if (shutdown)
        {
            // this should not happen but we protect against problems anyway.
        	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.WARNING_LEVEL, this,name+": Apparently i got shutdown even though i should not, restarting");
            shutdown = false;
            start();
        }

        int maxDelay = 30000;
        int gap = 1500;
        int i;

        int delay = 0;
        do
        {
            i = 0;
            while (i < maxSize && status[i] != false)
            {
                i++;
            }
            if (i >= maxSize)
            {
            	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.WARNING_LEVEL, this,name+": Couldn't get a connection to the db, waiting " + gap + " ms");
                try
                {
                    Thread.sleep(gap);
                } catch (Exception e)
                {
                }
                delay += gap;
            }
        } while (i >= maxSize && delay < maxDelay);
        if (i >= maxSize)
        {
        	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.ERROR_LEVEL, this,name+": Reached max tries to get a connection to the db");
			JOTLogger.logException(JOTLogger.CAT_DB, this, name, new Exception("Trace: "));
            return null;
        }
        if (i > lastIndex)
        {
            renewConnection(i);
            lastIndex = i;
        }
        status[i] = true;
        Connection con = null;
        con = (Connection) cons.get("" + i);
        boolean ok = true;
        try
        {
            if (con == null || con.isClosed())
            {
                ok = false;
            }
        } catch (Exception e)
        {
            ok = false;
        }
        if (!ok)
        {
        	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.WARNING_LEVEL, this, name+": A db connection seems to have died (" + i + "), renewing it");
            //trying to renew it
            renewConnection(i);
            con = (Connection) cons.get("" + i);
        }
        updateAccessTime(i);
        return new JOTTaggedConnection(name, i, con);
    }


    /**
     * Renew a connection when it got corrupted.
     *
     *@param  index  Description of Parameter
     *@return        Description of the Returned Value
     */
    public synchronized Connection renewConnection(int index) throws Exception
    {
    	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this,name+": Renewing connection: " + index);

        Connection con = null;
        Object obj = cons.get("" + index);
        if (obj != null)
        {
            con = (Connection) obj;
            try
            {
                con.close();
            } catch (Exception e)
            {
            	JOTLogger.logException(JOTLogger.CAT_DB,JOTLogger.ERROR_LEVEL, this,name+": Couldn't close a db connection: " , e);
            }
            con = null;
            cons.remove("" + index);
        }

        try
        {
        	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this,name+": Trying to open a connection to :" + setup.getURL() + " / " + setup.getUser());

            Properties prop = new java.util.Properties();
            prop.put("user", setup.getUser());
            prop.put("password", setup.getPassword());
            prop.put("useUnicode", "" + setup.getUseUnicode());
            if (setup.getEncoding() != null)
            {
                prop.put("characterEncoding", setup.getEncoding());
            }

            con = DriverManager.getConnection(setup.getURL(), prop);
            cons.put("" + index, con);
            updateAccessTime(index);
            status[index] = false;
        } catch (Exception e)
        {
            status[index] = false;
            JOTLogger.logException(JOTLogger.CAT_DB,JOTLogger.ERROR_LEVEL, this,name+": Couldn't renew a connection to the db: " + setup.getURL(), e);
            throw(new Exception("Couldn't renew a connection to the db: " + setup.getURL(),e));
        }
        return con;
    }


    /**
     *  Release/removes a connection
     *
     *@param  i  Description of the Parameter
     */
    public synchronized void dropConnection(int i)
    {
    	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this,name+": Dropping connection" + i + " (last:" + lastIndex + ")");
        status[i] = false;
        try
        {
            Connection con = (Connection) cons.get("" + i);
            con.close();
        } catch (Exception e)
        {
        	JOTLogger.logException(JOTLogger.CAT_DB,JOTLogger.ERROR_LEVEL, this,name+": Error dropping a connection: " , e);
        }
        cons.remove("" + i);
    }


    /**
     * Release/closes all the connections
     */
    public void dropAll()
    {
        for (int i = 0; i != lastIndex; i++)
        {
            dropConnection(i);
        }
        lastIndex = 0;
    }


    /**
     *  Terminates a pool and releases resources
     */
    public void shutdown()
    {
    	JOTLogger.log(JOTLogger.CAT_DB,JOTLogger.INFO_LEVEL, this,name+": Shutting down !");
        dropAll();
        shutdown = true;
        interrupt();
    }


    /**
     *  will automatically call shutdown when java app is terminated, to avoid hung DB connections.
     */
    public void finalize() throws Throwable
    {
        shutdown();
        super.finalize();
    }


    /**
     *  Updates the last time a pooled connection was used, so we can manage it better.
     *
     *@param  dataId  Description of the Parameter
     */
    public synchronized void updateAccessTime(int id)
    {
        lastAccess[id] = new Date().getTime();
    }
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar