Rollback() JPA + Hibernate

Asked

Viewed 616 times

3

I have a problem with persisting several information in the same transaction, in case something goes wrong a part of the information is recorded in the bank and the rollback() is not executed.

Follow an example:

try 
   {


    em = ConnectionHib.emf.createEntityManager();

    em.getTransaction().begin();
    Recibo recibo = new Recibo();
    Log log = new Log();

    em.createQuery("UPDATE Numeracao s SET s.fcNoRecibo = s.fcNoRecibo+1 WHERE s.emp= :emp ")
    .setParameter("emp", emp.getCodigo())
    .executeUpdate();       

    recibo.setNoRecibo(noRecibo);       
    log.setnorecibo(noRecibo); 

    em.persist(recibo);
    em.persist(log);


    em.getTransaction().commit();

   } 
   catch (Exception e) 
   {

    em.getTransaction().rollback();

    throw e;
   } 
     finally 
   {
    em.close();
   }

If something goes wrong in the log Insert, the receipt data and the update command in the numbering is saved in the database.

Error presented:

jan 16, 2018 11:06:12 AM
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL
Error: 1048, SQLState: 23000 jan 16, 2018 11:06:12 AM  
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR:
Column ‘COD_OPERACAO’ cannot be null jan 16, 2018 11:06:12 AM  
org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release INFO:
HHH000010: On release of batch it still contained JDBC statements jan 
16, 2018 11:06:12 AM org.hibernate.internal.ExceptionMapperStandardImpl 
mapManagedFlushFailure ERROR: HHH000346: Error during managed flush 
[org.hibernate.exception.ConstraintViolationException: could not execute 
statement] javax.persistence.RollbackException: Error while committing 
the transaction at org.hibernate.internal.ExceptionConverterImpl.
convertCommitException(ExceptionConverterImpl.java:75) at  
org.hibernate.engine.transaction.internal.TransactionImpl.
commit(TransactionImpl.java:71)

What can be done to make the rollback() work properly??

   <?xml version="1.0" encoding="UTF-8" ?>
   <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence">

    <persistence-unit name="ConnectionData" transaction-type="RESOURCE_LOCAL">  
        <properties>
            <property name="hibernate.show_sql" value="true"/>          
        </properties>   
        <!-- <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.0.20:3306/basedados" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties> -->
    </persistence-unit>

</persistence>

Version of Hibernate is 5.2.4

  • Which error is shown?

  • Ai to Exception, I force an error in receipt to test the rollback, but even so the data is persisted.

  • Edit your question and add your own persistence.xml and the version of Hibernate you are using.

  • There is the persistence

2 answers

0

org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Column ‘COD_OPERACAO’ cannot be null jan 16, 2018 11:06:12 AM 

Code operation cannot be null see this and run again!!

catch (RuntimeException  e) {
   em.getTransaction().rollback();
    throw e;
}finally{
    em.getTransaction().commit();
   em.close();
 } 
  • Yes I know, I’m forcing this mistake, the problem is the rollback that doesn’t run.

  • Exception exchange for Runtimeexception. I think you can force the error otherwise..

  • I switched but the result is still the same.

  • catch (persistenceexception e) achoq is more specific! it be entering Cath ?

  • I made the exchange, it enters the catch yes, it just not effective the rollback in the database.

  • On what object does he not perform Rollbak ? all ?

  • This in all, it saves the log, saves the update and triggers the error in receipt, in the understanding of the transaction was to undo everything.

  • Move your.getTransaction(). commit(); to finnaly.. forehead ai..

  • I did it but you still recorded it.

  • @Brunoc. you may have set auto commit to true, check out your Hibernate settings.

  • Hibernate Auto Commit is false.

Show 6 more comments

0

Use in function

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = java.lang.Exception.class, timeout = DEFAUL_TIMEOUT)
    public void suaFuncao () throws Exception{  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.