我试图使用 Oracle UCP 在 tomcat 中创建数据源,但我的要求不是在服务器上添加密码.xml ,我需要在服务器运行时添加它,我已经尝试了很多方法但没有奏效。
这是我的代码示例
服务器.xml在汤姆卡特
<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);
}
}
没有任何结果,我的用户名和密码在控制台中打印,但无法设置为数据源。
创建数据源时,我得到的日志信息低于以下。
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
有了详细信息,我无法创建数据源,我在下面得到错误:
Error occurred while establishing connection to the Test database
任何帮助将不胜感激。
我正在使用Ojdbc8.jar,ucp.jar
最后我在tomcat中实现了密码的加密和解密,此解决方案将适用于您的所有密码问题,请访问此链接以获取更多详细信息。
Tomcat 支持密码加密和解密