oracle-databasetomcatservletsdatasourceora-01017

Getting "ORA-01017: invalid username/password; logon denied" when trying to create a data source in Tomcat 6


Been pulling my hair trying to set up a data source in in Tomcat for an application. The steps I've taken are

  1. Create a META-INF/context.xml with the following content

    <Context>
     <Resource name="jdbc/mydb"
      auth="Container"
      type="javax.sql.DataSource"
      driverClassName="oracle.jdbc.OracleDriver"
      url="jdbc:oracle:thin:@gpqa6.myserver.com:1526:gpqa6"
      username="foo"
      password="foobar"
      maxActive="20"
      maxIdle="30"
      maxWait="-1" />           
    </Context>
    
  2. Enter the following text in WEB-INF/web.xml

    <resource-ref>
        <description>Datasource</description>
        <res-ref-name>jdbc/mydb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    
  3. Using it in the code

    public class MyServlet extends HttpServlet {
    
      private DataSource ds;             
    
      public void init() {
    
        try {
        Context ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");
    
        } catch (NamingException e) {           
            e.printStackTrace();
        }
     }
    
      protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException {
        //this is where the exception is thrown
        Connection conn = ds.getConnection(); 
    
        //...do stuff with the connection
    }
    

But keep on getting the following error

"Encounter exception during mapping. Error: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied ) org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied"

I know that the username and password are correct. Because the following code works

    Class.forName("oracle.jdbc.OracleDriver").newInstance(); 
    conn = DriverManager.getConnection(
    "jdbc:oracle:thin:@gpqa6.myserver.com:1526:gpqa6", "foo", "foobar");

I've even tried to enter an invalid url but still get the same error. What's going on???

Also, does Tomcat have some way of testing the data source similar to WebLogic or Glassfish?


Solution

  • Now it's working. Seem like after I establish a connection with the following code

        Class.forName("oracle.jdbc.OracleDriver").newInstance(); 
        conn = DriverManager.getConnection(
        "jdbc:oracle:thin:@gpqa6.myserver.com:1526:gpqa6", "foo", "foobar");
    

    then switching back to using the data source, it's fine. Maybe it's a caching issue?

    --Update-- It is a caching issue. Ijust have to delete the \conf\Catalina folder.

    This link really helped. http://pwu-developer.blogspot.com/2010/02/why-isnt-my-datasource-configuration.html