<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot事務(wù)注解詳解

          共 3922字,需瀏覽 8分鐘

           ·

          2020-08-10 21:26

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)


          ? 作?|?Kero小柯

          來源 |?cnblogs.com/kesimin/p/9546225.html

          66套java從入門到精通實(shí)戰(zhàn)課程分享

          精品帖子大匯總

          @Transactional

          spring 事務(wù)注解

          1.簡單開啟事務(wù)管理

          @EnableTransactionManagement?// 啟注解事務(wù)管理,等同于xml配置方式的 


          2.事務(wù)注解詳解

          默認(rèn)遇到throw new RuntimeException(“…”);會(huì)回滾?
          需要捕獲的throw new Exception(“…”);不會(huì)回滾

          指定回滾

          @Transactional(rollbackFor=Exception.class)
          ????public?void?methodName()?{
          ???????// 不會(huì)回滾
          ???????throw?new?Exception("...");
          ????}


          指定不回滾

          @Transactional(noRollbackFor=Exception.class)
          ????public?ItimDaoImpl getItemDaoImpl()?{
          ????????// 會(huì)回滾
          ????????throw?new?RuntimeException("注釋");
          ????}


          如果有事務(wù),那么加入事務(wù),沒有的話新建一個(gè)(不寫的情況下)

          @Transactional(propagation=Propagation.REQUIRED)


          容器不為這個(gè)方法開啟事務(wù)

          @Transactional(propagation=Propagation.NOT_SUPPORTED)


          readOnly=true只讀,不能更新,刪除

          @Transactional (propagation = Propagation.REQUIRED,readOnly=true)


          設(shè)置超時(shí)時(shí)間

          @Transactional (propagation = Propagation.REQUIRED,timeout=30)


          設(shè)置數(shù)據(jù)庫隔離級(jí)別

          @Transactional (propagation = Propagation.REQUIRED,isolation=Isolation.DEFAULT)


          3.指定事務(wù)管理器

          spring Boot 使用事務(wù)非常簡單,首先使用注解 @EnableTransactionManagement 開啟事務(wù)支持后,然后在訪問數(shù)據(jù)庫的Service方法上添加注解 @Transactional 便可。

          關(guān)于事務(wù)管理器,不管是JPA還是JDBC等都實(shí)現(xiàn)自接口 PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依賴,框架會(huì)默認(rèn)注入 DataSourceTransactionManager 實(shí)例。如果你添加的是 spring-boot-starter-data-jpa 依賴,框架會(huì)默認(rèn)注入 JpaTransactionManager 實(shí)例。

          你可以在啟動(dòng)類中添加如下方法,Debug測試,就能知道自動(dòng)注入的是 PlatformTransactionManager 接口的哪個(gè)實(shí)現(xiàn)類。

          3.1 打印項(xiàng)目事務(wù)管理器


          @EnableTransactionManagement?// 啟注解事務(wù)管理,等同于xml配置方式的 
          @SpringBootApplication
          public class ProfiledemoApplication {

          ????@Bean
          ????public Object testBean(PlatformTransactionManager platformTransactionManager){
          ????????System.out.println(">>>>>>>>>>"?+ platformTransactionManager.getClass().getName());
          ????????return?new?Object();
          ????}

          ????public?static?void?main(String[] args) {
          ????????SpringApplication.run(ProfiledemoApplication.class, args);
          ????}
          }


          這些SpringBoot為我們自動(dòng)做了,這些對(duì)我們并不透明,如果你項(xiàng)目做的比較大,添加的持久化依賴比較多,我們還是會(huì)選擇人為的指定使用哪個(gè)事務(wù)管理器。?
          代碼如下:

          3.2 指定事務(wù)管理器


          @EnableTransactionManagement
          @SpringBootApplication
          public?class?ProfiledemoApplication?{

          ????// 其中 dataSource 框架會(huì)自動(dòng)為我們注入
          ????@Bean
          ????public?PlatformTransactionManager txManager(DataSource dataSource)?{
          ????????return?new?DataSourceTransactionManager(dataSource);
          ????}

          ????@Bean
          ????public?Object testBean(PlatformTransactionManager platformTransactionManager)?{
          ????????System.out.println(">>>>>>>>>>"?+ platformTransactionManager.getClass().getName());
          ????????return?new?Object();
          ????}

          ????public?static?void?main(String[] args)?{
          ????????SpringApplication.run(ProfiledemoApplication.class, args);
          ????}
          }


          在Spring容器中,我們手工注解@Bean 將被優(yōu)先加載,框架不會(huì)重新實(shí)例化其他的 PlatformTransactionManager 實(shí)現(xiàn)類。

          然后在Service中,被 @Transactional 注解的方法,將支持事務(wù)。如果注解在類上,則整個(gè)類的所有方法都默認(rèn)支持事務(wù)。

          對(duì)于同一個(gè)工程中存在多個(gè)事務(wù)管理器要怎么處理,請(qǐng)看下面的實(shí)例,具體說明請(qǐng)看代碼中的注釋。

          3.1 使用指定的事務(wù)管理器


          @EnableTransactionManagement?// 開啟注解事務(wù)管理,等同于xml配置文件中的 
          @SpringBootApplication
          public?class?ProfiledemoApplication?implements?TransactionManagementConfigurer?{

          ????@Resource(name="txManager2")
          ????private?PlatformTransactionManager txManager2;

          ????// 創(chuàng)建事務(wù)管理器1
          ????@Bean(name = "txManager1")
          ????public?PlatformTransactionManager txManager(DataSource dataSource)?{
          ????????return?new?DataSourceTransactionManager(dataSource);
          ????}

          ????// 創(chuàng)建事務(wù)管理器2
          ????@Bean(name = "txManager2")
          ????public?PlatformTransactionManager txManager2(EntityManagerFactory factory)?{
          ????????return?new?JpaTransactionManager(factory);
          ????}

          ????// 實(shí)現(xiàn)接口 TransactionManagementConfigurer 方法,其返回值代表在擁有多個(gè)事務(wù)管理器的情況下默認(rèn)使用的事務(wù)管理器
          ????@Override
          ????public?PlatformTransactionManager annotationDrivenTransactionManager()?{
          ????????return?txManager2;
          ????}

          ????public?static?void?main(String[] args)?{
          ????????SpringApplication.run(ProfiledemoApplication.class, args);
          ????}

          }

          @Component
          public?class?DevSendMessage?implements?SendMessage?{

          ????// 使用value具體指定使用哪個(gè)事務(wù)管理器
          ????@Transactional(value="txManager1")
          ????@Override
          ????public?void?send()?{
          ????????System.out.println(">>>>>>>>Dev Send()<<<<<<<<");
          ????????send2();
          ????}

          ????// 在存在多個(gè)事務(wù)管理器的情況下,如果使用value具體指定
          ????// 則默認(rèn)使用方法 annotationDrivenTransactionManager() 返回的事務(wù)管理器
          ????@Transactional
          ????public?void?send2()?{
          ????????System.out.println(">>>>>>>>Dev Send2()<<<<<<<<");
          ????}

          }



          粉絲福利:108本java從入門到大神精選電子書領(lǐng)取

          ???

          ?長按上方二維碼?2 秒
          回復(fù)「1234」即可獲取資料以及
          可以進(jìn)入java1234官方微信群



          感謝點(diǎn)贊支持下哈?

          瀏覽 46
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美一级爱爱 | 草热视频在线 | 视频一区二区77在线 | 国产老妈操 | 久色 |