在persistence.xml中引用Tomcat JNDI数据源 [英] referring Tomcat JNDI datasource in persistence.xml

查看:163
本文介绍了在persistence.xml中引用Tomcat JNDI数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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


javax.naming.NameNotFoundException:
名称jdbc未在此上下文中绑定


blockquote>

有什么不对?为什么这不起作用?

更新:



我试图直接获取数据源:

  public class WebAppListener实现ServletContextListener 
{
// ServletContextListener接口 - 开始
public void contextInitialized (ServletContextEvent sce)
{
try
{
上下文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){}
$ b}

my web.xml:

 < listener> 
< display-name>监听器< / display-name>
< listener-class> WebAppListener< / listener-class>
< / listener>

当我连接到Tomcat $ b时,虽然在JMX控制台中看到数据源,但仍然出现相同的错误$ b(Catalina - Datasource - javax.sql.Datasource =jdbc / myds:
ObjectName = Catalina:type = DataSource,class = javax.sql.DataSource,name =jdbc / myds。) p>

解决方案

< non-jta-data-source> c $ c> persistence.xml 应该是

java:comp / env / jdbc / myds

rel = noreferrer> http://forums.oracle.com/forums/thread.jspa?messageID=1899677



同样也是你的db driver在 $ CATALINA_HOME / lib


in server.xml I've defined global resource (I'm using Tomcat 6):

<GlobalNamingResources>
   <Resource name="jdbc/myds" auth="Container"
             type="javax.sql.DataSource"
             maxActive="10" maxIdle="3" maxWait="10000"
             username="sa"  password=""
             driverClassName="org.h2.Driver"
             url="jdbc:h2:~/.myds/data/db"
   />
</GlobalNamingResources>

I see in catalina.out that this is bound, so I suppose it's OK.

In my web app I have the link to the datasource, I'm not sure it's OK:

<Context>    
 <ResourceLink global='jdbc/myds' name='jdbc/myds' type="javax.sql.Datasource"/>    
</Context>

and in application there is 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="oam" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>jdbc/myds</non-jta-data-source>
    <!-- class definitions here, nothing else -->

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

It should be OK, but most probably this or the ResourceLink definition is wrong because I'm getting:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

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数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆