0

我有一些可以使用 JTA 的类(ejb、webservices、mdb 等)。对于我需要的某些类RESOURCE_LOCAL(不能注入)。但是我无法让 tomee 引用RESOURCE_LOCAL. 你如何设置 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>

MYDSEntityManagerFactory查找中使用名称,但收到此错误:

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)
4

2 回答 2

2

解决方案似乎是这样的(仍在验证):(不是很直观或记录在案,将 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>    
于 2016-07-05T18:05:36.070 回答
0

你如何得到你的持久性单元?手动?

如果使用注入:

@PersistenceUnit EntityManagerFactory emf;
@PersistenceContect EntityManager em;

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

于 2016-07-21T18:33:51.440 回答