jsp中用mysql实现事物处理.高手们进来看看!不会也来捧个场,来者有分!
我建立的表类型是BDB
程序如下:
---------------------------------------------------
public void registerAttraction(Attraction attraction)
throws AttractionException{
validateAttraction(attraction);
Connection con = null;
try{
con = pool.getConnection();
con.setAutoCommit(false);
AttractionDAO attractionDAO = new AttractionDAO(con);
attractionDAO.create(attraction);
UserRoleDAO userRoleDAO = new UserRoleDAO(con);
UserRole userRole = new UserRole();
userRole.setUserId(attraction.getUserId());
userRole.setRoleName(Role.ATTRACTION);
userRoleDAO.create(userRole);
con.commit();
}catch(Exception e){
try{
if(con != null){
con.rollback();
throw new AttractionException(e.getMessage());
}
}catch(SQLException sqle){
System.out.println("AttractionBO.registerAttraction sqle:" + sqle);
e.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}finally{
try{
con.setAutoCommit(true);
if(con != null){
con.close();
}
}catch(SQLException sqle){
sqle.printStackTrace();
throw new RuntimeException("error.unexpected");
}
}
}
---------------------------------------------------------------
我事先已经执行con.setAutoCommit(false);将自动提交关闭.
为了测试是否实现了事物处理我故意将userRoleDAO.create(userRole);
操作中的表名改错.看看con.rollback();是否能够将数据写回.结果
attractionDAO.create(attraction);还是执行了.为什么呢?