package com.terac.board;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import javax.sql.DataSource;
import java.sql.SQLException;
public class ConnectionManager {
private final static ConnectionManager instance = new ConnectionManager();
private DataSource ds;
public static ConnectionManager getInstance() {
return instance;
}
private ConnectionManager() {
try {
//setupDriver();
this.ds = setupDataSource();
} catch (Exception e) {
//
}
}
public static DataSource setupDataSource() {
try {
Class.forName(Config.getInstance().getDatabaseDriver());
} catch (ClassNotFoundException e) {
//
}
ObjectPool connectionPool = new GenericObjectPool();
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
Config.getInstance().getDatabaseUrl()
, Config.getInstance().getDatabaseUser()
, Config.getInstance().getDatabasePassword());
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
return dataSource;
}
public DataSource getDataSource() throws SQLException {
return ds;
}
}