Skip to content

Commit 42ed96d

Browse files
committed
针对不同的事务类型做优化
1 parent 627113e commit 42ed96d

8 files changed

Lines changed: 77 additions & 103 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class TxTransactionInfo {
1414

1515
private TxTransaction transaction;
1616

17-
private Transactional transactional;
17+
// private Transactional transactional;
1818

1919
private TxTransactionLocal txTransactionLocal;
2020

@@ -25,13 +25,13 @@ public class TxTransactionInfo {
2525
private TransactionInvocation invocation;
2626

2727

28-
public TxTransactionInfo(TxTransaction transaction,Transactional transactional, TxTransactionLocal txTransactionLocal,TransactionInvocation invocation, String txGroupId, int maxTimeOut) {
28+
public TxTransactionInfo(TxTransaction transaction, TxTransactionLocal txTransactionLocal,TransactionInvocation invocation, String txGroupId, int maxTimeOut) {
2929
this.transaction = transaction;
3030
this.txTransactionLocal = txTransactionLocal;
3131
this.txGroupId = txGroupId;
3232
this.maxTimeOut = maxTimeOut;
3333
this.invocation = invocation;
34-
this.transactional = transactional;
34+
// this.transactional = transactional;
3535
}
3636

3737
public int getMaxTimeOut() {
@@ -56,7 +56,7 @@ public TransactionInvocation getInvocation() {
5656
return invocation;
5757
}
5858

59-
public Transactional getTransactional() {
60-
return transactional;
61-
}
59+
// public Transactional getTransactional() {
60+
// return transactional;
61+
// }
6262
}

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.codingapi.tx.aop.bean;
22

3-
import org.springframework.transaction.annotation.Transactional;
4-
53
/**
64
* 分布式事务远程调用控制对象
75
* Created by lorne on 2017/6/5.
@@ -22,10 +20,7 @@ public class TxTransactionLocal {
2220

2321
private String type;
2422

25-
private boolean readOnly;
26-
27-
private Transactional transactional;
28-
23+
private boolean autoCommit = true;
2924

3025
public boolean isHasIsGroup() {
3126
return hasIsGroup;
@@ -90,25 +85,11 @@ public String getType() {
9085
return type;
9186
}
9287

93-
94-
95-
public boolean isReadOnly() {
96-
return readOnly;
97-
}
98-
99-
public Transactional getTransactional() {
100-
return transactional;
88+
public boolean isAutoCommit() {
89+
return autoCommit;
10190
}
10291

103-
public void setTransactional(Transactional transactional) {
104-
this.transactional = transactional;
105-
106-
//set readOnly
107-
if(transactional==null){
108-
//没有配置事务注解的时候当做只读来处理
109-
readOnly = true;
110-
}else{
111-
readOnly = transactional.readOnly();
112-
}
92+
public void setAutoCommit(boolean autoCommit) {
93+
this.autoCommit = autoCommit;
11394
}
11495
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.aspectj.lang.reflect.MethodSignature;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.stereotype.Service;
14-
import org.springframework.transaction.annotation.Transactional;
1514

1615
import java.lang.reflect.Method;
1716

@@ -35,16 +34,11 @@ public Object around(String groupId,int maxTimeOut, ProceedingJoinPoint point) t
3534

3635
TxTransaction transaction = thisMethod.getAnnotation(TxTransaction.class);
3736

38-
Transactional transactional = thisMethod.getAnnotation(Transactional.class);
39-
if (transactional == null) {
40-
transactional = clazz.getAnnotation(Transactional.class);
41-
}
42-
4337
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
4438

4539
TransactionInvocation invocation = new TransactionInvocation(clazz, thisMethod.getName(), thisMethod.toString(), args, method.getParameterTypes());
4640

47-
TxTransactionInfo info = new TxTransactionInfo(transaction,transactional,txTransactionLocal,invocation,groupId,maxTimeOut);
41+
TxTransactionInfo info = new TxTransactionInfo(transaction,txTransactionLocal,invocation,groupId,maxTimeOut);
4842

4943
TransactionServer server = transactionServerFactoryService.createTransactionServer(info);
5044

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public Object execute(final ProceedingJoinPoint point, final TxTransactionInfo i
3232
txTransactionLocal.setGroupId(txGroupId);
3333
txTransactionLocal.setHasStart(false);
3434
txTransactionLocal.setKid(kid);
35-
txTransactionLocal.setTransactional(info.getTransactional());
3635
txTransactionLocal.setMaxTimeOut(info.getMaxTimeOut());
3736
TxTransactionLocal.setCurrent(txTransactionLocal);
3837

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public Object execute(final ProceedingJoinPoint point, final TxTransactionInfo i
5151
txTransactionLocal.setGroupId(txGroupId);
5252
txTransactionLocal.setHasStart(false);
5353
txTransactionLocal.setKid(kid);
54-
txTransactionLocal.setTransactional(info.getTransactional());
5554
txTransactionLocal.setMaxTimeOut(info.getMaxTimeOut());
5655
TxTransactionLocal.setCurrent(txTransactionLocal);
5756

@@ -61,7 +60,7 @@ public Object execute(final ProceedingJoinPoint point, final TxTransactionInfo i
6160
Object res = point.proceed();
6261

6362
//写操作 处理
64-
if(!txTransactionLocal.isReadOnly()) {
63+
if(!txTransactionLocal.isAutoCommit()) {
6564

6665
String methodStr = info.getInvocation().getMethodStr();
6766

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public Object execute(ProceedingJoinPoint point, final TxTransactionInfo info) t
5050
TxTransactionLocal txTransactionLocal = new TxTransactionLocal();
5151
txTransactionLocal.setGroupId(groupId);
5252
txTransactionLocal.setHasStart(true);
53-
txTransactionLocal.setTransactional(info.getTransactional());
5453
txTransactionLocal.setMaxTimeOut(Constants.maxOutTime);
5554
TxTransactionLocal.setCurrent(txTransactionLocal);
5655
Object obj = point.proceed();
@@ -72,7 +71,6 @@ public Object execute(ProceedingJoinPoint point, final TxTransactionInfo info) t
7271
public void run0() {
7372
//记录补偿日志
7473
txManagerService.sendCompensateMsg(groupId, time, info);
75-
7674
}
7775
}).start();
7876

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ protected C initLCNConnection(C connection) {
125125
logger.info("lcn datasource transaction control ");
126126

127127
//只读操作,直接返回connection
128-
if(txTransactionLocal.isReadOnly()){
129-
logger.info("readonly transaction ");
130-
return connection;
131-
}
128+
// if(txTransactionLocal.isReadOnly()){
129+
// logger.info("readonly transaction ");
130+
// return connection;
131+
// }
132132

133133
//补偿的情况的
134134
if (TxCompensateLocal.current() != null) {

tx-plugins-db/src/main/java/com/codingapi/tx/datasource/relational/LCNDBConnection.java

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public class LCNDBConnection implements Connection,ILCNResource<Connection> {
4242

4343
private String groupId;
4444

45-
private boolean readOnly;
46-
4745
private TxTask waitTask;
4846

4947

@@ -77,13 +75,8 @@ public Connection get() {
7775

7876
@Override
7977
public void commit() throws SQLException {
80-
if(readOnly){
81-
connection.commit();
82-
return;
83-
}
84-
85-
8678
logger.info("commit label");
79+
8780
state = 1;
8881

8982
close();
@@ -92,11 +85,6 @@ public void commit() throws SQLException {
9285

9386
@Override
9487
public void rollback() throws SQLException {
95-
if(readOnly){
96-
connection.rollback();
97-
return;
98-
}
99-
10088
logger.info("rollback label");
10189

10290
state = 0;
@@ -113,60 +101,68 @@ protected void closeConnection() throws SQLException {
113101

114102
@Override
115103
public void close() throws SQLException {
116-
if(readOnly){
117-
connection.close();
118-
return;
119-
}
120104

121105
if(hasClose){
122106
hasClose = false;
123107
return;
124108
}
125-
logger.info("now transaction state is" + state + ", (1:commit,0:rollback) groupId:" + groupId);
126-
127-
if (state == 0) {
128-
//再嵌套时,第一次成功后面出现回滚。
129-
if(waitTask!=null&&waitTask.isAwait()&&!waitTask.isRemove()) {
130-
//通知第一个连接回滚事务。
131-
waitTask.setState(0);
132-
waitTask.signalTask();
133-
}else {
134-
connection.rollback();
135-
closeConnection();
136-
}
137109

138-
logger.info("rollback transaction ,groupId:" +groupId);
139-
}
140-
if (state == 1) {
141-
if (hasGroup) {
142-
//加入队列的连接,仅操作连接对象,不处理事务
143-
return;
110+
if(connection.getAutoCommit()){
111+
112+
closeConnection();
113+
114+
//没有开启事务控制
115+
116+
logger.info("now transaction over ! ");
117+
118+
return;
119+
}else {
120+
121+
logger.info("now transaction state is " + state + ", (1:commit,0:rollback) groupId:" + groupId);
122+
123+
if (state == 0) {
124+
//再嵌套时,第一次成功后面出现回滚。
125+
if (waitTask != null && waitTask.isAwait() && !waitTask.isRemove()) {
126+
//通知第一个连接回滚事务。
127+
waitTask.setState(0);
128+
waitTask.signalTask();
129+
} else {
130+
connection.rollback();
131+
closeConnection();
132+
}
133+
134+
logger.info("rollback transaction ,groupId:" + groupId);
144135
}
136+
if (state == 1) {
137+
if (hasGroup) {
138+
//加入队列的连接,仅操作连接对象,不处理事务
139+
return;
140+
}
145141

146-
Runnable runnable = new HookRunnable() {
147-
@Override
148-
public void run0() {
149-
try {
150-
transaction();
151-
} catch (Exception e) {
152-
try {
153-
connection.rollback();
154-
} catch (SQLException e1) {
155-
e1.printStackTrace();
156-
}
157-
} finally {
142+
Runnable runnable = new HookRunnable() {
143+
@Override
144+
public void run0() {
158145
try {
159-
closeConnection();
160-
} catch (SQLException e) {
161-
e.printStackTrace();
146+
transaction();
147+
} catch (Exception e) {
148+
try {
149+
connection.rollback();
150+
} catch (SQLException e1) {
151+
e1.printStackTrace();
152+
}
153+
} finally {
154+
try {
155+
closeConnection();
156+
} catch (SQLException e) {
157+
e.printStackTrace();
158+
}
162159
}
163160
}
164-
}
165-
};
166-
Thread thread = new Thread(runnable);
167-
thread.start();
161+
};
162+
Thread thread = new Thread(runnable);
163+
thread.start();
164+
}
168165
}
169-
170166
}
171167

172168

@@ -218,7 +214,15 @@ public TxTask getWaitTask() {
218214

219215
@Override
220216
public void setAutoCommit(boolean autoCommit) throws SQLException {
221-
connection.setAutoCommit(false);
217+
218+
if(!autoCommit){
219+
logger.info("setAutoCommit - >" +autoCommit);
220+
connection.setAutoCommit(autoCommit);
221+
222+
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
223+
txTransactionLocal.setAutoCommit(autoCommit);
224+
}
225+
222226
}
223227

224228

@@ -265,7 +269,6 @@ public DatabaseMetaData getMetaData() throws SQLException {
265269

266270
@Override
267271
public void setReadOnly(boolean readOnly) throws SQLException {
268-
this.readOnly = readOnly;
269272
connection.setReadOnly(readOnly);
270273
}
271274

0 commit comments

Comments
 (0)