user2256799
user2256799

Reputation: 229

Error when loading datasource

So I recently have started working in a new proyect. These guys have the Datasource connection declared this way:

Context init = new InitialContext();
Context context = (Context) init.lookup("java:comp/env");
ds = (DataSource)context.lookup("jdbc/WhateverDS");

I don't know why have they declared it this way. I've had always seen it like this:

DataSource ds = new InitialContext().lookup("java:comp/env/jdbc/WhateverDS");

First of all do you guys know why would they do it in two steps?

My second question is that sometimes we get the following error, and if it may have something to do with the code above:

Caused by: javax.naming.NameNotFoundException: The name comp/env is not bound in this context
    at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    ... 19 more

We are using MySQL over Tomcat using jdbc.

Edit: They have nothing about the DB declared in web.xml. The context.xml (among other parameters) looks like this:

Resource driverClassName="com.mysql.jdbc.Driver"
            name="jdbc/WhateverDS"
            type="javax.sql.DataSource"
            url="jdbc:mysql://localhost:3306/someDB"

Upvotes: 0

Views: 249

Answers (1)

Christopher Schultz
Christopher Schultz

Reputation: 20882

To answer your first question: there is no effective difference between the two-step and the one-step lookup for the DataSource.

Your second question appears to be a duplicate of javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context

Upvotes: 1

Related Questions