001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.database;
011:
012: import java.sql.*;
013: import java.util.*;
014:
015: import java.util.concurrent.ConcurrentHashMap;
016: /**
017: * MultiPoolHandler handles multi pools so we can have more than one database
018: * open and they can all have a multipool.
019: *
020: */
021:
022: import org.mmbase.util.logging.Logger;
023: import org.mmbase.util.logging.Logging;
024:
025: public class MultiPoolHandler {
026: private static final Logger log = Logging
027: .getLoggerInstance(MultiPoolHandler.class);
028: private int maxConnections;
029: private int maxQueries;
030: private Map<String, MultiPool> pools = new ConcurrentHashMap<String, MultiPool>();
031:
032: private DatabaseSupport databaseSupport;
033: private long maxLifeTime = 120000;
034:
035: public MultiPoolHandler(DatabaseSupport databaseSupport,
036: int maxConnections) {
037: this (databaseSupport, maxConnections, 500);
038: }
039:
040: public MultiPoolHandler(DatabaseSupport databaseSupport,
041: int maxConnections, int maxQueries) {
042: this .maxConnections = maxConnections;
043: this .maxQueries = maxQueries;
044: this .databaseSupport = databaseSupport;
045: }
046:
047: /**
048: * @since MMBase-1.8.3
049: */
050: void setMaxLifeTime(long l) {
051: maxLifeTime = l;
052: }
053:
054: public MultiConnection getConnection(String url, String name,
055: String password) throws SQLException {
056: MultiPool pool = pools.get(url + "," + name + "," + password);
057: if (pool != null) {
058: return pool.getFree();
059: } else {
060: log.service("No multipool present, creating one now");
061: pool = new MultiPool(databaseSupport, url, name, password,
062: maxConnections, maxQueries);
063: pool.setMaxLifeTime(maxLifeTime);
064: if (pools.put(url + "," + name + "," + password, pool) != null) {
065: log.error("Replaced an old MultiPool!? "
066: + Logging.stackTrace());
067: }
068: return pool.getFree();
069: }
070: }
071:
072: /**
073: * Calls shutdown of all registered MultiPools
074: * @since MMBase-1.6.2
075: */
076: public void shutdown() {
077: for (MultiPool pool : pools.values()) {
078: pool.shutdown();
079: }
080: }
081:
082: public void checkTime() {
083: for (MultiPool pool : pools.values()) {
084: pool.checkTime();
085: }
086: }
087:
088: public Set<String> keySet() {
089: return pools.keySet();
090: }
091:
092: /*
093: public Enumeration keys() {
094: return pools.keys();
095: }
096: */
097:
098: public MultiPool get(String id) {
099: return pools.get(id);
100: }
101:
102: public void setMaxConnections(int max) {
103: maxConnections = max;
104: }
105:
106: public int getMaxConnections() {
107: return maxConnections;
108: }
109:
110: public void setMaxQuerys(int max) {
111: maxQueries = max;
112: }
113:
114: public int getMaxQuerys() {
115: return maxQueries;
116: }
117: }
|