Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

JdbcPoolManagerDB.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.common.util.database;
00030 
00031 import java.sql.*;
00032 import java.util.HashMap;
00033 import com.bitmechanic.sql.*;
00034 
00035 import org.openmobileis.common.util.PropertiesManager;
00036 import org.openmobileis.common.util.log.*;
00037 import org.openmobileis.common.util.exception.ServiceException;
00038 
00053 public class JdbcPoolManagerDB extends ManagerDB {
00054 
00055    // Store the connection of the database
00056    private ConnectionPoolManager poolManager;
00057    private static String DEFAULT_ALIAS = "JDBCPoolManagerDB";
00058    private String poolAlias;
00059    private String shutdownQuery = null;
00060    private HashMap openedConnection;
00061    private int monitorInterval=0;
00062    private String driver;
00063    private String server = null;
00064    private String login = null;
00065    private String password = null;
00066    private int maxConnections = 0;
00067    private int idleTimeout = 0;
00068    private int checkoutTimeout = 0;
00069    private int maxCheckout = 0;
00070 
00071 
00088   public JdbcPoolManagerDB () {
00089         this(
00090                         DEFAULT_ALIAS
00091                         ,PropertiesManager.getManager().getProperty("server.database.dbDriver")
00092         , PropertiesManager.getManager().getProperty("server.database.dbServer")
00093         , PropertiesManager.getManager().getProperty("server.database.dbLogin")
00094         , PropertiesManager.getManager().getProperty("server.database.dbPassword")
00095         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.monitorInterval"))
00096         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.maxConns"))
00097         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.idleTimeout"))
00098         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.checkoutTimeout"))
00099         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.maxCheckout"))
00100         , PropertiesManager.getManager().getProperty("server.database.shutDownQuery")
00101      );
00102   }
00103   public JdbcPoolManagerDB (
00104                         String alias
00105                         ,String srvdriver
00106                         , String dbserver
00107                         , String srvlogin
00108                         , String srvpassword
00109                         , int monitorinterval
00110                         , int maxconnection
00111                         , int idltimeout
00112                         , int checkouttimeout
00113                         , int maxcheckout
00114                         , String shutdownquery
00115                 ) {
00116         poolAlias = alias;
00117         driver = srvdriver;
00118         monitorInterval=monitorinterval;
00119         server = dbserver;
00120         login = srvlogin;
00121         password = srvpassword;
00122         maxConnections = maxconnection;
00123         idleTimeout = idltimeout;
00124         checkoutTimeout = checkouttimeout;
00125         maxCheckout = maxcheckout;
00126     this.shutdownQuery = shutdownquery;
00127     try {
00128 
00129       if ( driver == null) {// no DB
00130           LogManager.traceInfo(LogServices.DATABASESERVICE, "Undefined DB");
00131           return;
00132       }
00133       this.connect();
00134 
00135     } catch (Exception e) {
00136       LogManager.traceAlert(LogServices.SERVERSERVICE, new ServiceException("database connection error", e));
00137     }
00138   }
00139   
00140   public void connect() throws ServiceException {
00141     try {
00142         
00143             poolManager = new ConnectionPoolManager(monitorInterval);
00144             Class.forName(driver).newInstance();
00145             poolManager.addAlias(poolAlias, driver, server, login, password, maxConnections, idleTimeout, checkoutTimeout, maxCheckout);
00146             if (DatabaseConst.debug)  {
00147               poolManager.getPool(poolAlias).setTracing(true);
00148             }
00149             openedConnection = new HashMap(maxConnections);
00150             LogManager.traceInfo(LogServices.DATABASESERVICE, "DB open successfully");
00151     } catch (Exception e) {
00152       LogManager.traceAlert(LogServices.SERVERSERVICE, new ServiceException("database connection error", e));
00153       throw new ServiceException(e);
00154     }
00155   }
00156 
00163   public void disconnect () {
00164     try {
00165       if (DatabaseConst.debug)  {
00166         // before closing, retreive the stats of the current db session
00167         ConnectionPool p = poolManager.getPool(poolAlias);
00168         LogManager.traceInfo(LogServices.SERVERSERVICE, " Current size: " + p.size() + " of " + p.getMaxConn());
00169         LogManager.traceInfo(LogServices.SERVERSERVICE, "  Connection requests: " + p.getNumRequests());
00170         LogManager.traceInfo(LogServices.SERVERSERVICE, "  Number of waits: " + p.getNumWaits());
00171         LogManager.traceInfo(LogServices.SERVERSERVICE, "  Number of timeouts: " +p.getNumCheckoutTimeouts());
00172       }
00173       // perform shutdown if necessary
00174       if (shutdownQuery != null) {
00175         Connection connection = this.getConnection();
00176         if (connection != null) {
00177           Statement stat = connection.createStatement();
00178           stat.executeUpdate(shutdownQuery);
00179           stat.close();
00180           connection.close();
00181         }
00182         if (DatabaseConst.debug)  {
00183           LogManager.traceInfo(LogServices.SERVERSERVICE, "executed shutdown : " + shutdownQuery);
00184         }
00185       }
00186       // close pool connections
00187       poolManager.removeAlias(poolAlias);
00188       openedConnection = null;
00189       if (DatabaseConst.debug)  {
00190         LogManager.traceInfo(LogServices.SERVERSERVICE, "The DB is closed");
00191       }
00192     }  catch (Exception e) {
00193       LogManager.traceAlert(LogServices.SERVERSERVICE, new ServiceException("Impossible to close the database", e));
00194     }
00195   }
00196   
00197   public boolean isConnected()  {
00198         return (openedConnection !=null);
00199   }
00200 
00208   public Connection getConnection () throws java.sql.SQLException {
00209    // return DriverManager.getConnection(ConnectionPoolManager.URL_PREFIX + alias);
00210    if (! this.isConnected()) {
00211                 try     {
00212                         this.connect();
00213                 } catch (ServiceException ex)   {
00214                         LogManager.traceError(LogServices.SERVERSERVICE, ex);
00215                         throw new SQLException(ex.getMessage());
00216                 }
00217    }
00218    this.garbageOpenedConnection();
00219    Connection connection = poolManager.getPool(poolAlias).getConnection();
00220    openedConnection.put(Thread.currentThread(), connection);
00221    return connection;
00222   }
00223 
00224   public void garbageOpenedConnection()  {
00225     Thread currentThread = Thread.currentThread();
00226     Object obj = openedConnection.get(currentThread);
00227     while (obj != null) {
00228       try {
00229         ((Connection)obj).close();
00230       } catch (SQLException ex) {
00231         LogManager.trace(new ServiceException("JdbcPoolManagerDB error during garbage of connexion", ex));
00232       }
00233       openedConnection.remove(currentThread);
00234       obj = openedConnection.get(currentThread);
00235    }
00236   }
00237 
00238 
00239  }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4