I was trying to create a Datasource in tomcat with Oracle UCP and my requirement was not add the password at server.xml , I need to add that at server runtime, I have tried in many ways but didn't work.
Here is my code samples
server.xml in tomcat
<Resource
name="testds"
connectionPoolName="testds"
auth="Container"
factory="com.test.tomcat.datasorceEncrypt.CustomizeOracleUCPDataSource"
type="oracle.ucp.jdbc.PoolDataSource"
connectionFactoryClassName="com.test.tomcat.datasorceEncrypt.CustomizeOracleUCPDataSource2"
jmxEnabled="true"
initialPoolSize="10"
minPoolSize="10"
maxPoolSize="300"
fastConnectionFailoverEnabled="true"
url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST=localhost)(PORT=3203))(CONNECT_DATA=
(SERVICE_NAME=employee)))"
sqlForValidateConnection="select 1 from DUAL" />
CustomizeOracleUCPDataSource .java
package com.test.tomcat.datasorceEncrypt;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import oracle.ucp.jdbc.PoolDataSourceImpl;
public class CustomizeOracleUCPDataSource extends PoolDataSourceImpl {
/* public CustomizeOracleUCPDataSource() {
Properties dbProperties = null;
try {
this.setPassword("pass");
this.setUser("user");
System.out.println(this.getUser() + "-------" + this.getPassword());
} catch (Exception e) {
}
}*/
@Override
public Connection getConnection() throws SQLException {
try {
this.setPassword("pass");
this.setUser("user");
System.out.println(this.getUser() + "-------" + this.getPassword());
} catch (Exception e) {
}
return super.getConnection(username, password);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return super.getConnection(username, password);
}
@Override
protected void createPoolWithDefaultProperties() throws SQLException {
try {
this.setPassword("pass");
this.setUser("user");
System.out.println(this.getUser() + "-------" + this.getPassword());
} catch (Exception e) {
}
super.createPoolWithDefaultProperties();
}
}
CustomizeOracleUCPDataSource2.java
package com.test.tomcat.datasorceEncrypt;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import oracle.jdbc.pool.OracleDataSource;
public class CustomizeOracleUCPDataSource2 extends OracleDataSource {
public CustomizeOracleUCPDataSource2() throws SQLException {
super();
this.setPassword("pass");
this.setUser("user");
}
@Override
public Connection getConnection() throws SQLException {
this.setPassword("pass");
this.setUser("user");
return super.getConnection(username, password);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return super.getConnection(username, password);
}
}
Nothing is working out my user name and passwords were printed in console, but couldn't able to set to the datasource.
I am getting below log info while datasource was getting created.
19-Dec-2019 19:34:35.763 FINE [http-nio-8080-exec-1] oracle.ucp.logging.ClioSupport._log oracle.ucp.jdbc.PoolDataSourceImpl:createPoolWithDefaultProperties:oracle.ucp.jdbc.PoolDataSourceImpl@6b938ce5:Connection pool instance is
19-Dec-2019 19:34:35.803 FINE [http-nio-8080-exec-1] oracle.ucp.logging.ClioSupport._log oracle.ucp.jdbc.PoolDataSourceImpl:createPool:oracle.ucp.jdbc.PoolDataSourceImpl@6b938ce5:Connection pool instance is created
With the details aI am not able to create a datasource I am getting below Error :
Error occurred while establishing connection to the Test database
Any help could be greatly appreciated.
I am using Ojdbc8.jar,ucp.jar
Finally I achieved encryption and decryption of password in tomcat , this solution will work for all your password problems please go to this link for more details.
Tomcat support for password encryption and deryption