Search code examples
javaoraclesslapache-commons-dbcp

Secure connection to oracle database using apache commons Basic Datasource


As part of securing all the connection we are enabling ASO in Oracle database, i was able to make secure connection from my java using oracle.jdbc.pool.OracleDataSource. But we have projects using apache basic datasource. I tried the below but still the data is not getting encrypted.

    BasicDataSource ods = new BasicDataSource();
    ods.setUrl(URL);
    ods.setUsername(user);
    ods.setPassword(password);
    ods.setConnectionProperties("AutoCommit=false;");
    ods.setConnectionProperties("OracleConnection.CONNECTION_PROPERTY_THIN_NET_ENCRYPTION_LEVEL=REQUIRED;");
    ods.setConnectionProperties("OracleConnection.CONNECTION_PROPERTY_THIN_NET_ENCRYPTION_TYPES=(AES256);");
    ods.setConnectionProperties("OracleConnection.CONNECTION_PROPERTY_THIN_NET_CRYPTO_SEED=(sfdsvcfdssegdsvg);");

Please let me know if I am missing something in the connection properties. Thanks.


Solution

  • Method BasicDataSource#setConnectionProperties(String) overrides all properties that been added to the DataSource previously

    You need to use method BasicDataSource#addConnectionProperty(String, String) to add single property:

    BasicDataSource ods = new BasicDataSource();
    ods.addConnectionProperty("OracleConnection.CONNECTION_PROPERTY_THIN_NET_ENCRYPTION_LEVEL", "REQUIRED")
    

    or build Properties instance with relevant data, and only then add it to the DataSource:

    Properties prop = new Properties();
    prop.setProperty("OracleConnection.CONNECTION_PROPERTY_THIN_NET_ENCRYPTION_LEVEL", "REQUIRED");
    prop.setProperty("OracleConnection.CONNECTION_PROPERTY_THIN_NET_ENCRYPTION_TYPES", "(AES256)")
    ...
    BasicDataSource ods = new BasicDataSource();
    ods.setConnectionProperties(prop);