JPA和EJB-什么时候需要使用事务? [英] JPA and EJB - When do I need to use transaction?

查看:81
本文介绍了JPA和EJB-什么时候需要使用事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照一些教程学习Java的持久性.

I'm learning persistence in Java following some tutorial.

我正在使用Java EE 7和Payara服务器.

I'm using Java EE 7 and Payara server.

我注意到每个人都使用不同的持久性方法.

I noticed that each uses a different method for persistence.

示例:

  • 简单

@Stateless
public class BookServiceBean implements BookService {
    @PersistenceContext
    private EntityManager em;

    public void createOrUpdate(Book book) {
        em.persist(book);
    }
    public void remove(Book book) {
        em.remove(book);
    }
}

  • flush()一起使用,当未在 persistene.xml 中的"AUTO"上设置验证策略时使用吗,对吗?

  • with flush(), this is used when validation strategy isn't set on "AUTO" in persistene.xml, right?

    @Stateless
    public class BookServiceBean implements BookService {
        @PersistenceContext
        private EntityManager em;
    
        public void createOrUpdate(Book book) {
            em.persist(book);
            em.flush();
        }
        public void remove(Book book) {
            em.remove(book);
            em.flush();
        }
    }
    

  • 有交易

  • with transaction

    @Stateless
    public class BookServiceBean implements BookService {
        @PersistenceContext
        private EntityManager em;
    
        public void createOrUpdate(Book book) {
            utx.begin();
            em.persist(book);
            utx.commit();
        }
        public void remove(Book book) {
            utx.begin();
            em.remove(book);
            utx.commit();
        }
    }
    

  • 何时以及为什么我必须使用最后一个?

    When and why do I have to use the last one?

    每个方法的末尾是否需要使用 em.close()?

    Is it necessary to use em.close() at the end of each method?

    有哪些好的做法?

    推荐答案

    第一个方法是没有所有手动刷新和事务模糊处理的EJB方法,是一种规范方法.默认情况下,单个EJB方法调用已计为单个完整事务.EJB容器将在调用方法之前透明地开始事务,并在方法返回时提交事务(或从方法抛出应用程序异常时回滚).

    The first one, an EJB method without all that manual flush and transaction fuzz, is the canonical approach. A single EJB method call counts by default already as a single full transaction. The EJB container will transparently begin the transaction before the method is invoked and commit the transaction when the method returns (or rollback when an application exception is thrown from the method).

    第二个示例中的手动冲洗是不必要的.通常,仅在修改实体(即使其成为脏")时才要使用 em.flush(),然后要(间接)执行 SELECT 在同一笔交易中.这种情况很少见,但是有很多实际的用例,通常是当您想 SELECT 一个肮脏的实体是其子实体的父对象时.

    The manual flush in second example is unnecessary. Generally, you want to use em.flush() only when you're modifying an entity (i.e. making it "dirty") and want afterwards (indirectly) perform a SELECT on it within the very same transaction. It's rare, but there are real world use cases for it, generally when you'd like to SELECT a parent whose the dirty entity is a child of.

    完全不需要第三示例中的手动交易管理.阅读第一段之后,您应该已经意识到这一点.仅当您不使用 JTA 而是使用 RESOURCE_LOCAL (通常在Java SE中不是Java EE)时,才需要进行手动事务管理.

    The manual transaction management in third example is totally unnecessary. You should already realize that after having read the first paragraph. Manual transaction management is only necessary when you aren't using JTA, but RESOURCE_LOCAL (usually, in Java SE not Java EE).

    这篇关于JPA和EJB-什么时候需要使用事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆