What is difference between @Resource UserTransaction and EntityManager.getTransaction()

前端 未结 3 1209
滥情空心
滥情空心 2020-12-13 06:31

Can anybody explain what is difference between :

@Resource
UserTransaction objUserTransaction;

and

EntityManager.getTransac         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 07:01

    In addition to @Marco's answer which does well to tell the difference between the JTA and resource local transactions.

    Container Managed Transactions are [as it is named] managed by the container rather than your application. This is done through the EJB tier where you just need to write your method and the container will wrap the method around a transaction context so if any part of your method or its lower level calls throws an exception, the transaction will rollback.

    It can also be fine tuned using annotations. More info can be found here https://docs.oracle.com/javaee/5/tutorial/doc/bncij.html

    Note that this is only done through EJBs, and entity managers that are injected on the web tier (e.g. servlets or REST API) do not get managed by the container in which case you have to look up the transaction using @Resource UserTransaction or EntityManager.getTransaction, begin() and commit() yourself.

    From Java EE 6 you are allowed to have EJBs inside the web tier so you don't need to have an overly complex project layout unless you start wanting to expose your EJBs as web services.

提交回复
热议问题