mysql多数据源事务_多数据源一致性事务解决方案
spring 多數據源配置
spring 多數據源配置一般有兩種方案:
1、在spring項目啟動的時候直接配置兩個不同的數據源,不同的sessionFactory。在dao 層根據不同業務自行選擇使用哪個數據源的session來操作。
2、配置多個不同的數據源,使用一個sessionFactory,在業務邏輯使用的時候自動切換到不同的數據源,有一個種是在攔截器里面根據不同的業務現切換到不同的datasource;有的會在業務層根據業務來自動切換。但這種方案在多線程并發的時候會出現一些問題,需要使用threadlocal等技術來實現多線程競爭切換數據源的問題。
【本文暫時只討論第一種方案】
spring多事務配置主要體現在db配置這塊,配置不同的數據源和不同的session
1、一下貼出 spring-db.xml配置
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
2、dao層做了一個小的封裝,將不同的SqlSessionFactory 注入到 SessionFactory,通過BaseDao來做簡單的封裝,封裝不同庫的基本增刪改。dao實現層都集成于Basedao 這樣的話,實現可以根據自己需要來選擇不同的庫來操作不同的內容。
session工廠
package com.neo.dao;
import com.neo.entity.Entity;
public class BaseDao extends SessionFactory{
public void test1Update(Entity entity) {
this.getTest1Session().update(entity.getClass().getSimpleName()+".update", entity);
}
public void test2Update(Entity entity) {
this.getTest2Session().update(entity.getClass().getSimpleName()+".update", entity);
}
}
BaseDao
package com.neo.dao;
import com.neo.entity.Entity;
public class BaseDao extends SessionFactory{
public void test1Update(Entity entity) {
this.getTest1Session().update(entity.getClass().getSimpleName()+".update", entity);
}
public void test2Update(Entity entity) {
this.getTest2Session().update(entity.getClass().getSimpleName()+".update", entity);
}
}
以上的配置在多數據源連接,正常的增刪改都是沒有問題的,但是遇到分布式的事務是就出問題:
測試代碼:
package com.neo.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.neo.dao.UserDao;
import com.neo.dao.UserInformationsDao;
import com.neo.entity.UserEntity;
import com.neo.entity.UserInformationsEntity;
import com.neo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Resource UserDao userDao;
@Resource UserInformationsDao userInformationsDao;
@Override
@Transactional
public void updateUserinfo() {
UserEntity user=new UserEntity();
user.setId(1);
user.setUserName("李四4");
UserInformationsEntity userInfo=new UserInformationsEntity();
userInfo.setUserId(1);
userInfo.setAddress("陜西4");
userDao.updateUser(user);
userInformationsDao.updateUserInformations(userInfo);
if(true){
throw new RuntimeException("test tx ");
}
}
}
在service添加事務后,更新完畢拋出異常,test2更新進行了回滾,test1 數據更新沒有回滾。
解決方案添加分布式的事務,Atomikos和spring結合來處理。
Atomikos多數據源的配置
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
${database.test1.url}
${database.test1.username}
${database.test1.password}
${database.test2.url}
${database.test2.username}
${database.test2.password}
所有代碼請參考這里:
https://github.com/ityouknow/spring-examples
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的mysql多数据源事务_多数据源一致性事务解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 金蝶易记账功能(金蝶易记账的产品功能)
- 下一篇: mysql workbench入门_5分