From 28d7d196f49775193a5b9b674e122ad223bd587d Mon Sep 17 00:00:00 2001 From: Felix Feisst Date: Thu, 16 Mar 2017 21:49:55 +0100 Subject: [PATCH] HHH-11575 Fixed bug where multiple revisions could have been created during a single transaction when flush mode is set to COMMIT. --- .../synchronization/AuditProcess.java | 5 +- ...ommitFlushSingleRevisionInTransaction.java | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/flush/CommitFlushSingleRevisionInTransaction.java diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java index 44f9528169..ac47d29ee8 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java @@ -11,8 +11,7 @@ import java.util.LinkedList; import java.util.Map; import java.util.Queue; -import javax.persistence.FlushModeType; - +import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.engine.spi.SessionImplementor; @@ -154,7 +153,7 @@ public class AuditProcess implements BeforeTransactionCompletionProcess { } // see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431 - if ( FlushModeType.COMMIT.equals( session.getFlushMode() ) || session.isClosed() ) { + if ( FlushMode.MANUAL.equals( session.getHibernateFlushMode() ) || session.isClosed() ) { Session temporarySession = null; try { temporarySession = session.sessionWithOptions() diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/flush/CommitFlushSingleRevisionInTransaction.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/flush/CommitFlushSingleRevisionInTransaction.java new file mode 100644 index 0000000000..43e6aef612 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/flush/CommitFlushSingleRevisionInTransaction.java @@ -0,0 +1,57 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.envers.test.integration.flush; + +import static org.junit.Assert.assertEquals; + +import javax.persistence.EntityManager; + +import org.hibernate.FlushMode; +import org.hibernate.envers.enhanced.SequenceIdRevisionEntity; +import org.hibernate.envers.query.AuditEntity; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +/** + * @author Felix Feisst (feisst dot felix at gmail dot com) + */ +public class CommitFlushSingleRevisionInTransaction extends AbstractFlushTest { + + @Override + public FlushMode getFlushMode() { + return FlushMode.COMMIT; + } + + @Test + @TestForIssue(jiraKey = "HHH-11575") + public void testSingleRevisionInTransaction() { + EntityManager em = getEntityManager(); + + em.getTransaction().begin(); + + SequenceIdRevisionEntity revisionBeforeFlush = getAuditReader().getCurrentRevision( SequenceIdRevisionEntity.class, true ); + int revisionNumberBeforeFlush = revisionBeforeFlush.getId(); + + em.flush(); + + StrTestEntity entity = new StrTestEntity( "entity" ); + em.persist( entity ); + + em.getTransaction().commit(); + + SequenceIdRevisionEntity entity2Revision = (SequenceIdRevisionEntity) ( (Object[]) getAuditReader().createQuery() + .forRevisionsOfEntity( StrTestEntity.class, false, false ).add( AuditEntity.id().eq( entity.getId() ) ).getSingleResult() )[1]; + + assertEquals( + "The revision number obtained before the flush and the persisting of the entity should be the same as the revision number of the entity because there should only be one revision number per transaction", + revisionNumberBeforeFlush, + entity2Revision.getId() ); + + } + +}