Implement remaining methods in JpaTransactionManager (#633)

This commit is contained in:
Shicong Huang 2020-06-17 12:39:34 -04:00 committed by GitHub
parent 31841ccc55
commit 2f600e3e69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 10 deletions

View file

@ -122,32 +122,35 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
@Override
public <T> T transactNew(Supplier<T> work) {
// TODO(shicong): Implements the functionality to start a new transaction.
throw new UnsupportedOperationException();
return transact(work);
}
@Override
public void transactNew(Runnable work) {
// TODO(shicong): Implements the functionality to start a new transaction.
throw new UnsupportedOperationException();
transact(work);
}
@Override
public <T> T transactNewReadOnly(Supplier<T> work) {
// TODO(shicong): Implements read only transaction.
throw new UnsupportedOperationException();
return transact(
() -> {
getEntityManager().createNativeQuery("SET TRANSACTION READ ONLY").executeUpdate();
return work.get();
});
}
@Override
public void transactNewReadOnly(Runnable work) {
// TODO(shicong): Implements read only transaction.
throw new UnsupportedOperationException();
transactNewReadOnly(
() -> {
work.run();
return null;
});
}
@Override
public <T> T doTransactionless(Supplier<T> work) {
// TODO(shicong): Implements doTransactionless.
throw new UnsupportedOperationException();
return transact(work);
}
@Override

View file

@ -116,6 +116,30 @@ public class TransactionManagerTest {
assertEntityExists(theEntity);
}
@TestTemplate
void transactNew_succeeds() {
assertEntityNotExist(theEntity);
tm().transactNew(() -> tm().saveNew(theEntity));
assertEntityExists(theEntity);
}
@TestTemplate
void transactNewReadOnly_succeeds() {
assertEntityNotExist(theEntity);
tm().transact(() -> tm().saveNew(theEntity));
assertEntityExists(theEntity);
TestEntity persisted = tm().transactNewReadOnly(() -> tm().load(theEntity.key()));
assertThat(persisted).isEqualTo(theEntity);
}
@TestTemplate
void transactNewReadOnly_throwsWhenWritingEntity() {
assertEntityNotExist(theEntity);
assertThrows(
RuntimeException.class, () -> tm().transactNewReadOnly(() -> tm().saveNew(theEntity)));
assertEntityNotExist(theEntity);
}
@TestTemplate
void saveNew_succeeds() {
assertEntityNotExist(theEntity);