private static final void initDataSource(){
if( DATA_SOURCE == null ){
synchronized( JdbcUtil.class ){
if( DATA_SOURCE == null ){
Configuration conf = Configuration.getInstance();
String driver = conf.getString("db.driver", "com.mysql.jdbc.Driver");
String uri = conf.getString("db.uri", "");
String username = conf.getString("db.username", "");
String password = conf.getString("db.password", "");
Properties p = new Properties();
p.setProperty("driverClassName", driver);
p.setProperty("url", uri);
p.setProperty("password", username);
p.setProperty("username", password);
p.setProperty("maxActive", "30");
p.setProperty("maxIdle", "10");
p.setProperty("maxWait", "1000");
p.setProperty("removeAbandoned", "false");
p.setProperty("removeAbandonedTimeout", "120");
p.setProperty("poolPreparedStatements", "true");
p.setProperty("maxOpenPreparedStatements", "1000");
try {
DATA_SOURCE = BasicDataSourceFactory.createDataSource(p);
} catch (Exception e) {
throw new ExceptionInInitializerError();
}
}
}
}
}
public static final Connection getConnection(){
initDataSource();
if( DATA_SOURCE != null ){
try {
return DATA_SOURCE.getConnection();
} catch (SQLException e) {
LOG.error(e.getMessage(),e);
}
}
return null;
}
转载于:https://my.oschina.net/aicoding/blog/151337