Skip to content

Commit 96ca5f5

Browse files
authored
Merge pull request #23 from codingapi/dev
Dev
2 parents 2c10b99 + aebde1f commit 96ca5f5

File tree

28 files changed

+539
-367
lines changed

28 files changed

+539
-367
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<java.version>1.8</java.version>
3333
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
3434

35-
<lcn.last.version>4.0.3.SNAPSHOT</lcn.last.version>
35+
<lcn.last.version>4.0.3.M1</lcn.last.version>
3636
</properties>
3737

3838

tx-client/src/main/java/com/codingapi/tx/aop/bean/TxCompensateLocal.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ public class TxCompensateLocal {
1010

1111
private String groupId;
1212

13+
private String type;
14+
15+
private int startState;
16+
17+
18+
public int getStartState() {
19+
return startState;
20+
}
21+
22+
public void setStartState(int startState) {
23+
this.startState = startState;
24+
}
1325

1426
public String getGroupId() {
1527
return groupId;
@@ -19,6 +31,14 @@ public void setGroupId(String groupId) {
1931
this.groupId = groupId;
2032
}
2133

34+
public String getType() {
35+
return type;
36+
}
37+
38+
public void setType(String type) {
39+
this.type = type;
40+
}
41+
2242
public static TxCompensateLocal current() {
2343
return currentLocal.get();
2444
}
@@ -27,5 +47,4 @@ public static void setCurrent(TxCompensateLocal current) {
2747
currentLocal.set(current);
2848
}
2949

30-
3150
}

tx-client/src/main/java/com/codingapi/tx/aop/service/impl/TxStartTransactionServerImpl.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.codingapi.tx.aop.bean.TxTransactionLocal;
77
import com.codingapi.tx.aop.service.TransactionServer;
88
import com.codingapi.tx.framework.task.TaskGroupManager;
9+
import com.codingapi.tx.framework.task.TaskState;
910
import com.codingapi.tx.framework.task.TxTask;
1011
import com.codingapi.tx.framework.thread.HookRunnable;
1112
import com.codingapi.tx.model.TxGroup;
@@ -88,20 +89,37 @@ public void run0() {
8889

8990
int lastState = rs==-1?0:resState;
9091

92+
int executeConnectionError = 0;
93+
9194
//控制本地事务的数据提交
9295
final TxTask waitTask = TaskGroupManager.getInstance().getTask(groupId, type);
9396
if(waitTask!=null){
9497
waitTask.setState(lastState);
9598
waitTask.signalTask();
99+
100+
while (!waitTask.isRemove()){
101+
try {
102+
Thread.sleep(1);
103+
} catch (InterruptedException e) {
104+
e.printStackTrace();
105+
}
106+
}
107+
108+
if(waitTask.getState()== TaskState.connectionError.getCode()){
109+
//本地执行失败.
110+
executeConnectionError = 1;
111+
112+
lastState = 0;
113+
}
96114
}
97115

98116

99117
if (compensateLocal == null) {
100118
long end = System.currentTimeMillis();
101119
long time = end - start;
102-
if (lastState == 1 && rs == 0) {
120+
if (executeConnectionError == 1||(lastState == 1 && rs == 0)) {
103121
//记录补偿日志
104-
txManagerService.sendCompensateMsg(groupId, time, info);
122+
txManagerService.sendCompensateMsg(groupId, time, info,executeConnectionError);
105123
}
106124
}
107125

tx-client/src/main/java/com/codingapi/tx/compensate/model/CompensateInfo.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class CompensateInfo {
1616
private String address;
1717
private long time;
1818
private String resJson;
19+
private int startError;
1920

2021
private int state;
2122

@@ -24,7 +25,7 @@ public String toParamsString() {
2425
String postParam = "model=" + modelName + "&uniqueKey=" + uniqueKey + "" +
2526
"&address=" + address + "&currentTime=" + currentTime +
2627
"&data=" + data + "&time=" + time + "&groupId=" + groupId + "" +
27-
"&className=" + className + "&methodStr=" + methodStr;
28+
"&className=" + className + "&methodStr=" + methodStr+"&startError="+startError;
2829
return postParam;
2930
}
3031

@@ -33,7 +34,7 @@ public CompensateInfo() {
3334

3435
public CompensateInfo(long currentTime, String modelName, String uniqueKey, String data,
3536
String methodStr, String className, String groupId, String address,
36-
long time) {
37+
long time,int startError) {
3738
this.currentTime = currentTime;
3839
this.modelName = modelName;
3940
this.uniqueKey = uniqueKey;
@@ -44,9 +45,18 @@ public CompensateInfo(long currentTime, String modelName, String uniqueKey, Stri
4445
this.address = address;
4546
this.time = time;
4647
this.state = 0;
48+
this.startError =startError;
4749
}
4850

4951

52+
public int getStartError() {
53+
return startError;
54+
}
55+
56+
public void setStartError(int startError) {
57+
this.startError = startError;
58+
}
59+
5060
public int getState() {
5161
return state;
5262
}

tx-client/src/main/java/com/codingapi/tx/compensate/service/CompensateService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public interface CompensateService {
1212

1313
void saveLocal(CompensateInfo compensateInfo);
1414

15-
boolean invoke(TransactionInvocation invocation, String groupId);
15+
boolean invoke(TransactionInvocation invocation, String groupId, int startState);
1616

1717
}

tx-client/src/main/java/com/codingapi/tx/compensate/service/impl/CompensateServiceImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ public void saveLocal(CompensateInfo compensateInfo) {
3232
}
3333

3434
@Override
35-
public boolean invoke(TransactionInvocation invocation, String groupId) {
35+
public boolean invoke(TransactionInvocation invocation, String groupId, int startState) {
3636

3737
TxCompensateLocal compensateLocal = new TxCompensateLocal();
3838
compensateLocal.setGroupId(groupId);
39+
compensateLocal.setStartState(startState);
3940

4041
TxCompensateLocal.setCurrent(compensateLocal);
4142

tx-client/src/main/java/com/codingapi/tx/control/service/impl/ActionCServiceImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ public String execute(JSONObject resObj, String json) {
3737

3838
String groupId = resObj.getString("g");
3939

40+
int startState = resObj.getInteger("ss");
41+
4042
byte[] bytes = Base64Utils.decode(data);
4143

4244
TransactionInvocation invocation = SerializerUtils.parserTransactionInvocation(bytes);
4345

4446
if (invocation != null) {
4547
logger.info("compensate method ->" + invocation.getMethodStr());
4648

47-
boolean res = compensateService.invoke(invocation, groupId);
49+
boolean res = compensateService.invoke(invocation, groupId,startState);
4850

4951
logger.info("compensate res ->" + res);
5052

tx-client/src/main/java/com/codingapi/tx/control/service/impl/ActionTServiceImpl.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.codingapi.tx.control.service.IActionService;
55
import com.codingapi.tx.framework.task.TaskGroup;
66
import com.codingapi.tx.framework.task.TaskGroupManager;
7+
import com.codingapi.tx.framework.task.TaskState;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910
import org.springframework.stereotype.Service;
@@ -64,7 +65,9 @@ private String notifyWaitTask(TaskGroup task, int state) {
6465
while (true) {
6566
if (task.isRemove()) {
6667

67-
if (task.getState() == 0 || task.getState() == 1) {
68+
if (task.getState() == TaskState.rollback.getCode()
69+
|| task.getState() == TaskState.commit.getCode()) {
70+
6871
res = "1";
6972
} else {
7073
res = "0";

tx-client/src/main/java/com/codingapi/tx/datasource/AbstractResourceProxy.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingapi.tx.datasource;
22

33

4-
import com.codingapi.tx.aop.bean.TxCompensateLocal;
4+
//import com.codingapi.tx.aop.bean.TxCompensateLocal;
55
import com.codingapi.tx.aop.bean.TxTransactionLocal;
66
import com.codingapi.tx.datasource.service.DataSourceService;
77
import com.lorne.core.framework.utils.task.Task;
@@ -64,8 +64,8 @@ public void close(ILCNResource connection) {
6464

6565
protected abstract void initDbType();
6666

67-
protected abstract C getRollback(C connection);
68-
67+
// protected abstract C getCompensateConnection(C connection,TxCompensateLocal txCompensateLocal);
68+
//
6969

7070

7171
protected ILCNResource loadConnection(){
@@ -128,10 +128,10 @@ protected C initLCNConnection(C connection) {
128128
logger.info("lcn datasource transaction control ");
129129

130130
//补偿的情况的
131-
if (TxCompensateLocal.current() != null) {
132-
logger.info("rollback transaction ");
133-
return getRollback(connection);
134-
}
131+
// if (TxCompensateLocal.current() != null) {
132+
// logger.info("rollback transaction ");
133+
// return getCompensateConnection(connection,TxCompensateLocal.current());
134+
// }
135135

136136
if(StringUtils.isNotEmpty(txTransactionLocal.getGroupId())){
137137

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codingapi.tx.framework.task;
2+
3+
/**
4+
* create by lorne on 2017/12/21
5+
*/
6+
public enum TaskState {
7+
8+
rollback(0),commit(1),networkError(-1),networkTimeOut(-2),connectionError(-3);
9+
10+
11+
/**
12+
* 数据状态:
13+
* 1:commit
14+
* 0:rollback
15+
* -1:network error
16+
* -2:network time out
17+
* -3:execute Connection error
18+
* @return state
19+
*/
20+
21+
private int code;
22+
23+
TaskState(int code) {
24+
this.code = code;
25+
}
26+
27+
28+
public int getCode() {
29+
return code;
30+
}
31+
}

tx-client/src/main/java/com/codingapi/tx/netty/service/MQTxManagerService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public interface MQTxManagerService {
6767
* @param groupId 事务组Id
6868
* @param time 执行时间
6969
* @param info 事务信息
70+
* @param startError 启动模块db执行异常
7071
*/
71-
void sendCompensateMsg(String groupId, long time, TxTransactionInfo info);
72+
void sendCompensateMsg(String groupId, long time, TxTransactionInfo info,int startError);
7273

7374

7475
/**

tx-client/src/main/java/com/codingapi/tx/netty/service/impl/MQTxManagerServiceImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public String httpGetServer() {
120120
}
121121

122122
@Override
123-
public void sendCompensateMsg(String groupId, long time, TxTransactionInfo info) {
123+
public void sendCompensateMsg(String groupId, long time, TxTransactionInfo info,int startError) {
124124

125125
String modelName = modelNameService.getModelName();
126126
String uniqueKey = modelNameService.getUniqueKey();
@@ -135,7 +135,7 @@ public void sendCompensateMsg(String groupId, long time, TxTransactionInfo info)
135135
long currentTime = System.currentTimeMillis();
136136

137137

138-
CompensateInfo compensateInfo = new CompensateInfo(currentTime, modelName, uniqueKey, data, methodStr, className, groupId, address, time);
138+
CompensateInfo compensateInfo = new CompensateInfo(currentTime, modelName, uniqueKey, data, methodStr, className, groupId, address, time,startError);
139139

140140
String json = managerHelper.httpPost(configReader.getTxUrl() + "sendCompensateMsg", compensateInfo.toParamsString());
141141

tx-manager/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.codingapi</groupId>
77
<artifactId>tx-manager</artifactId>
8-
<version>4.0.3.SNAPSHOT</version>
8+
<version>4.0.3.M1</version>
99
<packaging>jar</packaging>
1010

1111
<name>tx-manager</name>

tx-manager/src/main/java/com/codingapi/tm/api/controller/TxManagerController.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public boolean sendCompensateMsg(@RequestParam("model") String model, @RequestPa
4242
@RequestParam("currentTime") long currentTime,
4343
@RequestParam("groupId") String groupId, @RequestParam("className") String className,
4444
@RequestParam("time") int time, @RequestParam("data") String data,
45-
@RequestParam("methodStr") String methodStr, @RequestParam("address") String address) {
46-
return apiTxManagerService.sendCompensateMsg(currentTime, groupId, model, address, uniqueKey, className, methodStr, data, time);
45+
@RequestParam("methodStr") String methodStr, @RequestParam("address") String address,
46+
@RequestParam("startError") int startError) {
47+
return apiTxManagerService.sendCompensateMsg(currentTime, groupId, model, address, uniqueKey, className, methodStr, data, time,startError);
4748
}
4849

4950

tx-manager/src/main/java/com/codingapi/tm/api/service/ApiTxManagerService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ public interface ApiTxManagerService {
4646
* @param methodStr 事务启动方法
4747
* @param data 切面数据
4848
* @param time 执行时间
49+
* @param startError 启动模块异常
4950
* @return 是否保存成功
5051
*/
51-
boolean sendCompensateMsg(long currentTime, String groupId, String model, String address, String uniqueKey, String className, String methodStr, String data, int time);
52+
boolean sendCompensateMsg(long currentTime, String groupId, String model, String address, String uniqueKey, String className, String methodStr, String data, int time,int startError);
5253

5354
/**
5455
* 获取服务器状态

tx-manager/src/main/java/com/codingapi/tm/api/service/impl/ApiTxManagerServiceImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public int cleanNotifyTransaction(String groupId, String taskId) {
5050

5151

5252
@Override
53-
public boolean sendCompensateMsg(long currentTime, String groupId, String model, String address, String uniqueKey, String className, String methodStr, String data, int time) {
54-
TransactionCompensateMsg transactionCompensateMsg = new TransactionCompensateMsg(currentTime, groupId, model, address, uniqueKey, className, methodStr, data, time, 0);
53+
public boolean sendCompensateMsg(long currentTime, String groupId, String model, String address, String uniqueKey, String className, String methodStr, String data, int time,int startError) {
54+
TransactionCompensateMsg transactionCompensateMsg = new TransactionCompensateMsg(currentTime, groupId, model, address, uniqueKey, className, methodStr, data, time, 0,startError);
5555
return compensateService.saveCompensateMsg(transactionCompensateMsg);
5656
}
5757

tx-manager/src/main/java/com/codingapi/tm/compensate/model/TransactionCompensateMsg.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class TransactionCompensateMsg {
1616
private String methodStr;
1717
private String data;
1818
private int time;
19+
private int startError;
1920

2021
private TxGroup txGroup;
2122

@@ -24,7 +25,7 @@ public class TransactionCompensateMsg {
2425

2526
public TransactionCompensateMsg(long currentTime, String groupId, String model, String address,
2627
String uniqueKey, String className,
27-
String methodStr, String data, int time, int state) {
28+
String methodStr, String data, int time, int state,int startError) {
2829
this.currentTime = currentTime;
2930
this.groupId = groupId;
3031
this.model = model;
@@ -35,8 +36,16 @@ public TransactionCompensateMsg(long currentTime, String groupId, String model,
3536
this.time = time;
3637
this.address = address;
3738
this.state = state;
39+
this.startError = startError;
3840
}
3941

42+
public int getStartError() {
43+
return startError;
44+
}
45+
46+
public void setStartError(int startError) {
47+
this.startError = startError;
48+
}
4049

4150
public int getState() {
4251
return state;

0 commit comments

Comments
 (0)