0
即使我在update()方法抛出空指针异常,以下代码也无助于回滚。每次运行代码时,它都会将值插入数据库中。请帮助我如何在update()方法抛出空指针时回滚事务。我在代码中丢失了什么吗?CMT回滚:如何回滚事务
@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class Bean implements RemoteIF {
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void insertIntoDb() {
insert();
update();
}
private Integer update() {
val=0;
try {
Connection con = DbConn.getConnection();
Statement st = con.createStatement();
val1 = st.executeUpdate("INSERT INTO tab VALUES('ab')");
st.close();
throw new NullPointerException();
} catch (Exception e) {
System.out.println(e);
}
return val;
}
private Integer insert() {
int val = 0;
try {
Connection con = DbConn.getConnection();
Statement st = con.createStatement();
val = st.executeUpdate("INSERT INTO tab VALUES('bnm')");
st.close();
} catch (Exception e) {
System.out.println(e);
}
return val;
}
}
你为什么不使用JPA?另请参阅在线教程http://docs.oracle.com/javaee/6/tutorial/doc/以了解有关EJB中的事务传播,回滚等的更多信息 – 2012-01-16 15:24:57