@Test public void testNeverTransactionalShouldPassThrough() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNevetTrans_NoTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method - no transactional so address is created and // commited and then exception and rollback on person assertEquals(new Integer(0), addressVol); assertEquals(new Integer(1), personVol); // record created by createRoleNeverTransaction method - no transactional so role is commited // - as a first line of execution inside // customerService.createCustomerComplexInsideNevetTrans_NoTransOutside method assertEquals(new Integer(1), roleVol); } }
/** * In this example there is transaction during execution createCustomerComplexInsideSupport_Trans, * exception is thrown during customer creation */ @Test public void shouldRollbackAllCreationRoleAddressAndPerson() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception - first roles are created customerService.createCustomerComplexInsideSupport_WithTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method assertEquals(new Integer(0), addressVol); assertEquals(new Integer(0), personVol); // record created by createRoleTransactionSupport method assertEquals(new Integer(0), roleVol); } }
/** * Exception is thrown during customer creation, no transation and no support transaction inside * role creation */ @Test public void shouldCreatePersonAndRolesButNoAddress() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNotSupported_NoTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method - no transaction so executed independently but in // address insertion no because of exception - in this case is rollback assertEquals(new Integer(0), addressVol); assertEquals(new Integer(1), personVol); // record created by createRoleTransactionNotSupported method - no transaction so commited assertEquals(new Integer(1), roleVol); } }
/** In this case */ @Test @Transactional public void shouldCreateNewTransactionForCreateRoles() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is OK, but wrongRole has too long name customerService.createCustomerComplexNewRequired_NoTransOutside( okCustomer, okRole, wrongRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // address and person are commited independently (because of no transactional) but commited assertEquals(new Integer(1), addressVol); assertEquals(new Integer(1), personVol); // here second role was to long and exception was thrown but bacause of transaction required // (and created) all steps in this method are rolledback assertEquals(new Integer(0), roleVol); } }
/** * Exception is thrown during customer creation, no transation and no support transaction inside * role creation */ @Test public void shouldCreatePersonAndRolesButNoAddress2() { customerService.cleanDatabase(); roleService.cleanDatabase(); try { // customer is wrong - it is going to be exception customerService.createCustomerComplexInsideNotSupported_WithTransOutside( customerWithTooLongCity, okRole); } catch (Exception e) { Integer addressVol = customerDAO.getAddressVol(); Integer personVol = customerDAO.getPersonVol(); Integer roleVol = roleDAO.getRolesVol(); // records created by insertCustomer method - transactional so rollback all inserts in this // method, rollback because of problem during inserting address assertEquals(new Integer(0), addressVol); assertEquals(new Integer(0), personVol); // record created by createRoleTransactionNotSupported method - transaction is not supported // even if outside method is transactional - so in this example role record is commited assertEquals(new Integer(1), roleVol); } }
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { RoleForm rf = (RoleForm) form; Role role = new Role(); PropertyUtils.copyProperties(role, rf); Connection conn = null; System.out.println("role delete 000000"); try { // DataSource dts = getDataSource(request); conn = DBManager.getConnection(); // check if the role is in use conn.setAutoCommit(false); UserRoleDAO userRoleDao = new UserRoleDAO(); Collection userRoleCol = userRoleDao.find(conn, role); System.out.println("role delete 111111111"); if (userRoleCol != null && userRoleCol.size() > 0) { System.out.println("role delete 222222222"); ControlledError ctlErr = new ControlledError(); ctlErr.setErrorTitle("删除角色错误"); ctlErr.setErrorBody("角色正在使用中,不能删除"); request.setAttribute(com.magic.crm.util.Constants.ERROR_KEY, ctlErr); return mapping.findForward("controlledError"); } RoleDAO dao = new RoleDAO(); RoleRightDAO roleRightDao = new RoleRightDAO(); roleRightDao.delete(conn, role); System.out.println("role delete 3333333"); dao.delete(conn, role); System.out.println("role delete 4444444"); conn.commit(); return mapping.findForward("success"); } catch (SQLException se) { throw new ServletException(se); } finally { try { conn.close(); } catch (SQLException sqe) { throw new ServletException(sqe); } } }
public DAOResult addRole(RoleDTO dto, ArrayList<Integer> permissionIDList) { RoleDAO dao = new RoleDAO(); return dao.addRole(dto, permissionIDList); }
public RoleDTO getRoleDTO(int roleID) { RoleDAO dao = new RoleDAO(); return dao.getRoleDTO(roleID); }
public ArrayList<RoleDTO> getAllRoles(String roleName) { RoleDAO dao = new RoleDAO(); return dao.getAllRoles(roleName); }