2016-07-05 94 views
0

我有一些可以使用JTA的類(ejb,webservices,mdb等)。對於某些課程,我需要RESOURCE_LOCAL(不能注入)。但是我不能得到tomee引用RESOURCE_LOCAL的jndi名稱。你如何設置tomee和RESOURCE_LOCAL?我似乎無法在網上找到一個好例子,我不想在我的persistence.xml文件中輸入任何用戶名和密碼。tomee - 如何使用RESOURCE_LOCAL數據源

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$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) 
+0

什麼版本的TomEE是這樣的? –

+0

7.0.1和7.0.1(最新2個版本) – user18896654353

回答

2

的解決方案似乎這是(仍在驗證): (不是很intuative或文件,增加OpenEJB的:資源來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>  
+0

謝謝!這節省了我的一天。 – npocmaka

0

你如何讓你的持久單元?手動?

如果使用注射:

@PersistenceUnit EntityManagerFactory emf; 
@PersistenceContect EntityManager em; 

TomEE解決了數據源爲你從它的短名稱(在tomee.xml ID),否則你需要給它充分的JNDI名稱,我認爲是用java:OpenEJB的/資源/ MYDS

+0

是的,我正在注入EnityManager,這個問題似乎是JTA與RESOURCE_LOCAL(從我上面的答案)不同的jndi命名方案。我認爲這是新的或錯誤,因爲老版本的tomee我記得使用相同的jndi類型字符串 – user18896654353

+0

JTA應該由tomee提供,非JTA可能由hibernate根據版本和配置查找,即使tomee仍然提供給提供者(hibernate忽略它,因爲它調用JndiServiceImpl)。 –

相關問題