Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accomodate manually initiated rollbacks #1137

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
//everything was normal so commit the txn (do not move into try block above as it
// interferes with the advised method's throwing semantics)
try {
txn.commit();
if (txn.isActive()) {
if (txn.getRollbackOnly()) {
txn.rollback();
} else {
txn.commit();
}
}
} finally {
//close the em if necessary
if (null != didWeStartWork.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
import com.google.inject.persist.PersistService;
import com.google.inject.persist.Transactional;
import com.google.inject.persist.UnitOfWork;

import java.io.IOException;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;

import junit.framework.TestCase;

/** @author Dhanji R. Prasanna (dhanji@gmail.com) */
/**
* @author Dhanji R. Prasanna (dhanji@gmail.com)
*/

public class ManagedLocalTransactionsTest extends TestCase {
private Injector injector;
Expand Down Expand Up @@ -146,6 +150,53 @@ public void testSimpleTransactionRollbackOnUnchecked() {
}
}

public void testSimpleTransactionRollbackPerformedManuallyWithoutException() {
try {
injector.getInstance(TransactionalObject.class).runOperationInTxnWithManualRollback();
} catch (RuntimeException re) {
fail("finishing the transactional resulted in an exception");
}

EntityManager em = injector.getInstance(EntityManager.class);
assertFalse(
"Session was not closed by transactional service (rollback didnt happen?)",
em.getTransaction().isActive());

try {
Object result =
em.createQuery("from JpaTestEntity where text = :text")
.setParameter("text", TRANSIENT_UNIQUE_TEXT)
.getSingleResult();
injector.getInstance(UnitOfWork.class).end();
fail("a result was returned! rollback sure didnt happen!!!");
} catch (NoResultException e) {
}
}

public void testSimpleTransactionRollbackOnlySetWithoutException() {
try {
injector.getInstance(TransactionalObject.class).runOperationInTxnWithRollbackOnlySet();
} catch (RuntimeException re) {
fail("finishing the transactional resulted in an exception");
}

EntityManager em = injector.getInstance(EntityManager.class);
assertFalse(
"Session was not closed by transactional service (rollback didnt happen?)",
em.getTransaction().isActive());

try {
Object result =
em.createQuery("from JpaTestEntity where text = :text")
.setParameter("text", TRANSIENT_UNIQUE_TEXT)
.getSingleResult();
injector.getInstance(UnitOfWork.class).end();
fail("a result was returned! rollback sure didnt happen!!!");
} catch (NoResultException e) {
}
}


public static class TransactionalObject {
private final EntityManager em;

Expand Down Expand Up @@ -185,5 +236,23 @@ public void runOperationInTxnThrowingUnchecked() {

throw new IllegalStateException();
}

@Transactional
public void runOperationInTxnWithManualRollback() {
JpaTestEntity entity = new JpaTestEntity();
entity.setText(TRANSIENT_UNIQUE_TEXT);
em.persist(entity);

em.getTransaction().rollback();
}

@Transactional
public void runOperationInTxnWithRollbackOnlySet() {
JpaTestEntity entity = new JpaTestEntity();
entity.setText(TRANSIENT_UNIQUE_TEXT);
em.persist(entity);

em.getTransaction().setRollbackOnly();
}
}
}