public void init(ServletConfig config) throws ServletException { super.init(config); ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()); dao = (DerbyDao) ctx.getBean("personDao"); txManager = (PlatformTransactionManager) ctx.getBean("transactionManager"); dao.createTable(); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setName("cargotest"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = txManager.getTransaction(def); try { dao.create("Adrian", "Cole"); } catch (RuntimeException ex) { txManager.rollback(status); throw ex; } txManager.commit(status); if (dao.selectAll().size() != 1) throw new RuntimeException("Commit didn't work"); PrintWriter out = response.getWriter(); out.print("all good!"); out.close(); }
public void destroy() { dao.dropTable(); super.destroy(); }