00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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) {
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
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
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
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
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 }