Skip to content

Commit

Permalink
1.在HmilyTac注解增加isolationLevel属性, 用来确定tac模式下的隔离级别, 默认为read_uncommitted…
Browse files Browse the repository at this point in the history
…。执行select SQL时, 根据隔离级别判断是否需要checkLocks。 (#366)

2.修改tac demo中对应的测试案例。
  • Loading branch information
zkyoma authored and cherrylzhao committed Oct 24, 2023
1 parent 0cf2acf commit c002fab
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 7 deletions.
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

0 comments on commit c002fab

Please sign in to comment.