13. Ex 2: Programmatic Control Connection connection = null ; Statement statement = null ; try { Class. forName ( "driverClass" ); connection = DriverManager.getConnection( "url" , "user" , "pass" ); connection.setAutoCommit( false ); statement = connection.createStatement(); // Execute queries statement.executeUpdate( "UPDATE Table1 SET Value = 1 WHERE Name = 'foo'" ); statement.executeUpdate( "UPDATE Table2 SET Value = 2 WHERE Name = 'bar'" ); connection.commit(); } catch (SQLException ex) { if ( null != connection) { connection.rollback(); } } finally { if ( null != statement) { statement.close(); } if ( null != connection) { connection.close(); } }
14. Ex 3: Incorrect Demarcation @ Repository public class UserRepository { @ Transactional public void save(User user) { ... } } @ Component public class UserService { @ Autowired private UserRepository repo ; public void disableUsers( List<User> usersList) { for (User user : usersList) { repo .save(user); } } }
15. Ex 4: Batch processing @ Component public class LongRunningJob { @ Autowired private RecordRepository repository ; @ Transactional public void updateRecords() { for ( int i = 1; i < 100000; i++) { Record rec = repository .getById(i); repository .updateTimeStamp(rec, new Date()); } } }
46. Declarative Transaction Model with ASPECT (1) public class AccountServiceImpl implements AccountService { @ Transactional public boolean transferAmount( final Amount amount, final Account debetAcc, final Account creditAcc) { ..... } }
47. Declarative Transaction Model with ASPECT (2) @ Aspect public class TransactionAspect { private TransactionService transactionService = new TransactionServiceNull(); @ Pointcut( "execution(@org.spring...Transactional * *(..))" ) public void transactionalMethod() {} public void setTransactionService( final TransactionService transactionService) { this . transactionService = transactionService; } .... to be continued ....
48. Declarative Transaction Model with ASPECT (3) ... continued ... @ Before( "transactionalMethod()" ) public void beforeTransactionalMethod(JoinPoint joinPoint) { transactionService.beginTransaction(); } @ AfterReturning( "transactionalMethod()" ) public void afterTransactionalMethod(JoinPoint joinPoint) { transactionService .commit(); } @ AfterThrowing(pointcut = "transactionalMethod()" , throwing = "e" ) public void afterThrowingFromTransactionalMethod(JoinPoint joinPoint, RuntimeException e) { transactionService.rollback(); } }
49. Declarative Transaction Model with Spring Annotations <context:component-scan base-package="com........" /> <tx:annotation-driven /> <bean id="txnMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> @ Component public class UsersService { @ Transactional(propagation = Propagation.REQUIRED) public void modifyUser(User user) { ... } }
82. Server Delegate Owner Pattern Example @ Component @ Transactional public class UpdateRateCommand { private CurrencyRateSystem crs ; private RateData data ; public UpdateRateCommand(RateData data) { this . data = data; } public void execute() { Currency currFrom = data .getFrom(); Currency currTo = data .getTo(); Rate rate = crs .rate(currFrom, currTo, new Date()); data .setRate(rate); } }