2011-06-14 41 views
9

從Eclipse運行它時,我無法使我的DataSource與JNDI和Tomcat 6一起工作。我添加了一個context.xml中我的/ META-INF具有以下內容:帶有Tomcat 6和Eclipse的JNDI數據源

<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) 

我不明白爲什麼這不起作用......任何想法?

回答

6

我改變後,現在它的工作原理:

在我的context.xml中完成Context標籤:

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

而且在連接URL中的字符&引起Cannot create resource錯誤,不知道爲什麼,所以我現在的網址是這樣的:

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

請注意&amp; INT網址...

4

如果我沒記錯的話,你應該訪問它

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

謝謝丹尼,我想我更近了一步,現在我得到:javax.naming.NamingException:無法創建資源實例 – 2011-06-14 15:07:41

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> 
+0

好吧!我已經做到了,但是現在我得到: javax.naming.NamingException:無法創建資源實例 – 2011-06-14 14:59:04

+0

您能在Tomcat日誌中看到什麼嗎?你有lib中的MySQL驅動程序嗎? – abalogh 2011-06-14 19:29:39

+0

在日誌中沒有什麼不尋常的事情,並且庫JAR在tomcat/lib中... – 2011-06-15 03:55:56

相關問題