Skip to content
Browse files

HHH-5565 - Fix and test

  • Loading branch information...
1 parent d51a0d0 commit ffa9cf5768aa410edf87b4dc16d4538a8bd70133 @lukasz-antoniak lukasz-antoniak committed Jun 17, 2012
View
2 ...te-envers/src/main/java/org/hibernate/envers/event/BaseEnversCollectionEventListener.java
@@ -70,6 +70,8 @@ protected final void onCollectionAction(
return;
}
if ( getAuditConfiguration().getEntCfg().isVersioned( entityName ) ) {
+ checkIfTransactionInProgress(event.getSession());
+
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get(event.getSession());
String ownerEntityName = ((AbstractCollectionPersister) collectionEntry.getLoadedPersister()).getOwnerEntityName();
View
8 hibernate-envers/src/main/java/org/hibernate/envers/event/BaseEnversEventListener.java
@@ -31,6 +31,7 @@
import org.hibernate.envers.entities.RelationDescription;
import org.hibernate.envers.entities.RelationType;
import org.hibernate.envers.entities.mapper.id.IdMapper;
+import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.synchronization.AuditProcess;
import org.hibernate.envers.synchronization.work.CollectionChangeWorkUnit;
import org.hibernate.envers.tools.Tools;
@@ -126,4 +127,11 @@ private void addCollectionChangeWorkUnit(AuditProcess auditProcess, SessionImple
toPropertyName, enversConfiguration, id, value));
}
+ protected void checkIfTransactionInProgress(SessionImplementor session) {
+ if (!session.isTransactionInProgress()) {
+ // Historical data would not be flushed to audit tables if outside of active transaction
+ // (AuditProcess#doBeforeTransactionCompletion(SessionImplementor) not executed).
+ throw new AuditException("Unable to create revision because of non-active transaction");
+ }
+ }
}
View
2 ...te-envers/src/main/java/org/hibernate/envers/event/EnversPostDeleteEventListenerImpl.java
@@ -45,6 +45,8 @@ public void onPostDelete(PostDeleteEvent event) {
String entityName = event.getPersister().getEntityName();
if ( getAuditConfiguration().getEntCfg().isVersioned( entityName ) ) {
+ checkIfTransactionInProgress(event.getSession());
+
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get( event.getSession() );
AuditWorkUnit workUnit = new DelWorkUnit(
View
2 ...te-envers/src/main/java/org/hibernate/envers/event/EnversPostInsertEventListenerImpl.java
@@ -44,6 +44,8 @@ public void onPostInsert(PostInsertEvent event) {
String entityName = event.getPersister().getEntityName();
if ( getAuditConfiguration().getEntCfg().isVersioned( entityName ) ) {
+ checkIfTransactionInProgress(event.getSession());
+
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get(event.getSession());
AuditWorkUnit workUnit = new AddWorkUnit(
View
2 ...te-envers/src/main/java/org/hibernate/envers/event/EnversPostUpdateEventListenerImpl.java
@@ -46,6 +46,8 @@ public void onPostUpdate(PostUpdateEvent event) {
String entityName = event.getPersister().getEntityName();
if ( getAuditConfiguration().getEntCfg().isVersioned(entityName) ) {
+ checkIfTransactionInProgress(event.getSession());
+
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get(event.getSession());
final Object[] newDbState = postUpdateDBState( event );
View
117 ...ers/src/test/java/org/hibernate/envers/test/integration/basic/OutsideTransactionTest.java
@@ -0,0 +1,117 @@
+package org.hibernate.envers.test.integration.basic;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.envers.exception.AuditException;
+import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
+import org.hibernate.envers.test.entities.StrTestEntity;
+import org.hibernate.envers.test.integration.collection.norevision.Name;
+import org.hibernate.envers.test.integration.collection.norevision.Person;
+import org.hibernate.testing.TestForIssue;
+import org.junit.Test;
+
+/**
+ * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
+ */
+@TestForIssue(jiraKey = "HHH-5565")
+public class OutsideTransactionTest extends BaseEnversFunctionalTestCase {
+ @Override
+ protected Class<?>[] getAnnotatedClasses() {
+ return new Class[]{StrTestEntity.class, Person.class, Name.class};
+ }
+
+ @Override
+ protected void configure(Configuration configuration) {
+ configuration.setProperty("org.hibernate.envers.store_data_at_delete", "true");
+ configuration.setProperty("org.hibernate.envers.revision_on_collection_change", "true");
+ }
+
+ @Test(expected = AuditException.class)
+ public void testInsertOutsideActiveTransaction() {
+ Session session = openSession();
+
+ // Illegal insertion of entity outside of active transaction.
+ StrTestEntity entity = new StrTestEntity("data");
+ session.persist(entity);
+ session.flush();
+
+ session.close();
+ }
+
+ @Test(expected = AuditException.class)
+ public void testUpdateOutsideActiveTransaction() {
+ Session session = openSession();
+
+ // Revision 1
+ session.getTransaction().begin();
+ StrTestEntity entity = new StrTestEntity("data");
+ session.persist(entity);
+ session.getTransaction().commit();
+
+ // Illegal modification of entity state outside of active transaction.
+ entity.setStr("modified data");
+ session.update(entity);
+ session.flush();
+
+ session.close();
+ }
+
+ @Test(expected = AuditException.class)
+ public void testDeleteOutsideActiveTransaction() {
+ Session session = openSession();
+
+ // Revision 1
+ session.getTransaction().begin();
+ StrTestEntity entity = new StrTestEntity("data");
+ session.persist(entity);
+ session.getTransaction().commit();
+
+ // Illegal removal of entity outside of active transaction.
+ session.delete(entity);
+ session.flush();
+
+ session.close();
+ }
+
+ @Test(expected = AuditException.class)
+ public void testCollectionUpdateOutsideActiveTransaction() {
+ Session session = openSession();
+
+ // Revision 1
+ session.getTransaction().begin();
+ Person person = new Person();
+ Name name = new Name();
+ name.setName("Name");
+ person.getNames().add(name);
+ session.saveOrUpdate(person);
+ session.getTransaction().commit();
+
+ // Illegal collection update outside of active transaction.
+ person.getNames().remove(name);
+ session.saveOrUpdate(person);
+ session.flush();
+
+ session.close();
+ }
+
+ @Test(expected = AuditException.class)
+ public void testCollectionRemovalOutsideActiveTransaction() {
+ Session session = openSession();
+
+ // Revision 1
+ session.getTransaction().begin();
+ Person person = new Person();
+ Name name = new Name();
+ name.setName("Name");
+ person.getNames().add(name);
+ session.saveOrUpdate(person);
+ session.getTransaction().commit();
+
+ // Illegal collection removal outside of active transaction.
+ person.setNames(null);
+ session.saveOrUpdate(person);
+ session.flush();
+
+ session.close();
+ }
+}

0 comments on commit ffa9cf5

Please sign in to comment.
Something went wrong with that request. Please try again.