KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > database > MultiPoolHandler


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.database;
11
12 import java.sql.*;
13 import java.util.*;
14
15 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
16 /**
17  * MultiPoolHandler handles multi pools so we can have more than one database
18  * open and they can all have a multipool.
19  *
20  */

21  
22 import org.mmbase.util.logging.Logger;
23 import org.mmbase.util.logging.Logging;
24
25 public class MultiPoolHandler {
26     private static final Logger log = Logging.getLoggerInstance(MultiPoolHandler.class);
27     private int maxConnections;
28     private int maxQueries;
29     private Map pools = new ConcurrentHashMap();
30
31     private DatabaseSupport databaseSupport;
32     private long maxLifeTime = 120000;
33
34     public MultiPoolHandler(DatabaseSupport databaseSupport, int maxConnections) {
35         this(databaseSupport, maxConnections, 500);
36     }
37
38     public MultiPoolHandler(DatabaseSupport databaseSupport, int maxConnections,int maxQueries) {
39     this.maxConnections = maxConnections;
40     this.maxQueries = maxQueries;
41     this.databaseSupport= databaseSupport;
42     }
43
44     /**
45      * @since MMBase-1.8.3
46      */

47     void setMaxLifeTime(long l) {
48         maxLifeTime = l;
49     }
50
51     public MultiConnection getConnection(String JavaDoc url, String JavaDoc name, String JavaDoc password) throws SQLException {
52     MultiPool pool = (MultiPool) pools.get(url + "," + name + "," + password);
53     if (pool != null) {
54         return pool.getFree();
55     } else {
56             log.service("No multipool present, creating one now");
57             pool = new MultiPool(databaseSupport, url, name, password, maxConnections, maxQueries);
58             pool.setMaxLifeTime(maxLifeTime);
59             if (pools.put(url + "," + name + "," + password, pool) != null) {
60                 log.error("Replaced an old MultiPool!? " + Logging.stackTrace());
61             }
62             return pool.getFree();
63     }
64     }
65
66     /**
67      * Calls shutdown of all registered MultiPools
68      * @since MMBase-1.6.2
69      */

70     public void shutdown() {
71         for (Iterator i = pools.values().iterator(); i.hasNext();) {
72             MultiPool pool = (MultiPool) i.next();
73         pool.shutdown();
74     }
75     }
76
77     public void checkTime() {
78     for (Iterator i = pools.values().iterator(); i.hasNext();) {
79         MultiPool pool = (MultiPool) i.next();
80         pool.checkTime();
81     }
82     }
83
84     public Set keySet() {
85     return pools.keySet();
86     }
87
88     /*
89     public Enumeration keys() {
90     return pools.keys();
91     }
92     */

93
94     public MultiPool get(String JavaDoc id) {
95         return (MultiPool) pools.get(id);
96     }
97
98     public void setMaxConnections(int max) {
99         maxConnections = max;
100     }
101
102     public int getMaxConnections() {
103         return maxConnections;
104     }
105
106     public void setMaxQuerys(int max) {
107     maxQueries = max;
108     }
109
110     public int getMaxQuerys() {
111     return maxQueries;
112     }
113 }
114
Popular Tags