我有一个 a_DAO 类,注明为 @Transactional ,在其中我有一个名为 x(字符串标记)的方法。此方法在名为 y()的 b_BC 类方法中调用,该方法也注释为 @Transactional ,并且还称为持久性的另一种方法好。问题是在 x()方法中,英国的异常可能发生在数据库中,我想在容器在数据库中持久化之前捕获。
a_DAO.class
@Singleton
@Transactional
public class a_DAO {
@PersistenceContext
private EntityManager em;
a_DAO() {
}
public static a_DAO getInstance() {
return CDI.current().select(a_DAO.class).get();
}
/* If a UK problem occurs here I'd like to throw the exception before the container does it. */
public void x(String token) {
try {
em.persist(token);
} catch (Exception e) {
System.out.println("Here I'd like do intercept the error");
}
}
}
b_BC.class
public class b_BC {
@Transactional
public void y() {
/*
my business logic
*/
a_DAO.getInstance().insert(token);
// c_DAO is another DAO
c_DAO.getInstance().insert("xxx");
}