RollBack() JPA + Hibernate

Question:

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

Here is 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 recorded 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 rollback() work correctly??

   <?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>

Hibernate version is 5.2.4

Answer:

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

Operation code cannot be null check this and run again!!

catch (RuntimeException  e) {
   em.getTransaction().rollback();
    throw e;
}finally{
    em.getTransaction().commit();
   em.close();
 } 
Scroll to Top