我使用单例模式为我的独立应用程序获取jdbc连接。代码如下。
public static synchronized MysqlConnect getDbCon() {
if ( db == null ) {
db = new MysqlConnect();
}
return db;
}
但是我在很多讨论中看到单身人士不善于获得联系。这是真的吗? 并建议使用连接池。任何人都可以给我一个很好的连接池实现来代替上面的代码吗?
答案 0 :(得分:0)
这是一个基于单例的简单连接池实现。此连接池支持使用DriverManager接口或通过DataSource(JNDI)获取连接。
我将这个连接池用于我的一些项目(虽然实际实现稍微复杂一些)
public class ConnectionPool{
private String jdbcDriver;
private String jdbcURL;
private String user;
private String password;
private int connectionCount=10;
private List<Connection> connections;
private static boolean loadFromDataSource;
private static DataSource dataSource;
private static ConnectionPool connPool;
private ConnectionPool() throws EasyORMException{}
private void setConnection(String jdbcDriver, String jdbcURL, String user, String password,String dbDataSource) throws EasyORMException{
this.jdbcDriver = jdbcDriver;
this.jdbcURL=jdbcURL;
this.user=user;
this.password=password;
connections= new ArrayList<Connection>();
connections.add((ConnectionPool.loadFromDataSource) ? getConnectionFromDataSource() : getConnection());
}
static ConnectionPool getInstance(){
return connPool;
}
private static ConnectionPool getInstanceFromJndi(String propertyFile,boolean loadFromJndi) throws EasyORMException{
ConnectionProp cp=readPropFromFile(propertyFile);
if(loadFromJndi){
dataSource=createDatasource(cp.getDataSource());
loadFromDataSource=true;
}
return ConnectionPool.createConnectionPool(cp.getJdbcDriver(),cp.getDbURL(), cp.getUsername(), cp.getPassword(),cp.getDataSource())
}
public static ConnectionPool getInstance(String propertyFile,boolean loadFromJndi) throws EasyORMException{
return ConnectionPool.getInstanceFromJndi(propertyFile, loadFromJndi, false);
}
public static ConnectionPool getInstance(ConnectionProp cp) throws EasyORMException{
return ConnectionPool.createConnectionPool(cp.getJdbcDriver(),cp.getDbURL(), cp.getUsername(), cp.getPassword(),cp.getDataSource());
}
public static ConnectionPool getInstance(String jndiName) throws EasyORMException{
dataSource=createDatasource(jndiName);
loadFromDataSource=true;
return ConnectionPool.createConnectionPool(null,null, null, null,jndiName);
}
public static ConnectionPool getInstance(String jdbcDriver, String jdbcURL, String user, String password) throws EasyORMException{
return ConnectionPool.createConnectionPool(jdbcDriver,jdbcURL, user, password,null);
}
private static ConnectionPool createConnectionPool(String jdbcDriver, String jdbcURL, String user, String password,String dbDataSource) throws EasyORMException{
if(connPool==null) {
connPool = new ConnectionPool();
connPool.setConnection(jdbcDriver, jdbcURL, user, password, dbDataSource);
}
return connPool;
}
synchronized Connection getAvailableConnection() throws EasyORMException {
Connection conn=null;
int connSize = connections.size();
if(connSize>0){
conn=connections.remove(connSize-1);
}else{
if(connSize<connectionCount){
for(int i=0;i<initialConnCount;i++)
conn=(ConnectionPool.loadFromDataSource)?getConnectionFromDataSource() :getConnection();
}else{
throw new EasyORMException(EasyORMException.CONNECTION_NUM_EXCEEDED);
}
}
return conn;
}
synchronized void returnConnection(Connection conn){
connections.add(conn);
}
private Connection getConnection() throws EasyORMException {
Connection conn=null;
try {
Class.forName(jdbcDriver);
conn = DriverManager.getConnection(jdbcURL, user, password);
} catch (ClassNotFoundException e) {
throw new EasyORMException(e);
} catch (SQLException e) {
throw new EasyORMException(e);
}
return conn;
}
private Connection getConnectionFromDataSource() throws EasyORMException {
try{
return dataSource.getConnection();
}catch(SQLException e){
throw new EasyORMException(e);
}
}
public void setNumberOfConnections(int count){
this.connectionCount=count;
}
public int getNumberOfConnections(){
return connectionCount;
}
private static DataSource createDatasource(String jndiDb)throws EasyORMException{
InitialContext initCtx=null;
try {
initCtx = new InitialContext();
return (DataSource)initCtx.lookup("java:comp/env/"+jndiDb);
} catch (NamingException e) {
throw new EasyORMException(e);
}finally{
if(initCtx!=null){
try {
initCtx.close();
} catch (NamingException e) {
throw new EasyORMException(e);
}
}
}
}
}
某些方法无法公开访问(因为它们是作为库的一部分编写的)但您可以根据需要将其更改为公开。