亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)管理

 更新時(shí)間:2022年05月30日 10:25:08   作者:把蘋果咬哭的測(cè)試筆記  
這篇文章主要為大家介紹了spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

spring JdbcTemplate數(shù)據(jù)庫事務(wù)管理

現(xiàn)在有個(gè)賬戶表,里面存著用戶金額。

如果要真正地做好轉(zhuǎn)賬的操作,就要用到事務(wù),否則當(dāng)出現(xiàn)異常后會(huì)出現(xiàn)數(shù)據(jù)不一致等問題。

try {
  // 第一步 開啟事務(wù)
  // 第二步 進(jìn)行業(yè)務(wù)操作
  // 第三步 沒有發(fā)生異常,提交事務(wù)
} catch(){
  // 第四步 發(fā)生異常,事務(wù)回滾
}

一、spring 中的事務(wù)管理

通常,把事務(wù)加在 service 層(業(yè)務(wù)邏輯層)。

而在 spring 中管理事務(wù)可以有 2 種方式實(shí)現(xiàn):

  • 編程式管理:就像上面?zhèn)未a那樣,這種使用起來不方便。
  • 聲明式管理:通過配置方式實(shí)現(xiàn),推薦使用。其中,可以基于 XML 方式進(jìn)行配置,也可以基于注解,顯然后者更方便。

在 spring 中進(jìn)行聲明式事務(wù)管理,底層使用的是 AOP 原理。

二、spring 事務(wù)管理 API

spring 提供了一個(gè)接口 PlatformTransactionManager ,代表事務(wù)管理器。此接口針對(duì)不同的框架提供不同的實(shí)現(xiàn)類。

利用idea工具,展開結(jié)構(gòu),使用 jdbcTemplate 用到的是 DataSourceTransactionManager 。

三、使用事務(wù)管理

1. 配置文件

創(chuàng)建事務(wù)管理器。

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

開啟事務(wù)注解,使用名稱空間 tx。

<!--開啟事務(wù)注釋-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

2. 類上添加事務(wù)注解

注解 @Transactional 可以加在 service 類上,也可以加到方法上:

加到類上,表示類下所有的方法都添加了事務(wù)。加到方法,表示只有該方法添加事務(wù)。

@Service
@Transactional
public class UserService {
    @Autowired
    private UserDao userDao;
    // 轉(zhuǎn)賬方法
    public void accountMoney() {
        // 大周 少 100
        userDao.reduceMoney();
        // 模擬異常
        int i = 1/0;
        // 小毛 加 100
        userDao.addMoney();
    }
}

接口實(shí)現(xiàn)類 UserDaoImpl 。

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addMoney() {
        String sql = "update t_account set money=money+? where username=?";
        jdbcTemplate.update(sql, 100, "小毛");
    }
    @Override
    public void reduceMoney() {
        String sql = "update t_account set money=money-? where username=?";
        jdbcTemplate.update(sql, 100, "大周");
    }
}

到測(cè)試類里執(zhí)行一下:

public class TestTrans {
    @Test
    public void testJdbc() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.accountMoney();
    }
}

注意,上面的 service 里我手動(dòng)添加了異常,所以執(zhí)行后,事務(wù)應(yīng)該是要回滾操作,2 個(gè)人的金額仍然各是 1000 。

八月 07, 2021 10:39:57 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
java.lang.ArithmeticException: / by zero

刷新數(shù)據(jù)表。

現(xiàn)在我去掉 service 類中的異常 int i = 1/0;,重新執(zhí)行測(cè)試方法:

八月 07, 2021 10:47:01 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Process finished with exit code 0

刷新數(shù)據(jù)表。

結(jié)果正確。

以上就是spring學(xué)習(xí)JdbcTemplate數(shù)據(jù)庫事務(wù)管理的詳細(xì)內(nèi)容,更多關(guān)于spring JdbcTemplate數(shù)據(jù)庫事務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論