Skip to content

Commit 96ca5f5

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

28 files changed

Lines changed: 539 additions & 367 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 20 additions & 1 deletion
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

Lines changed: 20 additions & 2 deletions
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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 31 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)