Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tac 模式增加事务隔离级别的控制 #366

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@
@Target(ElementType.METHOD)
public @interface HmilyTAC {

/**
* Transaction isolation level enum.
*
* @return the transaction isolation level enum
*/
IsolationLevelEnum isolationLevel() default IsolationLevelEnum.READ_UNCOMMITTED;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.dromara.hmily.annotation;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* The enum Transaction Isolation Level enum.
*
* @author zhangzhi
*/
@AllArgsConstructor
@Getter
public enum IsolationLevelEnum {

/**
* read_uncommitted enum.
*/
READ_UNCOMMITTED(0),

/**
* read_committed enum.
*/
READ_COMMITTED(1);

private final int value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dromara.hmily.core.context;

import lombok.Data;
import org.dromara.hmily.annotation.IsolationLevelEnum;
import org.dromara.hmily.common.enums.HmilyActionEnum;
import org.dromara.hmily.common.enums.HmilyRoleEnum;

Expand Down Expand Up @@ -58,6 +59,11 @@ public class HmilyTransactionContext {
*/
private String transType;

/**
* isolation level enum. {@linkplain IsolationLevelEnum}
*/
private int isolationLevel;

//以下为xa相关的参数.
/**
* xa相关的参数定义.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dromara.hmily.demo.tac.dubbo.order.service.impl;

import org.dromara.hmily.annotation.HmilyTAC;
import org.dromara.hmily.annotation.IsolationLevelEnum;
import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.common.account.api.AccountService;
import org.dromara.hmily.demo.common.account.dto.AccountDTO;
Expand All @@ -32,6 +33,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter;
import org.springframework.stereotype.Service;


Expand Down Expand Up @@ -152,9 +154,9 @@ public String mockPaymentInventoryWithConfirmTimeout(final Order order) {
}

@Override
@HmilyTAC
@HmilyTAC(isolationLevel = IsolationLevelEnum.READ_COMMITTED) // 开启读已提交隔离级别, 不开启事务会正常结束
public String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum transactionEnum) {
//第二个事务查询相同账户信息, 获取不到全局锁, 会进行回滚
//第二个事务查询相同账户信息, 获取不到全局锁, 会先进行重试, 重试完后会进行回滚
if (ReadCommittedTransactionEnum.TRANSACTION_READ_ONLY.equals(transactionEnum)) {
accountService.findByUserId(order.getUserId());
return "success";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dromara.hmily.demo.springcloud.order.service.impl;

import org.dromara.hmily.annotation.HmilyTAC;
import org.dromara.hmily.annotation.IsolationLevelEnum;
import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.common.account.dto.AccountDTO;
import org.dromara.hmily.demo.common.account.dto.AccountNestedDTO;
Expand Down Expand Up @@ -153,9 +154,9 @@ public String mockPaymentInventoryWithConfirmTimeout(final Order order) {
}

@Override
@HmilyTAC
@HmilyTAC(isolationLevel = IsolationLevelEnum.READ_COMMITTED) // 开启读已提交隔离级别, 不开启事务会正常结束
public String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum transactionEnum) {
//第二个事务查询相同账户信息, 获取不到全局锁, 会进行回滚
//第二个事务查询相同账户信息, 获取不到全局锁, 会先进行重试, 重试完后会进行回滚
if (ReadCommittedTransactionEnum.TRANSACTION_READ_ONLY.equals(transactionEnum)) {
accountClient.findByUserId(order.getUserId());
return "success";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Object handleTransaction(final ProceedingJoinPoint point, final HmilyTran
MetricsReporter.counterIncrement(LabelNames.TRANSACTION_TOTAL, new String[]{TransTypeEnum.TAC.name()});
LocalDateTime starterTime = LocalDateTime.now();
try {
tm.begin();
tm.begin(point);
try {
//execute try
returnValue = point.proceed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.dromara.hmily.annotation.HmilyTAC;
import org.dromara.hmily.annotation.TransTypeEnum;
import org.dromara.hmily.common.enums.ExecutorTypeEnum;
import org.dromara.hmily.common.enums.HmilyActionEnum;
Expand All @@ -32,6 +35,7 @@
import org.dromara.hmily.repository.spi.entity.HmilyParticipant;
import org.dromara.hmily.repository.spi.entity.HmilyTransaction;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

Expand All @@ -57,9 +61,10 @@ public static HmilyTacTransactionManager getInstance() {
/**
* Begin hmily transaction.
*
* @param point proceeding join point
* @return the hmily transaction
*/
public HmilyTransaction begin() {
public HmilyTransaction begin(final ProceedingJoinPoint point) {
//创建全局的事务,创建一个参与者
HmilyTransaction globalHmilyTransaction = createHmilyTransaction();
HmilyRepositoryStorage.createHmilyTransaction(globalHmilyTransaction);
Expand All @@ -75,6 +80,11 @@ public HmilyTransaction begin() {
context.setTransId(globalHmilyTransaction.getTransId());
context.setRole(HmilyRoleEnum.START.getCode());
context.setTransType(TransTypeEnum.TAC.name());
// set transaction isolation level
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
final HmilyTAC hmilyTAC = method.getAnnotation(HmilyTAC.class);
context.setIsolationLevel(hmilyTAC.isolationLevel().getValue());
context.setParticipantId(hmilyParticipant.getParticipantId());
HmilyContextHolder.set(context);
log.debug("TAC-tm-begin ::: {}", globalHmilyTransaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.p6spy.engine.common.ConnectionInformation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.IsolationLevelEnum;
import org.dromara.hmily.annotation.TransTypeEnum;
import org.dromara.hmily.common.enums.HmilyActionEnum;
import org.dromara.hmily.common.utils.IdWorkerUtils;
Expand Down Expand Up @@ -102,8 +103,12 @@ public void execute(final String sql, final List<Object> parameters, final Conne
String resourceId = ResourceIdUtils.INSTANCE.getResourceId(connectionInformation.getUrl());
HmilySQLComputeEngine sqlComputeEngine = HmilySQLComputeEngineFactory.newInstance(statement);
// select SQL
HmilyTransactionContext transactionContext = HmilyContextHolder.get();
if (statement instanceof HmilySelectStatement) {
executeSelect(sql, parameters, connectionInformation, sqlComputeEngine, resourceId);
if (IsolationLevelEnum.READ_COMMITTED.getValue() == transactionContext.getIsolationLevel()) {
// read committed level need check locks
executeSelect(sql, parameters, connectionInformation, sqlComputeEngine, resourceId);
}
return;
}
HmilyDataSnapshot snapshot = sqlComputeEngine.execute(sql, parameters, connectionInformation.getConnection(), resourceId);
Expand Down
Loading