Skip to content

Commit 9e43bb7

Browse files
committed
fixed bug.
1 parent 660c98b commit 9e43bb7

8 files changed

Lines changed: 59 additions & 44 deletions

File tree

txlcn-common/src/main/java/com/codingapi/txlcn/common/util/Transactions.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@
2424
* @author ujued
2525
*/
2626
public class Transactions {
27-
27+
2828
public static String APPLICATION_ID_WHEN_RUNNING;
29-
29+
3030
/////////// 事务类型 //////////////////
31-
32-
public static final String LCN = "lcn" ;
33-
34-
public static final String TCC = "tcc" ;
35-
36-
public static final String TXC = "txc" ;
37-
31+
32+
public static final String LCN = "lcn";
33+
34+
public static final String TCC = "tcc";
35+
36+
public static final String TXC = "txc";
37+
3838
/////////// 常量 //////////////////////
39-
40-
public static final String TE = "tx_ex" ;
41-
42-
public static final String TAG_TRANSACTION = "tx" ;
43-
44-
public static final String TAG_TASK = "tx_task" ;
45-
39+
40+
public static final String TE = "Transaction Error";
41+
42+
public static final String TAG_TRANSACTION = "Transaction";
43+
44+
public static final String TAG_TASK = "Transaction Task";
45+
4646
/////////// 工具方法 ////////////////////////////////////////////
47-
47+
4848
/**
4949
* 方法签名生成事务单元ID
5050
*

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/Slf4jTxLogger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ public class Slf4jTxLogger implements TxLogger {
2828

2929
@Override
3030
public void trace(String groupId, String unitId, String tag, String content, Object... args) {
31-
log.debug("{}-{}-{}: " + content, groupId, unitId, tag, content, args);
31+
log.debug("{}: " + content, tag, content, args);
3232
}
3333

3434
@Override
3535
public void info(String groupId, String unitId, String tag, String content, Object... args) {
36-
log.debug("{}-{}-{}: " + content, groupId, unitId, tag, args);
36+
log.debug("{}: " + content, tag, args);
3737
}
3838

3939
@Override
4040
public void error(String groupId, String unitId, String content, Object... args) {
41-
log.error("{}-{}-{}: " + content, groupId, unitId, Transactions.TE, args);
41+
log.error("{}: " + content, groupId, unitId, Transactions.TE, args);
4242
}
4343
}

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/TxLogger.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ default void error(String groupId, String unitId, String content, Object... args
7171
trace(groupId, unitId, Transactions.TE, content, args);
7272
}
7373

74-
default void error(String name, String content, Object... args) {
75-
error(name, "", content, args);
74+
default void error(String groupId, String unitId, String tag, String content, Object... args) {
75+
trace(groupId, unitId, tag, content, args);
76+
}
77+
78+
default void error(String tag, String content, Object... args) {
79+
error("", "", tag, content, args);
7680
}
7781

7882
}

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/aspect/weave/DTXLogicWeaver.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.codingapi.txlcn.tc.core.TxTransactionInfo;
2222
import com.codingapi.txlcn.tc.core.context.TCGlobalContext;
2323
import com.codingapi.txlcn.tc.core.context.TxContext;
24+
import com.codingapi.txlcn.tracing.TracingContext;
2425
import lombok.extern.slf4j.Slf4j;
2526
import org.springframework.beans.factory.annotation.Autowired;
2627
import org.springframework.stereotype.Component;
@@ -56,23 +57,22 @@ public Object runTransaction(DTXInfo dtxInfo, BusinessCallback business) throws
5657
return business.call();
5758
}
5859

59-
log.debug("TX-unit start---->");
60+
log.debug("<---- TxLcn start ---->");
6061
DTXLocalContext dtxLocalContext = DTXLocalContext.getOrNew();
6162
TxContext txContext;
6263
if (globalContext.hasTxContext()) {
63-
// 子Unit获取父上下文
64-
log.debug("Unit[{}] in unit: {}, use parent's TxContext.", dtxInfo.getUnitId(), dtxLocalContext.getUnitId());
64+
// 有事务上下文的获取父上下文
6565
txContext = globalContext.txContext();
6666
dtxLocalContext.setInGroup(true);
67+
log.debug("Unit[{}] used parent's TxContext[{}].", dtxInfo.getUnitId(), txContext.getGroupId());
6768
} else {
68-
// 非子Unit开启本地事务上下文
69-
log.debug("Unit start TxContext.");
69+
// 没有的开启本地事务上下文
7070
txContext = globalContext.startTx();
7171
}
7272

7373
// 本地事务调用
7474
if (Objects.nonNull(dtxLocalContext.getGroupId())) {
75-
dtxLocalContext.setInUnit(true);
75+
dtxLocalContext.setDestroy(false);
7676
}
7777

7878
dtxLocalContext.setUnitId(dtxInfo.getUnitId());
@@ -94,17 +94,21 @@ public Object runTransaction(DTXInfo dtxInfo, BusinessCallback business) throws
9494
try {
9595
return transactionServiceExecutor.transactionRunning(info);
9696
} finally {
97-
if (!dtxLocalContext.isInUnit()) {
98-
log.debug("Destroy TxContext[{}]", info.getGroupId());
97+
if (dtxLocalContext.isDestroy()) {
9998
// 获取事务上下文通知事务执行完毕
10099
synchronized (txContext.getLock()) {
101100
txContext.getLock().notifyAll();
102101
}
103-
// 销毁事务
104-
globalContext.destroyTx(info.getGroupId());
102+
103+
// TxContext生命周期是? 和事务组一样(不与具体模块相关的)
104+
if (!dtxLocalContext.isInGroup()) {
105+
globalContext.destroyTx();
106+
}
107+
105108
DTXLocalContext.makeNeverAppeared();
109+
TracingContext.tracing().destroy();
106110
}
107-
log.debug("TX-unit end------>");
111+
log.debug("<---- TxLcn end ---->");
108112
}
109113
}
110114
}

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/core/DTXLocalContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public class DTXLocalContext {
5656
////////////////////////// volatile ///////////////////////////////
5757

5858
/**
59-
* 本地事务互调标识
59+
* 是否需要销毁。什么时候需要?一个请求下来,这个模块有两个Unit被执行,那么被调方是不能销毁的,只能有上层调用方销毁
6060
*/
61-
private boolean inUnit;
61+
private boolean destroy = true;
6262

6363
/**
6464
* 同事务组标识

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/core/context/DefaultGlobalContext.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public TxContext startTx() {
147147
txContext.setGroupId(TracingContext.tracing().groupId());
148148
String txContextKey = txContext.getGroupId() + ".dtx";
149149
attachmentCache.attach(txContextKey, txContext);
150+
log.debug("Start TxContext[{}]", txContext.getGroupId());
150151
return txContext;
151152
}
152153

@@ -158,8 +159,7 @@ public TxContext startTx() {
158159
@Override
159160
public void destroyTx(String groupId) {
160161
attachmentCache.remove(groupId + ".dtx");
161-
// 销毁GroupId
162-
TracingContext.tracing().destroy();
162+
log.debug("Destroy TxContext[{}]", groupId);
163163
}
164164

165165
@Override
@@ -172,6 +172,14 @@ public TxContext txContext() {
172172
return txContext(TracingContext.tracing().groupId());
173173
}
174174

175+
@Override
176+
public void destroyTx() {
177+
if (!hasTxContext()) {
178+
throw new IllegalStateException("non TxContext.");
179+
}
180+
destroyTx(txContext().getGroupId());
181+
}
182+
175183
@Override
176184
public boolean hasTxContext() {
177185
return TracingContext.tracing().hasGroup() && txContext(TracingContext.tracing().groupId()) != null;
@@ -180,7 +188,7 @@ public boolean hasTxContext() {
180188
@Override
181189
public boolean isDTXTimeout() {
182190
if (!hasTxContext()) {
183-
throw new IllegalStateException("non txContext.");
191+
throw new IllegalStateException("non TxContext.");
184192
}
185193
return (System.currentTimeMillis() - txContext().getCreateTime()) >= clientConfig.getDtxTime();
186194
}

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/core/context/TCGlobalContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ TccTransactionInfo tccTransactionInfo(String unitId, Supplier<TccTransactionInfo
5252

5353
TxContext startTx();
5454

55-
void destroyTx(String groupId);
56-
5755
TxContext txContext(String groupId);
5856

5957
TxContext txContext();
6058

59+
void destroyTx();
60+
61+
void destroyTx(String groupId);
62+
6163
boolean hasTxContext();
6264

6365
boolean isDTXTimeout();

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/support/TxLcnBeanHelper.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ public TxLcnBeanHelper(ApplicationContext spring) {
7676

7777

7878
private String getControlBeanName(String transactionType, DTXPropagationState lcnTransactionState) {
79-
String name = String.format(CONTROL_BEAN_NAME_FORMAT, transactionType, lcnTransactionState.getCode());
80-
log.debug("getControlBeanName->{}", name);
81-
return name;
79+
return String.format(CONTROL_BEAN_NAME_FORMAT, transactionType, lcnTransactionState.getCode());
8280
}
8381

8482
private String getRpcBeanName(String transactionType, LCNCmdType cmdType) {
@@ -96,7 +94,6 @@ private String getRpcBeanName(String transactionType, LCNCmdType cmdType) {
9694

9795
public TransactionResourceProxy loadTransactionResourceProxy(String beanName) {
9896
String name = String.format(TRANSACTION_BEAN_NAME_FORMAT, beanName);
99-
log.debug("loadTransactionResourceExecutor name ->{}", name);
10097
return spring.getBean(name, TransactionResourceProxy.class);
10198
}
10299

0 commit comments

Comments
 (0)