title: How can I add username and password at runtime for Tomcate Datasource with Oracle UCP
body: <p>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.</p>
  
  <p>Here is my code samples</p>
  
  <p>server.xml in tomcat</p>
  
  <pre><code>&lt;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" /&gt;
  </code></pre>
  
  <p>CustomizeOracleUCPDataSource .java</p>
  
  <pre><code>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();
      }
  
  
  }
  </code></pre>
  
  <p>CustomizeOracleUCPDataSource2.java</p>
  
  <pre><code>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);
      }
  
  
  
  }
  </code></pre>
  
  <p>Nothing is working out my user name and passwords were printed in console, but couldn't able to set to the datasource.</p>
  
  <p>I am getting below log info while datasource was getting created.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>With the details aI am not able to create a datasource I am getting below Error :</p>
  
  <pre><code> Error occurred while establishing connection to the Test database
  </code></pre>
  
  <p>Any help could be greatly appreciated.</p>
  
  <p>I am using Ojdbc8.jar,ucp.jar</p>