jdbcc3p0apache-commons-dbcp

how to set driver connection properties using c3p0 libraries?


I am Migrating from dbcp2 to c3p0,

We create a datasource by extending the BasicDataSource from dbcp2 and setting the properties. Some properties are set at driver level via setConnectionProperties method.

I dont see such a provision in c3p0 when extending the AbstractComboPooledDataSource. Is there another way to set the same?

Digging through the docs, I found something called a connectionCustomizer, but not sure if it does the same

This is how I am currently setting the properties with dbcp2:

this.setConnectionProperties("driver:oracle.jdbc.ReadTimeout=180000");
this.setConnectionProperties("driver:oracle.net.CONNECT_TIMEOUT=180000");

where "this" is class which extends BasicDataSource

Is there anyway in c3p0 to get the same result?

EDIT:

Just to be clear, I am able to set the properties provided by the c3p0 library, what I am looking for is setting properties at driver level the way dbcp2 allows to do via the SetConnectionProperties() method.THanks


Solution

  • I found the answer in this answer: c3p0 hangs on getConnection when there is a network failure.

    cpds = new ComboPooledDataSource();
    ...
    
    //--------------------------------------------------------------------------------------
    // NOTE: Once you decide to use cpds.setProperties() to set some connection properties,
    //       all properties must be set, including user/password, otherwise an exception
    //       will be thrown
    Properties prop = new Properties();
    prop.setProperty("oracle.net.CONNECT_TIMEOUT",
        Integer.toString(JDBC_CONNECTION_TIMEOUT_IN_MILLISECONDS));
    prop.setProperty("oracle.jdbc.ReadTimeout",
        Integer.toString(JDBC_SOCKET_TIMEOUT_IN_MILLISECONDS));
    prop.setProperty("user", username);
    prop.setProperty("password", password);
    cpds.setProperties(prop);