开发者

JNDI DataSource with Tomcat 6 and Eclipse

开发者 https://www.devze.com 2023-03-13 08:42 出处:网络
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:

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&amp;characterEncoding=utf-8

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


If I remember correctly you should access it as

<property name="jndiName" value="java:comp/env/jdbc/myDB"/>


In the Spring appcontext, replace your definition with:

<bean id="dataSource" 
      class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" 
                  value="java:comp/env/jdbc/myDB"/>    
      <property name="resourceRef" 
                  value="true" /> 
</bean>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号