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?
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&characterEncoding=utf-8
Please note the &
int the URL...
If I remember correctly you should access it as
<property name="jndiName" value="java:comp/env/jdbc/myDB"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With