package com.base;
|
|
import java.sql.Connection;
|
import java.sql.SQLException;
|
import java.util.Properties;
|
|
import javax.sql.DataSource;
|
|
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
|
import org.apache.commons.dbcp.PoolableConnectionFactory;
|
import org.apache.commons.dbcp.PoolingDataSource;
|
import org.apache.commons.pool.impl.GenericObjectPool;
|
|
public class ConnectionFactory {
|
private static interface Singleton {
|
final ConnectionFactory INSTANCE = new ConnectionFactory();
|
}
|
|
private final DataSource dataSource;
|
|
private ConnectionFactory() {
|
|
try {
|
Class.forName("com.mysql.jdbc.Driver");
|
} catch (ClassNotFoundException e) {
|
e.printStackTrace();
|
System.exit(0);
|
}
|
|
Properties properties = new Properties();
|
properties.setProperty("user", "root");
|
properties.setProperty("password", "lmx8688139");
|
|
GenericObjectPool<Object> pool = new GenericObjectPool<Object>();
|
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
|
"jdbc:mysql://localhost:3360/db_app_sys", properties);
|
|
new PoolableConnectionFactory(
|
connectionFactory, pool, null,"SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
|
);
|
|
this.dataSource = new PoolingDataSource(pool);
|
}
|
|
public static Connection getDatabaseConnection() throws SQLException {
|
return (Connection) Singleton.INSTANCE.dataSource.getConnection();
|
//return (Connection) Singleton.INSTANCE.GB_MysqlConnPool.getConn();
|
|
}
|
}
|