From bbe408f99486a1afbed506b6800558693d632327 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Wed, 17 Apr 2013 16:12:05 +0200 Subject: [PATCH] HHH-8189 - Rollback audit records on flushed transaction rollback --- .../synchronization/AuditProcessManager.java | 12 ++++- .../basic/TransactionRollbackBehaviour.java | 48 ++++++++++++++----- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java index dc39a15003..41a435466f 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/AuditProcessManager.java @@ -27,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.hibernate.Transaction; import org.hibernate.action.spi.AfterTransactionCompletionProcess; +import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.envers.revisioninfo.RevisionInfoGenerator; import org.hibernate.event.spi.EventSource; @@ -53,7 +54,16 @@ public class AuditProcessManager { auditProcess = new AuditProcess(revisionInfoGenerator, session); auditProcesses.put(transaction, auditProcess); - session.getActionQueue().registerProcess(auditProcess); + session.getActionQueue().registerProcess( + new BeforeTransactionCompletionProcess() { + public void doBeforeTransactionCompletion(SessionImplementor session) { + final AuditProcess process = auditProcesses.get( transaction ); + if ( process != null ) { + process.doBeforeTransactionCompletion( session ); + } + } + } + ); session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() { public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java index b6622af69b..98c0e5cb27 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java @@ -1,19 +1,19 @@ package org.hibernate.envers.test.integration.basic; import java.util.List; - import javax.persistence.EntityManager; +import org.junit.Assert; +import org.junit.Test; + import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; import org.hibernate.envers.test.entities.IntTestEntity; -import org.junit.Assert; -import org.junit.Test; +import org.hibernate.testing.TestForIssue; /** * @author Tomasz Dziurko (tdziurko at gmail dot com) */ public class TransactionRollbackBehaviour extends BaseEnversJPAFunctionalTestCase { - @Test public void testAuditRecordsRollback() { // Given @@ -32,15 +32,39 @@ public class TransactionRollbackBehaviour extends BaseEnversJPAFunctionalTestCas em.getTransaction().commit(); // Then - List revisionsForSavedClass = getAuditReader() - .getRevisions( IntTestEntity.class, ite2Id ); - Assert.assertEquals( "There should be one revision for inserted entity", - 1, revisionsForSavedClass.size() ); + List revisionsForSavedClass = getAuditReader().getRevisions( IntTestEntity.class, ite2Id ); + Assert.assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() ); - List revisionsForRolledbackClass = getAuditReader() - .getRevisions( IntTestEntity.class, rollbackedIteId ); - Assert.assertEquals( "There should be one revision for inserted entity", - 0, revisionsForRolledbackClass.size() ); + List revisionsForRolledbackClass = getAuditReader().getRevisions( IntTestEntity.class, rollbackedIteId ); + Assert.assertEquals( "There should be no revision for rolled back transaction.", 0, revisionsForRolledbackClass.size() ); + } + + @Test + @TestForIssue( jiraKey = "HHH-8189" ) + public void testFlushedAuditRecordsRollback() { + // Given + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + IntTestEntity iteToRollback = new IntTestEntity( 30 ); + em.persist( iteToRollback ); + em.flush(); + Integer rollbackedIteId = iteToRollback.getId(); + em.getTransaction().rollback(); + + // When + em.getTransaction().begin(); + IntTestEntity ite2 = new IntTestEntity( 50 ); + em.persist( ite2 ); + em.flush(); + Integer ite2Id = ite2.getId(); + em.getTransaction().commit(); + + // Then + List revisionsForSavedClass = getAuditReader().getRevisions( IntTestEntity.class, ite2Id ); + Assert.assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() ); + + List revisionsForRolledbackClass = getAuditReader().getRevisions( IntTestEntity.class, rollbackedIteId ); + Assert.assertEquals( "There should be no revision for rolled back transaction.", 0, revisionsForRolledbackClass.size() ); } @Override