BustedSanta
BustedSanta

Reputation: 1388

JNDI DataSource configuration in Tomcat 7

I am new to JNDI and I am trying to get my db connection working. So far no luck. I either get a message stating: "Name [java:comp/env] is not bound in this Context. Unable to find [java:comp]" or I received a time out.

Here's information about my current configuration.

Tomcat: Apache Tomcat/7.0.29

JMV: 1.7.0_06-b24

OS: Win 10 Pro

Tomcat\conf\web.xml

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myDatabaseName</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Tomcat\conf\context.xml

<ResourceLink type="javax.sql.DataSource"
name="jdbc/localRemarket"
global="jdbc/remarket"
/>

I also tried to put the resource in context.xml to make sure it's findable:

<Resource
type="javax.sql.DataSource"
name="jdbc/myDatabaseName"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDatabaseName"
username="myUsername"
password="myPassword"
maxActive="1500"
maxIdle="200"
maxwait="-1"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="2000"
minEvictableIdleTimeMillis="15000"
removeAbandoned="true"
removeAbandonedTimeout="5"
/>

Tomcat\conf\server.xml

<Resource
type="javax.sql.DataSource"
name="jdbc/myDatabaseName"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDatabaseName"
username="myUsername"
password="myPassword"
maxActive="1500"
maxIdle="200"
maxwait="-1"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="2000"
minEvictableIdleTimeMillis="15000"
removeAbandoned="true"
removeAbandonedTimeout="5"
/>

java code:

Connection conn;

public void openMyConnection() {

try {

Properties props = new Properties();
props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");

InitialContext ctx = new InitialContext(props);
Context envCtx = (Context) ctx.lookup("java:comp/env"); // <<<<< PRB HERE
// error message : Name [java:comp/env] is not bound in this Context. Unable to find [java:comp]

org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) envCtx.lookup("jdbc/localDB");

conn = ds.getConnection();

} catch (Exception e) {
System.out.println(e.getMessage());
}

}

if I change

props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");

for

props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

I get :

Receive timed out

I have reviewed many posts related to JNDI including the following two that were the most helpful:

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html and https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-datasource-jndi-example/

Please note that I read the How to configure jndi DataSource in Tomcat 7 but it doesn't provide a solution for my problem.

Can anyone please help resolve this issue?

Upvotes: 2

Views: 7601

Answers (1)

Stefan Bicher
Stefan Bicher

Reputation: 96

It worked for me, when I configured the datasource directly in the webapp (file META-INF/context.xml):

<Context >
<Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="scott"
            password="tiger"
            driverClassName="oracle.jdbc.OracleDriver"
            url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
            maxActive="8"
            maxIdle="4"/>
 </Context>

Upvotes: 1

Related Questions