1
public void lagreMelding(String bruker, String msg) {
        EntityManager em = emf.createEntityManager();
        Melding melding = new Melding(bruker, msg);
    try {
        em.getTransaction().begin();
        em.persist(melding);
        em.getTransaction().commit();
        em.close();
    } catch (RollbackException e) {
        em.getTransaction().rollback();
    }
}

The error report is the following:

java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
    org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:73)
    org.eclipse.persistence.internal.jpa.EntityManagerImpl.getTransaction(EntityManagerImpl.java:1311)
    no.hib.dat104.oblig1.Kontroll.lagreMelding(Kontroll.java:37)
    no.hib.dat104.oblig1.MeldingServlet.doPost(MeldingServlet.java:80)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

All the other examples of this problem had been solved by removing

em.getTransaction().begin();

and

em.getTransaction().commit();

But upon doing this the database doesn't update. Anyone care to explain what I am missing? Pre-Thanks for taking your time for a simple question :)

npocmaka
  • 55,367
  • 18
  • 148
  • 187
Patidati
  • 1,048
  • 2
  • 12
  • 19
  • http://stackoverflow.com/questions/10915855/cannot-use-an-entitytransaction-while-using-jta – RahulArackal Sep 19 '14 at 11:46
  • Had the same problem - this worked for me - http://stackoverflow.com/a/38209862/388389 - adding *non-jta-data-source* right after provider definition – npocmaka Jan 03 '17 at 14:27

2 Answers2

0

You can't begin and commit the transaction this way when using JTA, try something like this:

public void lagreMelding(String bruker, String msg) {
        EntityManager em = emf.createEntityManager();
        Melding melding = new Melding(bruker, msg);

        UserTransaction transaction = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");

    try {
        transaction.begin();
        em.persist(melding);
        transaction.commit();
        em.close();
    } catch (RollbackException e) {
        transaction.rollback();
    }
}
Leonardo Jines
  • 360
  • 5
  • 17
0

From the stacktrace I assume you are trying to persist entity with JPA in Servlet method from some web app. You are also using JTA transaction management. With this type of transaction management you cannot operate on EntityTransaction obtained from EntityManager as you would with resource local transaction management. You have to allow the container to provide you a UserTransaction:

@Resource
UserTransaction tx;

And after that operate on it with begin(), commit() and rollback().

try {
    tx.begin();
    em.persist(melding);
    tx.commit();
} catch (Exception e) {
    if (status.STATUS_MARKED_ROLLBACK == tx.getStatus()) {
      try {
       tx.rollback();
      } catch (SystemException e1) {
        //log error
      }
    }
} finally () {
    em.close();
}

As you already noticed it is quite a hassle to manually maintain transactions. That's why, if possible, I use EJB beans with it's automatic transaction management.

zbig
  • 3,830
  • 2
  • 29
  • 37