简体   繁体   English

Hibernate Jpa批处理插入不起作用

[英]Hibernate Jpa batch insert not working

I am using hibernate jpa to perform a batch update . 我正在使用hibernate jpa执行批处理更新。

Its not giving any error or exceptions but the transaction roll backs everytime giving. 它没有给出任何错误或异常,但是事务在每次给出时都会回滚。

2015-04-21 16:30:33,548 ERROR [org.jboss.as.ejb3] (Thread-344 (HornetQ-client-global-threads-462057890)) javax.ejb.EJBTransactionRolledbackException: no transaction is in progress

This is my batch update code 这是我的批量更新代码

getEm().getTransaction().begin();
    System.out.println("transaction started--------------");
    try{    
    for(Receipt ReceiptEntity : arrReceiptEntity){


        getEm().persist(ReceiptEntity);


    }
    getEm().getTransaction().commit();
    System.out.println("commited");
    }catch(Exception exception){
        System.out.println("error----------------------------------------------------------------------");
        if(getEm().getTransaction().isActive())
            getEm().getTransaction().rollback();
            LOG.error(exception);
    }finally
    {
        getEm().flush();
        getEm().clear();
        getEm().close();
    }

I am using Postgres server 9.4.1 Please help me figure out what i am doing wrong. 我正在使用Postgres服务器9.4.1,请帮助我弄清楚我在做什么错。 I have checked other similar posts but nothing helped. 我检查了其他类似的职位,但没有任何帮助。

UPDATE: I got the solution: The problem was I was flushing the already flushed transaction.And thus it was giving me no transaction in progress error along with the one i posted above.So i just removed the `getEm().flush(); 更新:我得到了解决方案:问题是我正在刷新已经刷新的事务,因此它没有上面显示的事务,也没有出现正在进行的事务错误,所以我只是删除了`getEm()。flush() ; and getEm().clear(); 和getEm()。clear(); from the finally block and it started working :) 从finally块开始,它开始工作:)

I got the solution: The problem was I was flushing the already flushed transaction.And thus it was giving me no transaction in progress error along with the one i posted above.So i just removed the getEm().flush(); and getEm().clear(); 我得到了解决方案:问题是我正在刷新已刷新的事务。因此,它没有上面显示的事务,因此没有正在进行的事务错误。所以我只删除了getEm().flush(); and getEm().clear(); getEm().flush(); and getEm().clear(); from the finally block and it started working :) 从finally块开始,它开始工作:)

Hope it will help others.. 希望对别人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM