I use JavaEE 8 with OpenLiberty Application server .
in my project i try to use JTA over container managed transaction (BMT) in CRUD layer .
this is my example code :
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SampleCRUD {
@Inject
private Logger logger;
@PersistenceContext
private EntityManager em;
public void insertFood(Food food) {
em.persist(food);
}
public void updateFood(Food food) {
em.merge(food);
}
public Food selectFood(long id) {
return em.find(Food.class, id);
}
public void deleteFood(long id) {
em.remove(select(id));
}
}
and Food entity :
@Table
@Entity
@SequenceGenerator(name = "default_seq", sequenceName = "food_seq", allocationSize = 1)
public class Food extends BaseEntity {
@Column(unique = true)
private String name;
I want to understand :
is there any recommendation to select on database before sql insert/delete/update operations ?
I ask this question because in CMT mode can not catch Constraint or SQL exceptions on application .
in my example codes :
- need select before persist because duplicate key exception results in application sevrer .
- need select before remove because entity not found exception results in application server .
What is JTA with container managed transaction (CMT) advantage ?