tomee - 如何使用 RESOURCE_LOCAL 数据源

tomee - how to use RESOURCE_LOCAL datasource

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

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,但出现此错误:

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.perform(EntityManagerFactoryBuilderImpl.java:852)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.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 中删除它不起作用)

<?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>    

您如何获得持久性单元?手动?

如果使用注入:

@PersistenceUnit EntityManagerFactory emf;
@PersistenceContect EntityManager em;

TomEE 从其短名称(tomee.xml 中的 id)为您解析数据源,否则您需要为其提供完整的 JNDI 名称,我认为是 java:openejb/Resource/MYDS