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?
– LR10
Ai to Exception, I force an error in receipt to test the rollback, but even so the data is persisted.
– Bruno C.
Edit your question and add your own
persistence.xml
and the version of Hibernate you are using.– Felipe Marinho
There is the persistence
– Bruno C.