FruitShop实现:
public class TxTemplateJdbcFruitShop extends JdbcDaoSupport implements FruitShop { private PlatformTransactionManager txManager; public void setTxManager(PlatformTransactionManager txManager) { this.txManager = txManager; } @Override public boolean purchase(final int fruitId, final String userName, final int count) { TransactionTemplate txTemplate = new TransactionTemplate(txManager); final String querySql = "SELECT PRICE FROM FRUIT WHERE ID = ?"; final String upStockSql = "UPDATE FRUIT_STOCK SET STOCK = STOCK - ? WHERE ID = ?"; final String upAccountSql = "UPDATE ACCOUNT SET BALANCE = BALANCE - ? WHERE USERNAME = ?"; int retVal = txTemplate.execute(new TransactionCallback() { @Override public Integer doInTransaction(TransactionStatus status) { int price = getJdbcTemplate().queryForInt(querySql, new Object[] { fruitId }); getJdbcTemplate().update(upStockSql, new Object[] { count, fruitId }); getJdbcTemplate().update(upAccountSql, new Object[] { price * count, userName }); return 1; } }); return retVal == 1; } }
beans-fruitshop.xml文件加入:
JdbcFruitShopTest类加入:
@Resource(name = "txTemplateFruitShop") FruitShop txTemplateFruitShop; @Test public void txTemplateTest() { int fruitId = 1; String userName = "user1"; int count = 3; txTemplateFruitShop.purchase(fruitId, userName, count); }
附:
1. 可以直接注入TransactionTemplate实例,不用在代码中实例化:
去除PlatformTransactionManager属性,直接使用TransactionTemplate属性:
private TransactionTemplate txTemplate; public void setTxTemplate(TransactionTemplate txTemplate) { this.txTemplate = txTemplate; }
beans-fruitshop.xml加入:
2. execute方法可以接受TransactionCallback接口的抽象类,如TransactionCallbackWithoutResult:
txTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { ... } });
看下其实现:
接口方法中抽出一个没有返回结果的抽象方法(由用户自己实现),返回null是为了适配接口。
public abstract class TransactionCallbackWithoutResult implements TransactionCallback