tomee - how to use RESOURCE_LOCAL datasource

吃可爱长大的小学妹 提交于 2019-12-13 04:28:47

问题


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 has this:

<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 looks like:

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

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)

回答1:


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>    



回答2:


how do you get your persistence unit? Manually?

If using injection:

@PersistenceUnit EntityManagerFactory emf;
@PersistenceContect EntityManager em;

TomEE resolves the datasource for you from its short name (id in tomee.xml) otherwise you need to give it the full JNDI name which I think is java:openejb/Resource/MYDS



来源:https://stackoverflow.com/questions/38207088/tomee-how-to-use-resource-local-datasource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!