分类: 系统运维
2008-05-24 21:55:14
id name address 1 xiaoming wuhan student2表中记录为空
public interface StudentManagerService { public void bus_method(); }
public interface StudentDAO { public void deleteStudent1(); public void insertStudent2(); }
public class StudentDAOImp extends JdbcDaoSupport implements StudentDAO{ //删除student1表中的id=1的记录 public void deleteStudent1(){ JdbcTemplate jt=this.getJdbcTemplate(); jt.update("delete from student1 where id=1"); } //将student1表中删除的记录插入到student2中,但是此方法实现有错,因为 //id字段设置为自增长的,所以在插入记录时我们不能指定值 public void insertStudent2(){ JdbcTemplate jt=this.getJdbcTemplate(); String arg[]=new String[3]; arg[0]="1"; arg[1]="xiaoming"; arg[2]="wuhan"; jt.update("insert student2(id,name,address) values(?,?,?)",arg); } }
public class StudentManagerServiceImp implements StudentManagerService{ private StudentDAO stdDAO; public void setStdDAO(StudentDAO stdDAO){ this.stdDAO=stdDAO; } //此方法为事务型的:删除student1中的记录成功且插入student2的记录也成功, //如果insertStudent2()方法执行失败,那么deleteStudent1()方法也应该会失败 public void bus_method(){ this.stdDAO.deleteStudent1(); this.stdDAO.insertStudent2(); } }
public class StudentManagerAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { try{ WebApplicationContext appContext=WebApplicationContextUtils. getWebApplicationContext(this.getServlet().getServletContext()); StudentManagerService stdm=(StudentManagerService)appContext. getBean("stdServiceManager"); stdm.bus_method(); return mapping.findForward("chenggong"); } catch(DataAccessException e){ System.err.println("action execute service exception!"); return mapping.findForward("shibai"); } } }
log4jConfigLocation /WEB-INF/log4j.properties contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.util.Log4jConfigListener org.springframework.web.context.ContextLoaderListener action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 0 action *.do
PROPAGATION_REQUIRED
public class StudentManagerServiceImp implements StudentManagerService{ private StudentDAO stdDAO; public void setStdDAO(StudentDAO stdDAO){ this.stdDAO=stdDAO; } //此方法为事务型的,删除student1中的记录成功且插入student2的记录也成功 //如果insertStudent2()方法执行失败,那么deleteStudent1()也应该会失败 public void bus_method(){ try{ this.stdDAO.deleteStudent1(); this.stdDAO.insertStudent2(); } catch(DataAccessException de) System.err.println("service execute exception!"); } } }
public class StudentDAOImp extends JdbcDaoSupport implements StudentDAO{ //删除student1表中的id=1的记录 public void deleteStudent1(){ try{ JdbcTemplate jt=this.getJdbcTemplate(); jt.update("delete from student1 where id=1"); } catch(DataAccessException e){ System.err.println("dao deleteStudent1 execute exception!"); } } //将student1表中删除的记录插入到student2中,但是此方法实现有错,因为 //id字段设置为自增长的,所以在插入记录时我们不能指定值 public void insertStudent2(){ try{ JdbcTemplate jt=this.getJdbcTemplate(); String arg[]=new String[3]; arg[0]="1"; arg[1]="xiaoming"; arg[2]="wuhan"; jt.update("insert student2(id,name,address) values(?,?,?)",arg); } catch(DataAccessException e){ System.err.println("dao insertStudent2 execute exception!"); } } }
public class StudentManagerServiceImp implements StudentManagerService{ private StudentDAO stdDAO; public void setStdDAO(StudentDAO stdDAO){ this.stdDAO=stdDAO; } //此方法为事务型的,删除student1中的记录成功且插入student2的记录也成功 //如果insertStudent2()方法执行失败,那么deleteStudent1()也应该会失败 public void bus_method() throws StudentManagerException{ try{ this.stdDAO.deleteStudent1(); this.stdDAO.insertStudent2(); } catch(DataAccessException de) System.err.println("service execute exception!"); throw new StudentManagerException();//StudentManagerException类继承DataAcce //ssException异常 } } }
public class StudentManagerAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { try{ WebApplicationContext appContext=WebApplicationContextUtils. getWebApplicationContext(this.getServlet().getServletContext()); StudentManagerService stdm=(StudentManagerService)appContext. getBean("stdServiceManager"); stdm.bus_method(); return mapping.findForward("chenggong"); } catch(StudentManagerException e){ System.err.println("action execute service exception!"); return mapping.findForward("shibai"); } } }