简体   繁体   中英

Hibernate Jpa batch insert not working

I am using hibernate jpa to perform a batch update .

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. 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(); and getEm().clear(); from the finally block and it started working :)

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(); from the finally block and it started working :)

Hope it will help others..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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