springboot整合多資料來源

2021-08-20 05:21:33 字數 1391 閱讀 2259

多資料來源產生的問題:事物管理

在實際專案中,怎麼樣搭建多資料來源 區分資料來源

舉個例子 2個資料來源 test001 test002

1、分包結構

com.baidu.test001 --訪問test001資料庫

daoservice

com.baidu.test002 --訪問test002資料庫

daoservice

分布式事物的解決方案jta+automatic傳統專案

2、使用註解方式

@datasouetest001  --訪問test001資料庫

public void test001(){

@datasouetest002 --訪問test002資料庫

public void test002(){

springboot整合事務

spring事物分類(程式設計事務、宣告事務(xml方式、註解方式))

註解事務

@transactional  (兩個操作,當第二個操作發生錯誤可以回滾)

解決方式:分布式事物的解決方案

springboot+jta+atomikos

傳統專案 test01和test02資料庫資料來源註冊第三方(

atomikos)

---兩段提交協議、2pc

1、propagation_required:如果當前沒有事務,就建立乙個新事務,如果當前存在事務,就加入該事務,該設定是最常用的設定。

2、propagation_supports:支援當前事務,如果當前存在事務,就加入該事務,如果當前不存在事務,就以非事務執行。『

3、propagation_mandatory:支援當前事務,如果當前存在事務,就加入該事務,如果當前不存在事務,就丟擲異常。

4、propagation_requires_new:建立新事務,無論當前存不存在事務,都建立新事務。

5、propagation_not_supported:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。

6、propagation_never:以非事務方式執行,如果當前存在事務,則丟擲異常。

7、propagation_nested

如果當前存在事務,則在巢狀事務內執行。如果當前沒有事務,則執行與propagation_required類似的操作。

Spring Boot多資料來源配置

方案實現 新建兩個配置類 即加 configuration註解的類 乙個為primarydatasourceconfig,用來配置本專案的資料來源 另乙個叫seconddatasourceconfig,用來配置需要同步的資料來源。如下 configuration public class prima...

SpringBoot配置多資料來源

專案中用到了兩個資料庫,分別是oracle和mysql,涉及到了多資料來源問題,這裡做下記錄 官方講解 日誌jdbc配置 druid常見問題彙總 spring datasource type com.alibaba.druid.pool.druiddatasource base type com.a...

springboot多資料來源配置

多資料來源配置主要用到spring的abstractroutingdatasource,網上介紹資料也有很多,這裡主要做個備份 除了資料庫連線的依賴,spring的依賴,mybatis的依賴之外還需要 com.alibaba druid spring boot starter 1.1.5 使用com...