如何在JPA中回滚一系列持久化语句? [英] How to rollback a series of persist statements in JPA?

查看:149
本文介绍了如何在JPA中回滚一系列持久化语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况需要回滚一系列持久化方法。
我的控制器类中有一个方法,我从那里调用persist方法。

I have a situation where i a need to rollback a series of persist method. I have a method in my controller class, from where I am calling the persist method.

控制器类:

@EJB
private jpa.session.ClassMasterFacade ejbFacadeCM;
@EJB
private jpa.session.StudentMasterFacade ejbFacadeSM;
@EJB
private jpa.session.ParentsMasterFacade ejbFacadePM;
@EJB
private jpa.session.AddressMasterFacade ejbFacadeAM;

public String confirmData() {
    try {
        ejbFacadeSM;.create(selectedSM);
        ejbFacadeCM;.create(selectedCM)
        ejbFacadeAM;.create(selectedAM);
        ejbFacadePM;.create(selectedPM);
    } catch (Exception e) {
        //rollback all
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle ("/resources/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
 }

示例门面类:

@Stateless
public class ClassMasterFacade extends AbstractFacade<ClassMaster> {

    @PersistenceContext(unitName = "sdjv_smsPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ClassMasterFacade() {
        super(ClassMaster.class);
    }

}

所有其他门面类与以上课程。抽象类是:

All other facade classes are similar to the above class. The abstract class is:

public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }
}

如果有任何异常,我想回滚所有的create方法找到了。

I want to rollback all the create method if any exception is found.

我搜索并找到了一些答案:

I searched and found some answers:

  • Roll back transaction after exception in JPA + Spring
  • How to prevent JPA from rolling back transaction?

我是JPA新手,刚开始探索它。我错过了一些概念吗?

I am new to JPA and just started exploring it. Am I missing some concept?

直到有问题,不知道它是否相关。在所有实体类中,我有一个自动生成的密钥更新。问题是如果在第二个create语句中发生异常,则会生成第一个create语句的autogenerated键,但仍然没有在mysql中更新为excepted ..但是如果成功所有语句,则键序列被破坏。

there is till a problem, dont know if it is related. In all the entity class, i have a autogenerated key as updated. the problem is that if the exception occurs in second create statement, the autogenerated key for the first create statement is generated but still not updated in mysql as excepted..But in case of success all statement, the key sequence is broken.

实体类:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "class_group_id")
private Integer classGroupId;


推荐答案

使用 javax.transaction .UserTransaction ,可在任何标准Java EE环境中使用。

Use a javax.transaction.UserTransaction, which is available to you in any standard Java EE environment.

用法相当简单,注入并使用以下方法:

Usage is fairly straightforward, inject it and use the following methods:


  • begin()开始交易

  • commit()如果所有操作成功并且您想提交结果

  • rollback()如果发生错误并且您想要回滚到 begin()被调用的时间

  • begin() to start a transaction
  • commit() if all operations were successful and you want to commit the results
  • rollback() if an error occurred and you want to roll back to the point in time when begin() was called

请参阅:关于UserTransaction的Oracle文档

@Resource
UserTransaction tran;

...

public void confirmData (){

    tran.begin();
    try {
        ejbFacadeSM.create(selectedSM);
        ejbFacadeCM.create(selectedCM)
        ejbFacadeAM.create(selectedAM);
        ejbFacadePM.create(selectedPM);

        // Create's succeeded, commit transaction.
        tran.commit();
    } catch (Exception e) {
        // Error occurred, rollback transaction
        tran.rollback();
    }
}

这篇关于如何在JPA中回滚一系列持久化语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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