HHH-8189 - Rollback audit records on flushed transaction rollback

This commit is contained in:
Lukasz Antoniak 2013-04-17 15:57:54 +02:00
parent 8342194057
commit a8f0552b81
2 changed files with 44 additions and 10 deletions

View File

@ -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.internal.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) {

View File

@ -8,12 +8,12 @@ import org.junit.Test;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.entities.IntTestEntity;
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<Number> revisionsForSavedClass = getAuditReader()
.getRevisions( IntTestEntity.class, ite2Id );
Assert.assertEquals( "There should be one revision for inserted entity",
1, revisionsForSavedClass.size() );
List<Number> revisionsForSavedClass = getAuditReader().getRevisions( IntTestEntity.class, ite2Id );
Assert.assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() );
List<Number> revisionsForRolledbackClass = getAuditReader()
.getRevisions( IntTestEntity.class, rollbackedIteId );
Assert.assertEquals( "There should be one revision for inserted entity",
0, revisionsForRolledbackClass.size() );
List<Number> 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<Number> revisionsForSavedClass = getAuditReader().getRevisions( IntTestEntity.class, ite2Id );
Assert.assertEquals( "There should be one revision for inserted entity.", 1, revisionsForSavedClass.size() );
List<Number> revisionsForRolledbackClass = getAuditReader().getRevisions( IntTestEntity.class, rollbackedIteId );
Assert.assertEquals( "There should be no revision for rolled back transaction.", 0, revisionsForRolledbackClass.size() );
}
@Override