2

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?

duvo
  • 1,634
  • 2
  • 18
  • 30
  • Apparently you don't have a user `foo` with the password `foobar` –  May 01 '12 at 22:01
  • That's just as an example. I've verified that username and password are correct. – duvo May 01 '12 at 22:08
  • 1
    Apperently not, otherwise you wouldn't get the error message –  May 01 '12 at 22:12
  • 1
    This code works
    Class.forName("oracle.jdbc.OracleDriver").newInstance(); conn = DriverManager.getConnection("jdbc:oracle:thin:@gpqa6.myserver.com:1526:gpqa6", "foo", "foobar"); But when using the same configuration for datasource, it doesn't
    – duvo May 01 '12 at 22:57
  • I have a similar setup here but it's working fine. the only difference being driverClassName="oracle.jdbc.driver.OracleDriver" as I use a driver for oracle11 + the fact that the datasource is injected in my objects via spring. check your jdbc driver to make sure you have only 1 of them and not multiple versions (for example 1 in the common-lib/ of tomcat and another one in the web-inf/lib/ of your application) as this creates some nasty bugs. – Farid May 02 '12 at 13:25
  • possible duplicate of [Oracle JDBC : invalid username/password (ora-01017)](http://stackoverflow.com/questions/8435234/oracle-jdbc-invalid-username-password-ora-01017) – brandizzi Apr 24 '14 at 22:13

1 Answers1

4

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

duvo
  • 1,634
  • 2
  • 18
  • 30