【问题标题】:Transaction didnt rollback but committed using Hibernate JPA 2.0 and EJB3 with BMT事务没有回滚,而是使用 Hibernate JPA 2.0 和 EJB3 和 BMT 提交
【发布时间】:2011-06-28 10:15:02
【问题描述】:

我一直在研究 JPA 2 与 ejb3 上的休眠。 所以我做了一个示例类来测试功能。我曾尝试使用 BMT 交易,但遇到交易问题。 从下面的示例代码中,如果dosomething() 出现问题,将引发异常,因此将回滚 UserTransaction。 但是,我发现即使抛出异常,编辑的实体也会更新到数据库中。如果我在设置中遗漏了什么,谁能指出我?

@Stateless(mappedName = "MyManagementBean")
    @Local
    @TransactionManagement(TransactionManagementType.BEAN)


    public class MyManagement implements MyManagementLocal,MyManagementRemote {

        @PersistenceUnit(unitName="MyEjb") EntityManagerFactory emf;
        @Resource UserTransaction utx;
        @Resource SessionContext ctx;

        /**
         * Default constructor. 
         */
        public MyManagement () {
            // TODO Auto-generated constructor stub
        }

        public void dosomething(String id) throws Exception
        {

            try {
                utx.begin();    
                em = emf.createEntityManager();

                Myline line = em.find(Myline.class, id);

                line.setStatus("R");

                em.flush();
                utx.commit();
            }
            catch (Exception e) {
                e.printStackTrace();
                if (utx != null) utx.rollback();
                throw e; // or display error message
            }
            finally {
                em.close();
            }       
        } 

【问题讨论】:

    标签: hibernate transactions ejb-3.0 jpa-2.0


    【解决方案1】:

    异常的类型是什么?程序是否曾经调用过

    utx.rollback();
    

    试试

    e.printStackTrace();
    if (utx != null) {
        utx.rollback();
        system.error.println("Rolled Back");
    }                
    throw e; // or display error message
    

    【讨论】:

    • 也不例外。整段代码似乎运行良好。除了在执行 JPA 查询之前,它会自动刷新并将先前的更改提交到 DB。
    • 对不起,我误解了这个问题,“但是,我发现即使抛出异常,编辑的实体也会更新到数据库中。如果我在设置中遗漏了什么,谁能指出我? "
    • stackoverflow.com/questions/6516973/… - 更详细的相关问题
    猜你喜欢
    • 2012-12-24
    • 2013-11-22
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2011-01-22
    • 2016-03-07
    • 1970-01-01
    • 2015-05-31
    相关资源
    最近更新 更多