在persistence.xml中引用Tomcat JNDI数据源

编程入门 行业动态 更新时间:2024-10-21 14:42:30
本文介绍了在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 =java.sun/xml/ns/persistence xmlns:xsi =http://www.w3 / 2001 / XMLSchema-instance xsi:schemaLocation =java.sun/xml/ns/persistence java.sun/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> forums.oracle/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="java.sun/xml/ns/persistence" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/persistence java.sun/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 forums.oracle/forums/thread.jspa?messageID=1899677

And also is your db driver in $CATALINA_HOME/lib

更多推荐

在persistence.xml中引用Tomcat JNDI数据源

本文发布于:2023-10-25 14:03:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   xml   persistence   JNDI   Tomcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!