Cannot inject RESOURCE_LOCAL container managed EntityManager using @PersistenceContext
Asked Answered
L

1

13

I am using JBoss AS 7.1.1 and able to configure a new JTA datasource and wire it to my EJB using

@PersistenceContext(unitName="TestPU")
private EntityManager entityManager;

When I tried to use RESOURCE_LOCAL PersistenceUnit I am getting the error saying I can't inject RESOURCE_LOCAL PU using @PersistenceContext.

I have configured my persistence.xml as follows:

<persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>   
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/xy"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="blah"/>        
        <property name="hibernate.hbm2ddl.auto" value="update" />       
      </properties> 
</persistence-unit>

And in my DAO,

@Stateless
public class UserDAO {
    @PersistenceContext(unitName="TestPU")
    private EntityManager entityManager;


}

When I deployed my app on AS 7.1.1 I am getting the following error.

JBAS011428: Cannot inject RESOURCE_LOCAL container managed EntityManagers using @PersistenceContext
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor$1.handle(ModuleJndiBindingProcessor.java:169)
    at org.jboss.as.ee.component.ClassDescriptionTraversal.run(ClassDescriptionTraversal.java:54)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.processClassConfigurations(ModuleJndiBindingProcessor.java:162)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.deploy(ModuleJndiBindingProcessor.java:155)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    ... 5 more

Any solution to use RESOURCE_LOCAL PU with @PersistenceContext?

Lectureship answered 12/4, 2012 at 5:8 Comment(0)
F
26

JTA : In Java EE environment, transactions are managed by the container & by default its JTA transaction. You can get entity manager by lookup or injection.

RESOURCE_LOCAL : In Java SE, application have to manage transactions explicitly & resource local transactions are native transactions. You have to create EntityManagerFactory & then can create entity manager from it.

As you are deploying it in application server, change the transaction-type to JTA in persistence.xml.

Fatten answered 12/4, 2012 at 5:48 Comment(5)
Thanks for you explanation. So there is no way to configure the JTA datasource as part of WAR/EAR? We can do it with Annotations but i am looking for xml based configuration.Lectureship
@sivaprasadreddy.k You're welcome. If you have already configured data-source, then you can mention it in persistence.xml with <jta-data-source>.Fatten
@NayanWadekar can you please share us an example with RESOURCE_LOCAL (persistence.xml) and EntityManagerFactory (java code).Midweek
@Saba The required xml code is already posted in question while java code would be - Persistence.createEntityManagerFactory(persistenceUnitName).Fatten
thanks @NayanWadekar thought so. I changed it accordingly. Now it works fine.Midweek

© 2022 - 2024 — McMap. All rights reserved.