Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNDI DataSource with Tomcat 6 and Eclipse

I can't get my DataSource working with JNDI and Tomcat 6, while running it from Eclipse. I've added a context.xml to my /META-INF with the following content:

<Context>

<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"
     username="root"
     password="root"
     driverClassName="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/database"
     maxActive="15"
     maxIdle="7"
     validationQuery="Select 1" />
</Context>

And configured my Spring Bean as follows:

<bean id="UserDatabase" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/myDB"></property>
    <property name="lookupOnStartup" value="true"></property>
    <property name="cache" value="true"></property>
    <property name="proxyInterface" value="javax.sql.DataSource"></property>
</bean>

I've also added this lines to my web.xml:

<resource-ref>
    <description>Connection Pool</description>
    <res-ref-name>jdbc/myDB</res-ref-name>
    <res-type>javax.sql.Datasource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

But for some reason I still get this error:

javax.naming.NameNotFoundException: The name jdbc is not associated to this context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)

I can't understand why this is not working... Any idea?

like image 695
Joaquín L. Robles Avatar asked Jun 14 '11 14:06

Joaquín L. Robles


2 Answers

I changed the following and now it works:

In my context.xml completed the Context tag with:

<Context docBase="myApp" path="/myApp" reloadable="true" source="org.eclipse.jst.jee.server:app">

And in the Connection URL the character & caused the Cannot create resource error, don't know why, so my URL now is like:

jdbc:mysql://localhost/database?useUnicode=true&amp;characterEncoding=utf-8

Please note the &amp; int the URL...

like image 74
Joaquín L. Robles Avatar answered Oct 09 '22 14:10

Joaquín L. Robles


If I remember correctly you should access it as

<property name="jndiName" value="java:comp/env/jdbc/myDB"/>
like image 35
danny.lesnik Avatar answered Oct 09 '22 13:10

danny.lesnik