[hibernate-commits] Hibernate SVN: r16502 - in core/trunk/envers/src: test/java/org/hibernate/envers/test/integration/reventity and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sun May 3 09:57:43 EDT 2009


Author: adamw
Date: 2009-05-03 09:57:41 -0400 (Sun, 03 May 2009)
New Revision: 16502

Added:
   core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListener.java
   core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListenerRevEntity.java
   core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/TestExceptionRevisionListener.java
Modified:
   core/trunk/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java
Log:
Rolling back the TX when an exception occures in a revision listener

Modified: core/trunk/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java	2009-05-01 23:21:00 UTC (rev 16501)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java	2009-05-03 13:57:41 UTC (rev 16502)
@@ -145,26 +145,32 @@
             return;
         }
 
-        // see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431
-        if (FlushMode.isManualFlushMode(session.getFlushMode()) || session.isClosed()) {
-            Session temporarySession = null;
-            try {
-                temporarySession = session.getFactory().openTemporarySession();
+		try {
+			// see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431
+			if (FlushMode.isManualFlushMode(session.getFlushMode()) || session.isClosed()) {
+				Session temporarySession = null;
+				try {
+					temporarySession = session.getFactory().openTemporarySession();
 
-                executeInSession(temporarySession);
+					executeInSession(temporarySession);
 
-                temporarySession.flush();
-            } finally {
-                if (temporarySession != null) {
-                    temporarySession.close();
-                }
-            }
-        } else {
-            executeInSession(session);
+					temporarySession.flush();
+				} finally {
+					if (temporarySession != null) {
+						temporarySession.close();
+					}
+				}
+			} else {
+				executeInSession(session);
 
-            // Explicity flushing the session, as the auto-flush may have already happened.
-            session.flush();
-        }
+				// Explicity flushing the session, as the auto-flush may have already happened.
+				session.flush();
+			}
+		} catch (RuntimeException e) {
+			// Rolling back the transaction in case of any exceptions
+			session.getTransaction().rollback();
+			throw e;
+		}
     }
 
     public void afterCompletion(int i) {

Copied: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListener.java (from rev 16487, core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/Listener.java)
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListener.java	                        (rev 0)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListener.java	2009-05-03 13:57:41 UTC (rev 16502)
@@ -0,0 +1,60 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.envers.test.integration.reventity;
+
+import javax.persistence.EntityManager;
+
+import org.hibernate.envers.test.AbstractEntityTest;
+import org.hibernate.envers.test.entities.StrTestEntity;
+import org.testng.annotations.Test;
+
+import org.hibernate.ejb.Ejb3Configuration;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class ExceptionListener extends AbstractEntityTest {
+    public void configure(Ejb3Configuration cfg) {
+        cfg.addAnnotatedClass(StrTestEntity.class);
+        cfg.addAnnotatedClass(ExceptionListenerRevEntity.class);
+    }
+
+    @Test
+    public void testTransactionRollback() throws InterruptedException {
+        // Trying to persist an entity - however the listener should throw an exception, so the entity
+		// shouldn't be persisted
+        EntityManager em = getEntityManager();
+        em.getTransaction().begin();
+        StrTestEntity te = new StrTestEntity("x");
+        em.persist(te);
+        em.getTransaction().commit();
+
+		// Checking if the entity became persisted
+		em = getEntityManager();
+        em.getTransaction().begin();
+        Long count = (Long) em.createQuery("select count(s) from StrTestEntity s where s.str = 'x'").getSingleResult();
+		assert count == 0l;
+        em.getTransaction().commit();
+    }
+}
\ No newline at end of file

Copied: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListenerRevEntity.java (from rev 16487, core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ListenerRevEntity.java)
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListenerRevEntity.java	                        (rev 0)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/ExceptionListenerRevEntity.java	2009-05-03 13:57:41 UTC (rev 16502)
@@ -0,0 +1,82 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.envers.test.integration.reventity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.hibernate.envers.RevisionEntity;
+import org.hibernate.envers.RevisionNumber;
+import org.hibernate.envers.RevisionTimestamp;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+ at Entity
+ at RevisionEntity(TestExceptionRevisionListener.class)
+public class ExceptionListenerRevEntity {
+    @Id
+    @GeneratedValue
+    @RevisionNumber
+    private int id;
+
+    @RevisionTimestamp
+    private long timestamp;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public long getTimestamp() {
+        return timestamp;
+    }
+
+    public void setTimestamp(long timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (!(o instanceof ExceptionListenerRevEntity)) return false;
+
+        ExceptionListenerRevEntity revEntity = (ExceptionListenerRevEntity) o;
+
+        if (id != revEntity.id) return false;
+        if (timestamp != revEntity.timestamp) return false;
+
+        return true;
+    }
+
+    public int hashCode() {
+        int result;
+        result = id;
+        result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
+        return result;
+    }
+}
\ No newline at end of file

Copied: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/TestExceptionRevisionListener.java (from rev 16487, core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/TestRevisionListener.java)
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/TestExceptionRevisionListener.java	                        (rev 0)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/reventity/TestExceptionRevisionListener.java	2009-05-03 13:57:41 UTC (rev 16502)
@@ -0,0 +1,35 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.envers.test.integration.reventity;
+
+import org.hibernate.envers.RevisionListener;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class TestExceptionRevisionListener implements RevisionListener {
+    public void newRevision(Object revisionEntity) {
+        throw new RuntimeException();
+    }
+}
\ No newline at end of file




More information about the hibernate-commits mailing list