Skip to content

Instantly share code, notes, and snippets.

@dotmanila
Created February 25, 2016 05:52
Show Gist options
  • Save dotmanila/ed7217b43a4560ce51da to your computer and use it in GitHub Desktop.
Save dotmanila/ed7217b43a4560ce51da to your computer and use it in GitHub Desktop.
Java source code, using connection pool with c3p0
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSource {
private static DataSource datasource;
private ComboPooledDataSource cpds;
private DataSource() throws IOException, SQLException, PropertyVetoException {
cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver
cpds.setJdbcUrl("jdbc:mysql://192.168.56.31/test");
cpds.setUser("test");
cpds.setPassword("test");
// the settings below are optional -- c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxStatements(180);
}
public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
if (datasource == null) {
datasource = new DataSource();
return datasource;
} else {
return datasource;
}
}
public Connection getConnection() throws SQLException {
return this.cpds.getConnection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment