HHH-11575 Fixed bug where multiple revisions could have been created

during a single transaction when flush mode is set to COMMIT.
This commit is contained in:
Felix Feisst 2017-03-16 21:49:55 +01:00 committed by Chris Cranford
parent 2d4da59611
commit 28d7d196f4
2 changed files with 59 additions and 3 deletions

View File

@ -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()

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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() );
}
}