Beruflich Dokumente
Kultur Dokumente
*
*/
package com.nttuyen.lib.dao.jdbc;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.nttuyen.lib.core.Config;
/**
* @author Admin
*
*/
public class ConnectionPool {
private static final Logger log = Logger.getLogger(ConnectionPool.class);
//Attribute
private final List<Connection> connections = new LinkedList<Connection>();
private final List<Connection> free = new LinkedList<Connection>();
private Config cfg = null;
private final int max;
//private final int timeout;
if(connection == null) {
throw new SQLException("Connectin failure");
}
connections.add(connection);
log.info("No of connection: " + connections.size());
Connection connection =
DriverManager.getConnection(cfg.get("database_url",
"jdbc:mysql://localhost/test").trim(), cfg.get("database_user", "root").trim(),
cfg.get("database_password", "").trim());
return connection;
} catch (ClassNotFoundException e) {
log.error("Driver Class not found", e);
} catch (InstantiationException e) {
log.error("Can't create instace of Driver class", e);
} catch (IllegalAccessException e) {
log.error("Elegal access exception in load Driver class", e);
} catch (SQLException e) {
log.error("Can't connecto to DB", e);
}
return null;
}
log.info("Free connection");
connections.remove(this.connection);
free.add(this.connection);
return null;
}
if(method.getName().equalsIgnoreCase("prepareStatement")) {
if(this.connection == null || this.connection.isClosed() ||
free.contains(this.connection)) {
this.connection = getConnection();
}
PreparedStatement stm = (PreparedStatement)
method.invoke(this.connection, args);
statements.add(stm);
return stm;
}
return method.invoke(this.connection, args);
}
}