From 2f7b4e3a24d77fc2960b20e6dbdf4efea51bf34a Mon Sep 17 00:00:00 2001 From: "harry.chan" Date: Thu, 1 Aug 2013 11:42:38 +0800 Subject: [PATCH] added a test case to show the issue that the user record does NOT rolback when there is an exception thrown --- ...TransactionNotTerminatedAfterRollback.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/test/java/com/avaje/tests/transaction/TransactionNotTerminatedAfterRollback.java diff --git a/src/test/java/com/avaje/tests/transaction/TransactionNotTerminatedAfterRollback.java b/src/test/java/com/avaje/tests/transaction/TransactionNotTerminatedAfterRollback.java new file mode 100644 index 000000000..4616172d3 --- /dev/null +++ b/src/test/java/com/avaje/tests/transaction/TransactionNotTerminatedAfterRollback.java @@ -0,0 +1,72 @@ +package com.avaje.tests.transaction; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.PersistenceException; + +import org.avaje.agentloader.AgentLoader; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.avaje.ebean.Ebean; +import com.avaje.ebean.annotation.Transactional; + +/** + * It shows the issue that the user record does NOT rolback when there is an exception thrown + */ +public class TransactionNotTerminatedAfterRollback { + private static final Logger LOG = LoggerFactory.getLogger(TransactionNotTerminatedAfterRollback.class); + + @BeforeClass public static void preStart() { + LOG.debug("... preStart"); + // display the log message to see if the UserService is enhanced + AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent", "debug=1"); + } + + @Test public void test() { + try { + new UserService().create(new User(1L, "David")); + fail("Exception should be thrown"); + } catch (PersistenceException pe) { + LOG.error("e: " + pe); + } + List users = Ebean.find(User.class).findList(); + LOG.debug("users: {}", users); + assertTrue("users should be empty", users.isEmpty()); + } + + @Transactional(rollbackFor = PersistenceException.class) public class UserService { + public void create(User i) { + Ebean.save(i); + Ebean.save(new User(1L, "Peter")); // make it throw exception and rollback + } + } + + @Entity public class User { + @Id Long id; + String name; + + public User() {} + + public User(Long id, String name) { + this.id = id; + this.name = name; + } + + @Override public String toString() { + StringBuilder s = new StringBuilder(); + s.append("{"); + s.append("id: ").append(id).append(", "); + s.append("name: ").append(name); + s.append("}"); + return s.toString(); + } + } +}