本文介绍了tomee-如何使用RESOURCE_LOCAL数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可以使用JTA的类(ejb,webservices,mdb等).对于某些类,我需要RESOURCE_LOCAL(无法注入).但是我无法引用RESOURCE_LOCAL的jndi名称.如何设置tomee和RESOURCE_LOCAL?我似乎在网上找不到一个很好的例子,我宁愿不要在我的persistence.xml文件中放入任何用户名和密码.

I have some classes (ejb, webservices, mdb, etc..) that can use JTA. For some classes I need RESOURCE_LOCAL (can't be injected). However I can't get tomee to reference the jndi name of RESOURCE_LOCAL. How do you setup tomee and RESOURCE_LOCAL? I can't seem to find one good example online, I would prefer not to put any usernames and passwords in my persistence.xml file.

tomee.xml具有此功能:

<Resource id="MYDS" type="DataSource">
         JdbcDriver com.mysql.jdbc.Driver
         JdbcUrl jdbc:mysql://127.0.0.1:3306/maestro
         UserName myusername
         Password mypassword
         JtaManaged false
</Resource> 

persistence.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
  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">
    <persistence-unit name = "MYDS" transaction-type = "RESOURCE_LOCAL">     
        <provider>org.hibernate.ejb.HibernatePersistence</provider>        
            <non-jta-data-source>MYDS</non-jta-data-source>
    </persistence-unit>        
</persistence>

我在EntityManagerFactory查找中使用名称MYDS,但出现此错误:

I am using name MYDS in EntityManagerFactory lookup, but get this error:

Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [MYDS]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
    ... 36 more 
Caused by: javax.naming.NameNotFoundException: Name [MYDS] is not bound in this Context. Unable to find [MYDS].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:828)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)

推荐答案

解决方案似乎是这样(仍在验证中):(不是很直观或没有记录,将openejb:Resource添加到JPA和JPA不起作用,将它从RESOURCE_LOCAL和RESOURCE_LOCAL中删除是不起作用的)

the solution seems to be this (still verifying):(not very intuative or documented, adding openejb:Resource to JPA and JPA doesn't work, removing it from RESOURCE_LOCAL and RESOURCE_LOCAL doesn't work)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
  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">
  <persistence-unit name = "MYDS" transaction-type = "RESOURCE_LOCAL">     
        <provider>org.hibernate.ejb.HibernatePersistence</provider>        
            <non-jta-data-source>openejb:Resource/MYDS</non-jta-data-source>
  </persistence-unit>        
    <persistence-unit name="MYDSJPA" transaction-type = "JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>MYDS</jta-data-source>             
        </properties>
  </persistence-unit>
</persistence>    

这篇关于tomee-如何使用RESOURCE_LOCAL数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:46