Cannot use an EntityTransaction while using JTA

大兔子大兔子 提交于 2019-11-28 11:58:28

As the error states, if you are using JTA for transactions, you need to use JTA.

Either use JTA UserTransaction to begin/commit the transaction, or use a RESOURCE_LOCAL persistence unit and non-jta DataSource.

See, http://en.wikibooks.org/wiki/Java_Persistence/Transactions

You are not supposed to use em.getTransaction().begin(); nor em.getTransaction().commit();, these instructions are to be used with RESOURCE_LOCAL transaction type.

In your case the transaction is managed by the container, in the first use of the EntitiyManager in your method, the container checks whether there is an active transaction or not, if there is no transaction active then it creates one, and when the method call ends, the transaction is committed by the container. So, at the end your method should look like this:

public void save(User user) {
    em.persist(user);
}

The container takes care of the transaction, that is JTA.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!