- Java EE 8 Cookbook
- Elder Moraes
- 114字
- 2025-02-17 14:53:40
How it works...
The key code line in this recipe for JTA is right here:
<persistence-unit name="ch02-jta-pu" transaction-type="JTA">
When you use transaction-type='JTA', you are saying to the server that it should take care of all transactions made under this context. If you use RESOURCE-LOCAL instead, you are saying that you are taking care of the transactions:
@Test
public void validTransaction() throws Exception{
User user = new User(null, "Elder Moraes",
"elder@eldermoraes.com");
userBean.add(user);
user.setName("John Doe");
userBean.update(user);
User userDb = userBean.findById(1L);
assertEquals(userDb.getName(), "John Doe");
}
Each called method of UserBean starts a transaction to be completed and would run into a rollback if there's any issue while the transaction is alive would commit to the end of it.