Don't wrap exceptions thrown inside Cloud SQL transactions (#338)

* Don't wrap exceptions thrown inside Cloud SQL transactions

There's no reason to wrap them in PersistenceException, and it makes dealing
with thrown exceptions harder as seen in the diffs on test classes in this
commit.
This commit is contained in:
Ben McIlwain
2019-11-03 14:08:31 -05:00
committed by GitHub
parent 6f87fc115f
commit 5f62f91cd5
6 changed files with 13 additions and 31 deletions
@@ -41,6 +41,7 @@ public final class RegistryLockDao {
Long.class)
.setParameter("verificationCode", verificationCode)
.getSingleResult();
// TODO(gbrodman): Don't throw NPE here. Maybe NoResultException fits better?
checkNotNull(revisionId, "No registry lock with this code");
return em.find(RegistryLock.class, revisionId);
});
@@ -79,17 +79,14 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
T result = work.run();
txn.commit();
return result;
} catch (Throwable transactionException) {
String rollbackMessage;
} catch (RuntimeException e) {
try {
txn.rollback();
rollbackMessage = "transaction rolled back";
logger.atWarning().log("Error during transaction; transaction rolled back");
} catch (Throwable rollbackException) {
logger.atSevere().withCause(rollbackException).log("Rollback failed, suppressing error");
rollbackMessage = "transaction rollback failed";
logger.atSevere().withCause(rollbackException).log("Rollback failed; suppressing error");
}
throw new PersistenceException(
"Error during transaction, " + rollbackMessage, transactionException);
throw e;
} finally {
txnInfo.clear();
}