In this post we’ll see how to create a connection pool using C3P0 datasource. The DB we are connecting to is MySQL.
Jars needed
You need the following jars in your project’s classpath, check the versions as per your Java and DB versions.
c3p0-0.9.5.2.jar
mchange-commons-java-0.2.11.jar
Download path - https://sourceforge.net/projects/c3p0/
Example Code
In the example code there are two Java classes. We have a DataSource class that is a singleton class creating and returning the instance of C3P0 ComboPooledDataSource.
There is another class DSConnection where we get the instance of ComboPooledDataSource and use it to get the Connection object.DataSource.java
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSource {
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION_URL = "jdbc:mysql://localhost:3306/netjs";
private static final String DB_USER = "root";
private static final String DB_PWD = "admin";
private static DataSource ds;
private ComboPooledDataSource cpds = new ComboPooledDataSource();
//private constructor
private DataSource() throws PropertyVetoException{
cpds.setDriverClass(DRIVER_CLASS); //loads the jdbc driver
cpds.setJdbcUrl(DB_CONNECTION_URL);
cpds.setUser(DB_USER);
cpds.setPassword(DB_PWD);
// the settings below are optional
// c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
}
/**
*Static method for getting instance.
* @throws PropertyVetoException
*/
public static DataSource getInstance() throws PropertyVetoException{
if(ds == null){
ds = new DataSource();
}
return ds;
}
public ComboPooledDataSource getCpds() {
return cpds;
}
public void setCpds(ComboPooledDataSource cpds) {
this.cpds = cpds;
}
}
In this class, apart from setting the DB properties, we have set some of the parameters for the connection pool like setMinPoolSize() that sets the initial size of the connection pool. These many connection will immediately be created and put to connection pool, setMaxPoolSize() to set the maximum limit on the connection pool.
DSConnection.java
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DSConnection {
public static void main(String[] args) throws PropertyVetoException {
DSConnection dsCon = new DSConnection();
try {
dsCon.displayEmployee(6);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param connection
* @param id
* @throws SQLException
* @throws PropertyVetoException
*/
private void displayEmployee(int id) throws SQLException, PropertyVetoException{
Connection connection = null;
String selectSQL = "Select * from employee where id = ?";
PreparedStatement prepStmt = null;
try {
ComboPooledDataSource basicDS = DataSource.getInstance().getCpds();
connection = basicDS.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
prepStmt.setInt(1, id);
ResultSet rs = prepStmt.executeQuery();
while(rs.next()){
System.out.println("id : " + rs.getInt("id") + " Name : "
+ rs.getString("name") + " Age : " + rs.getInt("age"));
}
}finally{
if(prepStmt != null){
prepStmt.close();
}
if(connection != null){
connection.close();
}
}
}
}
That's all for this topic Connection Pooling Using C3P0 in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like -