Reputation: 1809
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?
Upvotes: 1
Views: 5819
Reputation: 1809
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
For more information refer to this other post: JTA & MySQL
Upvotes: 1
Reputation: 33946
Remove transaction-type="RESOURCE_LOCAL"
from your persistence.xml.
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.
Upvotes: 1
Reputation: 2146
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
Upvotes: 1