@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); } }