Skip to content

Commit 28d7d19

Browse files
Felix FeisstNaros
authored andcommitted
HHH-11575 Fixed bug where multiple revisions could have been created
during a single transaction when flush mode is set to COMMIT.
1 parent 2d4da59 commit 28d7d19

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

hibernate-envers/src/main/java/org/hibernate/envers/internal/synchronization/AuditProcess.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import java.util.Map;
1212
import java.util.Queue;
1313

14-
import javax.persistence.FlushModeType;
15-
14+
import org.hibernate.FlushMode;
1615
import org.hibernate.Session;
1716
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
1817
import org.hibernate.engine.spi.SessionImplementor;
@@ -154,7 +153,7 @@ public void doBeforeTransactionCompletion(SessionImplementor session) {
154153
}
155154

156155
// see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431
157-
if ( FlushModeType.COMMIT.equals( session.getFlushMode() ) || session.isClosed() ) {
156+
if ( FlushMode.MANUAL.equals( session.getHibernateFlushMode() ) || session.isClosed() ) {
158157
Session temporarySession = null;
159158
try {
160159
temporarySession = session.sessionWithOptions()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.envers.test.integration.flush;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
import javax.persistence.EntityManager;
12+
13+
import org.hibernate.FlushMode;
14+
import org.hibernate.envers.enhanced.SequenceIdRevisionEntity;
15+
import org.hibernate.envers.query.AuditEntity;
16+
import org.hibernate.envers.test.entities.StrTestEntity;
17+
import org.hibernate.testing.TestForIssue;
18+
import org.junit.Test;
19+
20+
/**
21+
* @author Felix Feisst (feisst dot felix at gmail dot com)
22+
*/
23+
public class CommitFlushSingleRevisionInTransaction extends AbstractFlushTest {
24+
25+
@Override
26+
public FlushMode getFlushMode() {
27+
return FlushMode.COMMIT;
28+
}
29+
30+
@Test
31+
@TestForIssue(jiraKey = "HHH-11575")
32+
public void testSingleRevisionInTransaction() {
33+
EntityManager em = getEntityManager();
34+
35+
em.getTransaction().begin();
36+
37+
SequenceIdRevisionEntity revisionBeforeFlush = getAuditReader().getCurrentRevision( SequenceIdRevisionEntity.class, true );
38+
int revisionNumberBeforeFlush = revisionBeforeFlush.getId();
39+
40+
em.flush();
41+
42+
StrTestEntity entity = new StrTestEntity( "entity" );
43+
em.persist( entity );
44+
45+
em.getTransaction().commit();
46+
47+
SequenceIdRevisionEntity entity2Revision = (SequenceIdRevisionEntity) ( (Object[]) getAuditReader().createQuery()
48+
.forRevisionsOfEntity( StrTestEntity.class, false, false ).add( AuditEntity.id().eq( entity.getId() ) ).getSingleResult() )[1];
49+
50+
assertEquals(
51+
"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",
52+
revisionNumberBeforeFlush,
53+
entity2Revision.getId() );
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)