日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

本地事務(wù)系列之二:使用PlatformTransactionMan

系統(tǒng) 2266 0
Spring的事務(wù)管理器接口:
    
public interface PlatformTransactionManager {

	TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; // 獲取事務(wù)狀態(tài)

	void commit(TransactionStatus status) throws TransactionException; // 提交事務(wù)

	void rollback(TransactionStatus status) throws TransactionException; // 回滾事務(wù)
}

  


Spring提供的實(shí)現(xiàn)
本地事務(wù)系列之二:使用PlatformTransactionManager

這里是單數(shù)據(jù)源,可以使用DataSourceTransactionManager:
FruitShop實(shí)現(xiàn)類(lèi):
    
public class TransactionalApiJdbcFruitShop extends JdbcDaoSupport implements FruitShop {
	private PlatformTransactionManager txManager;

	public void setTxManager(PlatformTransactionManager txManager) {
		this.txManager = txManager;
	}

	@Override
	public boolean purchase(int fruitId, String userName, int count) {
		TransactionDefinition definition = new DefaultTransactionDefinition();
		TransactionStatus status = txManager.getTransaction(definition);

		String querySql = "SELECT PRICE FROM FRUIT WHERE ID = ?";
		String upStockSql = "UPDATE FRUIT_STOCK SET STOCK = STOCK - ? WHERE ID = ?";
		String upAccountSql = "UPDATE ACCOUNT SET BALANCE = BALANCE - ? WHERE USERNAME = ?";

		try {
			int price = getJdbcTemplate().queryForInt(querySql, new Object[] { fruitId });
			getJdbcTemplate().update(upStockSql, new Object[] { count, fruitId });
			getJdbcTemplate().update(upAccountSql, new Object[] { price * count, userName });
			txManager.commit(status);
		} catch (DataAccessException e) {
			txManager.rollback(status);
			throw e;
		} catch (TransactionException e) {
			txManager.rollback(status);
			throw e;
		}
		return true;
	}
}

  


beans-fruitshop.xml文件:
    
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8" />
  <property name="username" value="spring" />
  <property name="password" value="123456" />
</bean>

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>

<bean id="txApiFruitShop" class="com.john.tx.service.impl.TransactionalApiJdbcFruitShop">
  <property name="dataSource" ref="dataSource" /><!--JdbcDaoSupport的子類(lèi)需要指定dataSource-->
  <property name="txManager" ref="txManager" />
</bean>

  


測(cè)試:
    
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/beans-fruitshop.xml" })
public class JdbcFruitShopTest {
	@Resource(name = "txApiFruitShop")
	FruitShop txApiFruitShop;

	@Test
	public void txApiTest() {
		int fruitId = 1;
		String userName = "user1";
		int count = 3;
		txApiFruitShop.purchase(fruitId, userName, count);
	}
}

  

本地事務(wù)系列之二:使用PlatformTransactionManager


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 苍梧县| 汉寿县| 博罗县| 大同县| 澄迈县| 治县。| 兴宁市| 兴文县| 望谟县| 姚安县| 集安市| 化隆| 偃师市| 东城区| 攀枝花市| 彭泽县| 沾益县| 新化县| 称多县| 桦川县| 当雄县| 内丘县| 沛县| 石狮市| 陇南市| 临洮县| 西吉县| 巨野县| 丰顺县| 茶陵县| 泰州市| 翼城县| 长子县| 岑巩县| 阜新| 咸宁市| 新沂市| 西贡区| 舞钢市| 哈巴河县| 英吉沙县|