1

I am developing an web application and I have to use JTA which I never used. I started using EntityManager but it seems not to work here. When I use EntityManager I get this message:

Only persistence units with transaction type JTA can be used as a container managed entity manager.

To cut it short, I have this piece of code:

@PersistenceContext(unitName = "zJSF2PU")
private EntityManager em;
em.getTransaction().begin();
//some code
em.getTransaction().commit();

How can I do this without EntityManager?

3
  • You still use entity managers even for CMT. What version of JPA are you using?
    – Perception
    Commented Mar 16, 2013 at 19:53
  • eclipse-link, but I used Hibernate also, and it was the same thing Commented Mar 16, 2013 at 20:25
  • No, not the provider. Are you using JPA 1 or 2?
    – Perception
    Commented Mar 16, 2013 at 20:25

3 Answers 3

1

In your ejb project META-INF/persistence.xml you must have something like:

<?xml version="1.0" encoding="UTF-8"?>
<persistence>
    <persistence-unit name="myPersistenceUnitNamePersonalised" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/MySQL</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
        <property name="eclipselink.logging.level" value="FINE" />
        </properties>
    </persistence-unit>
</persistence>

And you must declare this in your Application Server (jboss, tomcat, glassfish)

You need to search how to add a data-source and persistence unit in your Application Server...

And that's it... they comunicate thru jndi

3
  • yes I have this, but this does not help me with my code that I posted here Commented Mar 16, 2013 at 20:31
  • or better how can it help? Commented Mar 16, 2013 at 20:31
  • well.. I defined a JTA transaction... I just told you how to add a persistence unit like zJSF2PU but JTA :)
    – Alex
    Commented Mar 17, 2013 at 8:03
1
  1. Remove transaction-type="RESOURCE_LOCAL" from your persistence.xml.

  2. Remove calls to em.getTransaction(). Inject javax.transaction.UserTransaction (JTA) and use its begin/commit/rollback methods. Alternatively, inject the EM into a stateless EJB instead, and allow the EJB container to automatically manage the transaction.

3
  • All what you said I think is true, but I need to know how to do that, that is why I submitted a piece of code so if you know how to do this, you may help me with the updated code? Thank you for your answer. Commented Mar 16, 2013 at 20:29
  • On the other hand, I tried with stateless EJB but it is the same thing Commented Mar 16, 2013 at 20:50
  • Sorry, it seemed to be a misunderstanding at the conceptual level rather than the code level. Glad you figured it out.
    – Brett Kail
    Commented Mar 17, 2013 at 15:42
1

I finally was able to fix my problem. From my searches it resulted that you can not use EntityManager when you are using JTA within ManagedBeans for example. However it can be used in a stateless bean and then we can inject this Stateless Bean to the ManagedBean and use its methods. The procedure is as follows:

  • create an EJB (a simple class with @Stateless annotation)

  • move the method that uses EntityManager to the EJB

  • inject the EJB into your managed bean (using @EJB annotation) and call the relevant method

For more information refer to this other post: JTA & MySQL

Not the answer you're looking for? Browse other questions tagged or ask your own question.