Skip to content

Commit

Permalink
- 增强在多个事务管理器, 同时存在时的兼容性
Browse files Browse the repository at this point in the history
  • Loading branch information
xutengx committed Jul 7, 2023
1 parent 00e5bbb commit 92a4568
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void begin() {
try {
DataSource dataSource = getRealDataSource(true);
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
setAutoCommit(connection, false);
localThreadTransactionConnection.set(connection);
} catch (SQLException e) {
throw new SQLRuntimeException(e.getMessage(), e);
Expand All @@ -116,7 +116,7 @@ public void commit() {
if (localThreadTransactionSavepointLinkedList.get().isEmpty()) {
try {
connection.commit();
connection.setAutoCommit(true);
setAutoCommit(connection, true);
} catch (SQLException e) {
throw new SQLRuntimeException(e.getMessage(), e);
} finally {
Expand All @@ -137,7 +137,7 @@ public void rollBack() {
if (localThreadTransactionSavepointLinkedList.get().isEmpty()) {
try {
connection.rollback();
connection.setAutoCommit(true);
setAutoCommit(connection, true);
} catch (SQLException e) {
throw new SQLRuntimeException(e.getMessage(), e);
} finally {
Expand Down Expand Up @@ -358,4 +358,16 @@ public List<DataSource> getSlaveDataSourceList() {
public Container getContainer() {
return container;
}

/**
* 设置自动提交
* @param connection 数据库连接
* @param flag y/n
* @throws SQLException sql异常
*/
void setAutoCommit(Connection connection, boolean flag) throws SQLException {
if (!connection.isClosed()) {
connection.setAutoCommit(flag);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gaarason.database.spring.boot.starter.mybatis.test;

public class TestException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gaarason.database.spring.boot.starter.mybatis.test.service;

import gaarason.database.eloquent.GeneralModel;
import gaarason.database.spring.boot.starter.mybatis.test.TestException;
import gaarason.database.spring.boot.starter.mybatis.test.mybatis.mapper.StudentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TService {

@Resource
private StudentMapper studentMapper;

@Resource
private GeneralModel generalModel;

@Transactional( transactionManager="gaarasonTransactionManager", rollbackFor = TestException.class)
public void doSomething(int id, int age) throws TestException {

studentMapper.updateAgeById(id, age);

Object ageBySelect = generalModel.newQuery().from("student").where("id", id).firstOrFail().toMap().get("age");

System.out.println("通过mybatis更新后, age : " + ageBySelect);

throw new TestException();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package gaarason.database.spring.boot.starter.mybatis.test;

import gaarason.database.eloquent.GeneralModel;
import gaarason.database.spring.boot.starter.mybatis.test.mybatis.mapper.StudentMapper;
import gaarason.database.spring.boot.starter.mybatis.test.service.TService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

Expand All @@ -19,32 +17,13 @@ public class MybatisApplicationTests {
private GeneralModel generalModel;

@Resource
@Lazy
MybatisApplicationTests mybatisApplicationTests;

@Resource
private StudentMapper studentMapper;

@Transactional
public void doSomething(int id, int age) {

studentMapper.updateAgeById(id, age);

Object ageBySelect = generalModel.newQuery().from("student").findOrFail(id).toMap().get("age");

System.out.println("通过mybatis更新后, age : " + ageBySelect);

Assertions.assertEquals(age, ageBySelect);

throw new RuntimeException();
}

private TService tService;

@Test
void test1() {
int id = 1;

generalModel.newQuery().from("student").where("id", id).data("age",6).update();
generalModel.newQuery().from("student").where("id", id).data("age", 6).update();

Object age = generalModel.newQuery().from("student").where("id", id).firstOrFail().toMap().get("age");

Expand All @@ -54,10 +33,9 @@ void test1() {
int newAge = Integer.parseInt(String.valueOf(age)) + 99;

try {
mybatisApplicationTests.doSomething(id, newAge);
} catch (Throwable ignore) {
System.out.println("异常抛出");

tService.doSomething(id, newAge);
} catch (TestException e) {
System.out.println("意料中的异常抛出");
}
Object ageBySelect = generalModel.newQuery().from("student").where("id", id).firstOrFail().toMap().get("age");

Expand Down
3 changes: 3 additions & 0 deletions document/version.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Eloquent ORM for Java

## 版本升级指引

### 4.8.2
- 增强在多个事务管理器, 同时存在时的兼容性

### 4.8.1
-`Builder`中, 增加`count`对于非`group`下的自定义`select`的支持, 同步影响`paginate`等分页等函数

Expand Down

0 comments on commit 92a4568

Please sign in to comment.