mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
add FlexTransactionManager; close #I6V7XN
This commit is contained in:
parent
a0ac8d5eb8
commit
acf1106bee
@ -124,20 +124,20 @@ public class TransactionalManager {
|
||||
}
|
||||
|
||||
private static Boolean execNewTransactional(Supplier<Boolean> supplier) {
|
||||
String xid = TransactionalManager.startTransactional();
|
||||
String xid = startTransactional();
|
||||
Boolean success = false;
|
||||
boolean rollbacked = false;
|
||||
try {
|
||||
success = supplier.get();
|
||||
} catch (Exception e) {
|
||||
rollbacked = true;
|
||||
TransactionalManager.rollback(xid);
|
||||
rollback(xid);
|
||||
throw new TransactionException(e.getMessage(), e);
|
||||
} finally {
|
||||
if (success != null && success) {
|
||||
TransactionalManager.commit(xid);
|
||||
commit(xid);
|
||||
} else if (!rollbacked) {
|
||||
TransactionalManager.rollback(xid);
|
||||
rollback(xid);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
|
||||
@ -17,10 +17,13 @@ package com.mybatisflex.spring.boot;
|
||||
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.row.Db;
|
||||
import com.mybatisflex.spring.FlexTransactionManager;
|
||||
import com.mybatisflex.spring.SpringRowSessionManager;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.TransactionManager;
|
||||
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@ -28,7 +31,7 @@ import java.util.logging.Logger;
|
||||
@ConditionalOnClass(Db.class)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureAfter({MybatisFlexAutoConfiguration.class})
|
||||
public class DbAutoConfiguration {
|
||||
public class DbAutoConfiguration implements TransactionManagementConfigurer {
|
||||
|
||||
public DbAutoConfiguration() {
|
||||
FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
|
||||
@ -39,4 +42,13 @@ public class DbAutoConfiguration {
|
||||
Db.invoker().setRowSessionManager(new SpringRowSessionManager());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TransactionManager annotationDrivenTransactionManager() {
|
||||
return new FlexTransactionManager();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.mybatisflex.spring;
|
||||
|
||||
import com.mybatisflex.core.transaction.TransactionContext;
|
||||
import com.mybatisflex.core.transaction.TransactionalManager;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
|
||||
import org.springframework.transaction.support.DefaultTransactionStatus;
|
||||
|
||||
public class FlexTransactionManager extends AbstractPlatformTransactionManager {
|
||||
|
||||
@Override
|
||||
protected Object doGetTransaction() throws TransactionException {
|
||||
return new TransactionObject(TransactionContext.getXID());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isExistingTransaction(Object transaction) throws TransactionException {
|
||||
TransactionObject transactionObject = (TransactionObject) transaction;
|
||||
return StringUtil.isNotBlank(transactionObject.prevXid);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doSuspend(Object transaction) throws TransactionException {
|
||||
TransactionContext.release();
|
||||
TransactionObject transactionObject = (TransactionObject) transaction;
|
||||
return transactionObject.prevXid;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doResume(Object transaction, Object suspendedResources) throws TransactionException {
|
||||
String xid = (String) suspendedResources;
|
||||
TransactionContext.hold(xid);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
|
||||
TransactionObject transactionObject = (TransactionObject) transaction;
|
||||
transactionObject.currentXid = TransactionalManager.startTransactional();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
|
||||
TransactionObject transactionObject = (TransactionObject) status.getTransaction();
|
||||
TransactionalManager.commit(transactionObject.currentXid);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
|
||||
TransactionObject transactionObject = (TransactionObject) status.getTransaction();
|
||||
TransactionalManager.rollback(transactionObject.currentXid);
|
||||
}
|
||||
|
||||
|
||||
static class TransactionObject {
|
||||
|
||||
private final String prevXid;
|
||||
private String currentXid;
|
||||
|
||||
public TransactionObject(String prevXid) {
|
||||
this.prevXid = prevXid;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -19,9 +19,11 @@ import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.test.mapper.AccountMapper;
|
||||
import com.mybatisflex.test.model.Account;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.mybatisflex.test.service.AccountService;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@ -29,10 +31,14 @@ import java.util.stream.Collectors;
|
||||
@RestController
|
||||
public class AccountController {
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
AccountMapper accountMapper;
|
||||
|
||||
|
||||
@Resource
|
||||
AccountService accountService;
|
||||
|
||||
|
||||
@PostMapping("/account/add")
|
||||
String add(@RequestBody Account account){
|
||||
accountMapper.insert(account);
|
||||
@ -41,11 +47,35 @@ public class AccountController {
|
||||
|
||||
|
||||
@GetMapping("/account/{id}")
|
||||
Account selectOne(@PathVariable("id") Long id) {
|
||||
@Transactional
|
||||
public Account selectOne(@PathVariable("id") Long id) {
|
||||
|
||||
Account account = new Account();
|
||||
account.setId(1L);
|
||||
account.setUserName("heihei");
|
||||
accountMapper.update(account);
|
||||
|
||||
|
||||
accountService.update2();
|
||||
|
||||
// update2();
|
||||
|
||||
|
||||
return accountMapper.selectOneById(id);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
// @GetMapping("/account/uuu")
|
||||
// public void update2(){
|
||||
// Account account = new Account();
|
||||
// account.setId(2L);
|
||||
// account.setUserName("haha");
|
||||
// accountMapper.update(account);
|
||||
// }
|
||||
|
||||
|
||||
@GetMapping("/selectListByIds/{id}")
|
||||
List<Account> selectListByIds(@PathVariable("id") String id) {
|
||||
List<Long> ids = Arrays.stream(id.split(",")).mapToLong(Long::parseLong).boxed().collect(Collectors.toList());
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package com.mybatisflex.test.service;
|
||||
|
||||
import com.mybatisflex.test.mapper.AccountMapper;
|
||||
import com.mybatisflex.test.model.Account;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Component
|
||||
public class AccountService {
|
||||
|
||||
|
||||
@Resource
|
||||
AccountMapper accountMapper;
|
||||
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void update2() {
|
||||
Account account = new Account();
|
||||
account.setId(2L);
|
||||
account.setUserName("haha");
|
||||
accountMapper.update(account);
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,6 @@ spring:
|
||||
console:
|
||||
enabled: true
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user