JdbcPoolManagerDB.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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    private boolean autocommit = true;
00071 
00072 
00089   public JdbcPoolManagerDB () {
00090         this(
00091                         DEFAULT_ALIAS
00092                         ,PropertiesManager.getManager().getProperty("server.database.dbDriver")
00093         , PropertiesManager.getManager().getProperty("server.database.dbServer")
00094         , PropertiesManager.getManager().getProperty("server.database.dbLogin")
00095         , PropertiesManager.getManager().getProperty("server.database.dbPassword")
00096         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.monitorInterval"))
00097         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.maxConns"))
00098         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.idleTimeout"))
00099         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.checkoutTimeout"))
00100         , Integer.parseInt(PropertiesManager.getManager().getProperty("server.database.pool.maxCheckout"))
00101         , PropertiesManager.getManager().getProperty("server.database.shutDownQuery")
00102      );
00103   }
00104   public JdbcPoolManagerDB (
00105                         String alias
00106                         ,String srvdriver
00107                         , String dbserver
00108                         , String srvlogin
00109                         , String srvpassword
00110                         , int monitorinterval
00111                         , int maxconnection
00112                         , int idltimeout
00113                         , int checkouttimeout
00114                         , int maxcheckout
00115                         , String shutdownquery
00116                 ) {
00117         poolAlias = alias;
00118         driver = srvdriver;
00119         monitorInterval=monitorinterval;
00120         server = dbserver;
00121         login = srvlogin;
00122         password = srvpassword;
00123         maxConnections = maxconnection;
00124         idleTimeout = idltimeout;
00125         checkoutTimeout = checkouttimeout;
00126         maxCheckout = maxcheckout;
00127     this.shutdownQuery = shutdownquery;
00128     try {
00129 
00130       if ( driver == null) {// no DB
00131           LogManager.traceInfo(LogServices.DATABASESERVICE, "Undefined DB");
00132           return;
00133       }
00134       this.connect();
00135 
00136     } catch (Exception e) {
00137       LogManager.traceAlert(LogServices.SERVERSERVICE, new ServiceException("database connection error", e));
00138     }
00139   }
00140   
00141   public void connect() throws ServiceException {
00142     try {
00143         
00144             poolManager = new ConnectionPoolManager(monitorInterval);
00145             Class.forName(driver).newInstance();
00146             poolManager.addAlias(poolAlias, driver, server, login, password, maxConnections, idleTimeout, checkoutTimeout, maxCheckout);
00147             if (DatabaseConst.debug)  {
00148               poolManager.getPool(poolAlias).setTracing(true);
00149             }
00150             openedConnection = new HashMap(maxConnections);
00151             LogManager.traceInfo(LogServices.DATABASESERVICE, "DB open successfully");
00152     } catch (Exception e) {
00153       LogManager.traceAlert(LogServices.SERVERSERVICE, new ServiceException("database connection error", e));
00154       throw new ServiceException(e);
00155     }
00156   }
00157 
00164   public void disconnect () {
00165     try {
00166       if (DatabaseConst.debug)  {
00167         // before closing, retreive the stats of the current db session
00168         ConnectionPool p = poolManager.getPool(poolAlias);
00169         LogManager.traceInfo(LogServices.SERVERSERVICE, " Current size: " + p.size() + " of " + p.getMaxConn());
00170         LogManager.traceInfo(LogServices.SERVERSERVICE, "  Connection requests: " + p.getNumRequests());
00171         LogManager.traceInfo(LogServices.SERVERSERVICE, "  Number of waits: " + p.getNumWaits());
00172         LogManager.traceInfo(LogServices.SERVERSERVICE, "  Number of timeouts: " +p.getNumCheckoutTimeouts());
00173       }
00174       // perform shutdown if necessary
00175       if (shutdownQuery != null) {
00176         Connection connection = this.getConnection();
00177         if (connection != null) {
00178           Statement stat = connection.createStatement();
00179           stat.executeUpdate(shutdownQuery);
00180           stat.close();
00181           connection.close();
00182         }
00183         if (DatabaseConst.debug)  {
00184           LogManager.traceInfo(LogServices.SERVERSERVICE, "executed shutdown : " + shutdownQuery);
00185         }
00186       }
00187       // close pool connections
00188       poolManager.removeAlias(poolAlias);
00189       openedConnection = null;
00190       if (DatabaseConst.debug)  {
00191         LogManager.traceInfo(LogServices.SERVERSERVICE, "The DB is closed");
00192       }
00193     }  catch (Exception e) {
00194       LogManager.traceAlert(LogServices.SERVERSERVICE, new ServiceException("Impossible to close the database", e));
00195     }
00196   }
00197   
00198   public boolean isConnected()  {
00199         return (openedConnection !=null);
00200   }
00201 
00209   public Connection getConnection () throws java.sql.SQLException {
00210    // return DriverManager.getConnection(ConnectionPoolManager.URL_PREFIX + alias);
00211    if (! this.isConnected()) {
00212                 try     {
00213                         this.connect();
00214                 } catch (ServiceException ex)   {
00215                         LogManager.traceError(LogServices.SERVERSERVICE, ex);
00216                         throw new SQLException(ex.getMessage());
00217                 }
00218    }
00219    this.garbageOpenedConnection();
00220    Connection connection = poolManager.getPool(poolAlias).getConnection();
00221    connection.setAutoCommit(this.isAutocommit());
00222    openedConnection.put(Thread.currentThread(), connection);
00223    return connection;
00224   }
00225 
00226   public void garbageOpenedConnection()  {
00227     Thread currentThread = Thread.currentThread();
00228     Object obj = openedConnection.get(currentThread);
00229     while (obj != null) {
00230       try {
00231         ((Connection)obj).setAutoCommit(true);
00232         ((Connection)obj).close();
00233       } catch (SQLException ex) {
00234         LogManager.trace(new ServiceException("JdbcPoolManagerDB error during garbage of connexion", ex));
00235       }
00236       openedConnection.remove(currentThread);
00237       obj = openedConnection.get(currentThread);
00238    }
00239   }
00240         public boolean isAutocommit() {
00241                 return autocommit;
00242         }
00243         public void setAutocommit(boolean autocommit) {
00244                 this.autocommit = autocommit;
00245         }
00246 
00247 
00248  }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4