0

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)
Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45

2 Answers2

2

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

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

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13
  • yes I am injecting the EnityManager, the issue seems to be different jndi naming schemes for JTA vs RESOURCE_LOCAL (from my answer above). I think this is new or a bug since old versions of tomee I remember using the same jndi type string –  Jul 22 '16 at 20:16
  • The JTA one should be provided by tomee where the non JTA one can likely be looked-up by hibernate depending the version and config even if tomee still provides it to the provider (hibernate ignores it it seems since it calls JndiServiceImpl). – Romain Manni-Bucau Jul 24 '16 at 13:44