Spring编程式事务

Spring编程式事务

 

DataSource txDataSource = jdbcTemplate.getDataSource();
DataSourceTransactionManager txManager =  new DataSourceTransactionManager(txDataSource);    
final TransactionTemplate txTemplate = new TransactionTemplate(txManager );   
txTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
txTemplate.execute(new TransactionCallback(){
     public Object doInTransaction(TransactionStatus status) {
          try {
             //业务操作
             String sql = "INSERT INTO T_TEST (ID,STATUS) VALUES (?,?)";
             jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                 @Override
                 public void setValues(PreparedStatement ps, int i) throws SQLException {
                        DTO dsd = list.get(i);
                        ps.setString(1, dsd.getId());
                        ps.setInt(2, dsd.getStatus());
                 }
                 @Override
                 public int getBatchSize() {
                       return list.size();
                 }
              });
           } catch (Exception e) { //可使用具体业务异常代替
            	status.setRollbackOnly();
           }
           return null;
      }
});

 

http://blog.csdn.net/huazaichang/article/details/9158549

 

猜你喜欢

转载自singhoo.iteye.com/blog/2264154