从Eclipse运行时,我无法让我的DataSource使用JNDI和Tomcat 6。我已经使用以下内容向我的/ META-INF添加了一个context.xml:
<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>
并按如下方式配置我的Spring Bean:
<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>
我还将这些行添加到我的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>
但由于某种原因,我仍然会收到此错误:
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)
我无法理解为什么这不起作用......有什么想法吗?
答案 0 :(得分:6)
我改变了以下内容,现在可行了:
在我的context.xml中使用:
完成了Context
标记
<Context docBase="myApp" path="/myApp" reloadable="true" source="org.eclipse.jst.jee.server:app">
在连接网址中,字符&
导致Cannot create resource
错误,不知道原因,所以我的网址现在就像:
jdbc:mysql://localhost/database?useUnicode=true&characterEncoding=utf-8
请注意&
int的URL ...
答案 1 :(得分:4)
如果我没记错的话你应该以
的形式访问它<property name="jndiName" value="java:comp/env/jdbc/myDB"/>
答案 2 :(得分:1)
在Spring appcontext中,将您的定义替换为:
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="java:comp/env/jdbc/myDB"/>
<property name="resourceRef"
value="true" />
</bean>