2013-03-12 43 views
5

我在context.xml爲Tomcat下面的代碼錯誤:不能理解連接池使用JPA,Tomcat的,甲骨文

<Resource name="ds/OracleDS" auth="Container" type="javax.sql.DataSource" 
maxActive="1" maxIdle="2" maxWait="2" 
username="demo" password="demo" 
driverClassName="oracle.jdbc.driver.OracleDriver" 
url="jdbc:oracle:thin:@localhost:1521:orcl"/> 

我有這樣的代碼在web.xml

<resource-ref> 
<description>Oracle Datasource example</description> 
<res-ref-name>ds/OracleDS</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth> 
</resource-ref> 

我已經寫代碼persistence.xml如:

<persistence-unit name="ReceivablesPU" transaction-type="RESOURCE_LOCAL"> 
     <provider>oracle.toplink.essentials.PersistenceProvider</provider> 
     <non-jta-data-source>java:ds/OracleDS</non-jta-data-source> 

我不明白java:ds/OracleDS<Resource name="ds/OracleDS"<res-ref-name>ds/OracleDS</res-ref-name>。錯誤是:

javax.persistence.PersistenceException: Exception [TOPLINK-7060] (Oracle TopLink Essentials - 2.0 (Build b40-rc (03/21/2007))): oracle.toplink.essentials.exceptions.ValidationException 
Exception Description: Cannot acquire data source [java:ds/OracleDS]. 
Internal Exception: javax.naming.NamingException: This context must be accessed through a java: URL 

回答

0

i變化的contex.xml:

<Resource name="OracleDS" auth="Container" type="javax.sql.DataSource" 
    maxActive="1" maxIdle="2" maxWait="2" 
    username="demo" password="demo" 
    driverClassName="oracle.jdbc.driver.OracleDriver" 
    url="jdbc:oracle:thin:@localhost:1521:orcl"/> 

我在web.xml中的變化:

<resource-ref> 
    <description>Oracle Datasource example</description> 
    <res-ref-name>OracleDS</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
    </resource-ref> 

我改變了p ersistence.xml

<persistence-unit name="ReceivablesPU" transaction-type="RESOURCE_LOCAL"> 
     <provider>oracle.toplink.essentials.PersistenceProvider</provider> 
     <non-jta-data-source>java:comp/env/OracleDS</non-jta-data-source> 

然後我得到了輸出....

2

persistence.xml,你的第三個行應該是這樣的:

 <non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source> 
0

請參閱此回答一個類似(但不重複的問題):

Jetty Data Source, Hibernate, datasource not found

根據的Java EE Web應用程序容器,對於名爲「ds/OracleDS」的數據源,您可能需要在尋址JNDI上下文前加上「java:comp/env」(例如,在persistence.xml文件中)以使其工作。

在Tomcat中的你的具體情況,我會嘗試以下方法:

<Resource name="ds/OracleDS" auth="Container" type="javax.sql.DataSource" 
maxActive="1" maxIdle="2" maxWait="2" 
username="demo" password="demo" 
driverClassName="oracle.jdbc.driver.OracleDriver" 
url="jdbc:oracle:thin:@localhost:1521:orcl"/> 

你並不真的需要在你的web.xml什麼。在context.xml的上述配置將使Tomcat的公開數據源作爲一個JNDI資源

最後,在persistence.xml中放:

<persistence-unit name="ReceivablesPU" transaction-type="RESOURCE_LOCAL"> 
     <provider>oracle.toplink.essentials.PersistenceProvider</provider> 
     <non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source>