问题描述
在server.xml中,我定义了全局资源(我使用 Tomcat 6 ):
< GlobalNamingResources>
< Resource name =jdbc / mydsauth =Container
type =javax.sql.DataSource
maxActive =10maxIdle =3maxWait =10000
username =sapassword =
driverClassName =org.h2.Driver
url =jdbc:h2:〜/ .myds / data / db
/>
< / GlobalNamingResources>
我在catalina.out中看到这是绑定的,所以我认为没关系。
在我的web应用程序中,我有链接到数据源的链接,但我不确定它是否正常:
<语境GT;
< ResourceLink global ='jdbc / myds'name ='jdbc / myds'type =javax.sql.Datasource/>
< / Context>
,并且在应用程序中有persistence.xml:
< persistence xmlns =http://java.sun.com/xml/ns/persistence
xmlns:xsi =http://www.w3 .org / 2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0 .xsd
version =2.0>
< persistence-unit name =oamtransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<非-jta-data-source> jdbc / myds< / non-jta-data-source>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.H2Dialect/>
< / properties>
< / persistence-unit>
< /余辉>
它应该没问题,但最可能的是这个或ResourceLink定义是错误的,因为我得到:
$ b
What's wrong and why this does not work?
UPDATE:
I've tried to get the datasource directly:
public class WebAppListener implements ServletContextListener
{
// ServletContextListener interface - start
public void contextInitialized(ServletContextEvent sce)
{
try
{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/myds");
}
catch (NamingException ex)
{
System.out.println("!!!! Got NamingException:");
ex.printStackTrace(System.out);
}
}
public void contextDestroyed(ServletContextEvent sce) { }
}
my web.xml:
<listener>
<display-name>Listener</display-name>
<listener-class>WebAppListener</listener-class>
</listener>
Still getting the same error although I see the datasource in JMX console when I connect to the Tomcat(Catalina - Datasource - javax.sql.Datasource = "jdbc/myds" :ObjectName = Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/myds". )
The <non-jta-data-source>
in persistence.xml
should be
java:comp/env/jdbc/myds
as per the response in http://forums.oracle.com/forums/thread.jspa?messageID=1899677
And also is your db driver in $CATALINA_HOME/lib
这篇关于在persistence.xml中引用Tomcat JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!