SpringFramework: @Transactional (readOnly = true) not working with h2
I am doing transaction testing with SpringFramework. I have the following classes.
UserService.class
@Transactional
public interface UserService {
void add(User user);
@Transactional(readOnly = true)
User get(String id);
@Transactional(readOnly = true)
List<User> getAll();
void deleteAll();
void update(User user);
}
UserServiceImpl.class
public class UserServiceImpl implements UserService {
// skip some methods
@Override public void update(User user) { userDao.update(user); }
}
TestUserService.class
public class TestUserService extends UserServiceImpl {
@Override
public List<User> getAll() {
for (User user : super.getAll()) {
// try to update in get* method. it read-only.
super.update(user);
}
return null;
}
}
Test code
@Test(expected = TransientDataAccessResourceException.class)
public void readOnlyTransactionAttribute() {
testUserService.getAll();
}
This test code is successful when using mysql. But it doesn't work when using H2 In-memory. This is because the transaction completes without exception.
I read the Spring Framework documentation and found this: http://docs.spring.io/spring/docs/4.3.x/javadoc-api/org/springframework/transaction/annotation/Transactional.html#readOnly--
This is just a hint for the real transaction subsystem; this does not necessarily cause write access attempts to fail. A transaction manager that cannot interpret the read-only hint will not throw an exception when requesting a read-only transaction, but silently ignores the hint.
But I'm wondering if there is a way to pass this test with an H2 database.
source to share
No one has answered this question yet
Check out similar questions: