JPA - When to use getTransaction() when persisting objects

后端 未结 4 1756
长情又很酷
长情又很酷 2020-12-24 12:07

I\'ve recently started working with JPA on the Google App Engine. In reading some examples, I\'ve noticed a couple of variations in the way objects are persisted. In one cas

4条回答
  •  孤城傲影
    2020-12-24 12:59

    If you use container managed EntityManager then you're using JTA transactions. Hence, you don't need to (more precisely - you can not) interfere with EntityManager's transactions fetched using entityManager.getTransaction(). The JTA starts and commits your transaction.

    If you use application managed EntityManager and you don't want to be in part of JTA transaction, then you need to manage them for yourself (it's called a resource-local entity manager).

    Most typically, application managed EntityManager which works with EntityManager.getTransaction() is used in Java SE environment.

    EDIT: You might be interested in secion 7.5 Controlling Transactions from the JPA 2.0 specification.

提交回复
热议问题