|
| 1 | +package com.yiqiniu.easytrans.demos.order.impl; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.Date; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.concurrent.Future; |
| 8 | + |
| 9 | +import javax.annotation.Resource; |
| 10 | + |
| 11 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 12 | +import org.springframework.jdbc.core.PreparedStatementCreator; |
| 13 | +import org.springframework.jdbc.support.GeneratedKeyHolder; |
| 14 | +import org.springframework.jdbc.support.KeyHolder; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | +import org.springframework.transaction.annotation.Transactional; |
| 17 | + |
| 18 | +import com.yiqiniu.easytrans.core.EasyTransFacade; |
| 19 | +import com.yiqiniu.easytrans.demos.wallet.api.vo.WalletPayVO.WalletPayRequestVO; |
| 20 | +import com.yiqiniu.easytrans.demos.wallet.api.vo.WalletPayVO.WalletPayResponseVO; |
| 21 | + |
| 22 | +@Component |
| 23 | +public class OrderService { |
| 24 | + |
| 25 | + public static final String BUSINESS_CODE = "buySth"; |
| 26 | + |
| 27 | + |
| 28 | + @Resource |
| 29 | + private EasyTransFacade transaction; |
| 30 | + @Resource |
| 31 | + private JdbcTemplate jdbcTemplate; |
| 32 | + |
| 33 | + |
| 34 | + @Transactional |
| 35 | + public int buySomething(int userId,long money){ |
| 36 | + |
| 37 | + /** |
| 38 | + * finish the local transaction first, in order for performance and generated of business id |
| 39 | + * |
| 40 | + * 优先完成本地事务以 1. 提高性能(减少异常时回滚消耗)2. 生成事务内交易ID |
| 41 | + */ |
| 42 | + Integer id = saveOrderRecord(jdbcTemplate,userId,money); |
| 43 | + |
| 44 | + /** |
| 45 | + * annotation the global transactionId, it is combined of appId + bussiness_code + id |
| 46 | + * it can be omit,then framework will use "default" as businessCode, and will generate an id |
| 47 | + * but it will make us harder to associate an global transaction to an concrete business |
| 48 | + * |
| 49 | + * 声明全局事务ID,其由appId,业务代码,业务代码内ID构成 |
| 50 | + * 本代码可以省略,框架会自动生成ID及使用一个默认的业务代码 |
| 51 | + * 但这样的话,会使得我们难以把全局事务ID与一个具体的事务关联起来 |
| 52 | + */ |
| 53 | + transaction.startEasyTrans(BUSINESS_CODE, id); |
| 54 | + |
| 55 | + /** |
| 56 | + * call remote service to deduct money, it's a TCC service, |
| 57 | + * framework will maintains the eventually constancy based on the final transaction status of method buySomething |
| 58 | + * if you think introducing object transaction(EasyTransFacade) is an unacceptable coupling |
| 59 | + * then you can refer to another demo(interfacecall) in the demos directory, it will show you how to execute transaction by user defined interface |
| 60 | + * |
| 61 | + * 调用远程服务扣除所需的钱,这个远程服务实现了TCC接口, |
| 62 | + * 框架会根据buySomething方法的事务结果来维护远程服务的最终一致性 |
| 63 | + * 如果你认为引入transaction(EasyTransFacde)是一个无法接受的耦合 |
| 64 | + * 那么你可以参考在demos目录下另外一个样例(interfacecall),它会告诉你如何用用户自定义的接口来执行远程事务 |
| 65 | + */ |
| 66 | + WalletPayRequestVO deductRequest = new WalletPayRequestVO(); |
| 67 | + deductRequest.setUserId(userId); |
| 68 | + deductRequest.setPayAmount(money); |
| 69 | + /** |
| 70 | + * return future for the benefits of performance enhance(batch write execute log and batch execute RPC) |
| 71 | + * 返回future是为了能方便的优化性能(批量写日志及批量调用RPC) |
| 72 | + */ |
| 73 | + @SuppressWarnings("unused") |
| 74 | + Future<WalletPayResponseVO> deductFuture = transaction.execute(deductRequest); |
| 75 | + |
| 76 | + /** |
| 77 | + * you can add more types of transaction calls here, e.g. TCC,reliable message, SAGA-TCC and so on |
| 78 | + * framework will maintains the eventually consistent |
| 79 | + * |
| 80 | + * 你可以额外加入其它类型的事务,如TCC,可靠消息,SAGA-TCC等等 |
| 81 | + * 框架会维护全局事务的最终一致性 |
| 82 | + */ |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * we can get remote service result to determine whether to commit this transaction |
| 87 | + * |
| 88 | + * 可以获取远程返回的结果用以判断是继续往下走 还是 抛异常结束 |
| 89 | + * |
| 90 | + * deductFuture.get(); |
| 91 | + */ |
| 92 | + |
| 93 | + return id; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + private Integer saveOrderRecord(JdbcTemplate jdbcTemplate, final int userId, final long money) { |
| 98 | + |
| 99 | + final String INSERT_SQL = "INSERT INTO `order` (`order_id`, `user_id`, `money`, `create_time`) VALUES (NULL, ?, ?, ?);"; |
| 100 | + KeyHolder keyHolder = new GeneratedKeyHolder(); |
| 101 | + jdbcTemplate.update( |
| 102 | + new PreparedStatementCreator() { |
| 103 | + @Override |
| 104 | + public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { |
| 105 | + PreparedStatement ps = |
| 106 | + connection.prepareStatement(INSERT_SQL, new String[] {"id"}); |
| 107 | + ps.setInt(1, userId); |
| 108 | + ps.setLong(2, money); |
| 109 | + ps.setDate(3, new Date(System.currentTimeMillis())); |
| 110 | + return ps; |
| 111 | + } |
| 112 | + }, |
| 113 | + keyHolder); |
| 114 | + |
| 115 | + return keyHolder.getKey().intValue(); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +} |
0 commit comments