Skip to content

Commit e96bdc1

Browse files
committed
针对7种事务机制的,优化调整
1 parent 42ed96d commit e96bdc1

5 files changed

Lines changed: 54 additions & 18 deletions

File tree

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.codingapi.tx.annotation.TxTransaction;
44
import com.codingapi.tx.model.TransactionInvocation;
5-
import org.springframework.transaction.annotation.Transactional;
65

76

87
/**
@@ -14,24 +13,29 @@ public class TxTransactionInfo {
1413

1514
private TxTransaction transaction;
1615

17-
// private Transactional transactional;
1816

1917
private TxTransactionLocal txTransactionLocal;
2018

19+
/**
20+
* 事务组Id
21+
*/
2122
private String txGroupId;
2223

24+
/**
25+
* 最大超时时间(发起方模块的)
26+
*/
2327
private int maxTimeOut;
2428

29+
2530
private TransactionInvocation invocation;
2631

2732

28-
public TxTransactionInfo(TxTransaction transaction, TxTransactionLocal txTransactionLocal,TransactionInvocation invocation, String txGroupId, int maxTimeOut) {
33+
public TxTransactionInfo(TxTransaction transaction, TxTransactionLocal txTransactionLocal, TransactionInvocation invocation, String txGroupId, int maxTimeOut) {
2934
this.transaction = transaction;
3035
this.txTransactionLocal = txTransactionLocal;
3136
this.txGroupId = txGroupId;
3237
this.maxTimeOut = maxTimeOut;
3338
this.invocation = invocation;
34-
// this.transactional = transactional;
3539
}
3640

3741
public int getMaxTimeOut() {
@@ -56,7 +60,4 @@ public TransactionInvocation getInvocation() {
5660
return invocation;
5761
}
5862

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ public class TxTransactionLocal {
1212

1313
private int maxTimeOut;
1414

15+
/**
16+
* 是否同一个模块被多次请求
17+
*/
1518
private boolean hasIsGroup = false;
1619

20+
/**
21+
* 是否是发起方模块
22+
*/
1723
private boolean hasStart = false;
1824

25+
/**
26+
* 是否单模块下多次业务调用
27+
*/
28+
private boolean hasMoreService = false;
29+
1930
private String kid;
2031

2132
private String type;
@@ -54,6 +65,13 @@ public void setGroupId(String groupId) {
5465
this.groupId = groupId;
5566
}
5667

68+
public boolean isHasMoreService() {
69+
return hasMoreService;
70+
}
71+
72+
public void setHasMoreService(boolean hasMoreService) {
73+
this.hasMoreService = hasMoreService;
74+
}
5775

5876
public TxTransactionLocal() {
5977

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.codingapi.tx.model.TransactionInvocation;
1010
import org.aspectj.lang.ProceedingJoinPoint;
1111
import org.aspectj.lang.reflect.MethodSignature;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import org.springframework.stereotype.Service;
1416

@@ -24,6 +26,9 @@ public class AspectBeforeServiceImpl implements AspectBeforeService {
2426
private TransactionServerFactoryService transactionServerFactoryService;
2527

2628

29+
private Logger logger = LoggerFactory.getLogger(AspectBeforeServiceImpl.class);
30+
31+
2732
public Object around(String groupId,int maxTimeOut, ProceedingJoinPoint point) throws Throwable {
2833

2934
MethodSignature signature = (MethodSignature) point.getSignature();
@@ -36,6 +41,14 @@ public Object around(String groupId,int maxTimeOut, ProceedingJoinPoint point) t
3641

3742
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
3843

44+
45+
if(txTransactionLocal!=null){
46+
//在同一次事务下,调用多个业务模块。
47+
txTransactionLocal.setHasMoreService(true);
48+
}
49+
50+
logger.info("around--> groupId-> " +groupId+",txTransactionLocal->"+txTransactionLocal);
51+
3952
TransactionInvocation invocation = new TransactionInvocation(clazz, thisMethod.getName(), thisMethod.toString(), args, method.getParameterTypes());
4053

4154
TxTransactionInfo info = new TxTransactionInfo(transaction,txTransactionLocal,invocation,groupId,maxTimeOut);

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public void close(T connection) {
7070
protected T loadConnection(){
7171
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
7272

73-
if(txTransactionLocal==null){
73+
logger.info("loadConnection !");
74+
75+
if(txTransactionLocal==null||txTransactionLocal.isHasMoreService()){
7476
return null;
7577
}
7678
T old = pools.get(txTransactionLocal.getGroupId());
@@ -117,18 +119,14 @@ private C createConnection(TxTransactionLocal txTransactionLocal, C connection){
117119

118120

119121
protected C initLCNConnection(C connection) {
122+
logger.info("initLCNConnection");
120123
C lcnConnection = connection;
121124
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
122125

123-
if (txTransactionLocal != null) {
126+
if (txTransactionLocal != null&&!txTransactionLocal.isHasMoreService()) {
124127

125128
logger.info("lcn datasource transaction control ");
126129

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

133131
//补偿的情况的
134132
if (TxCompensateLocal.current() != null) {
@@ -145,6 +143,7 @@ protected C initLCNConnection(C connection) {
145143

146144

147145
}
146+
logger.info("load default connection !");
148147
return lcnConnection;
149148
}
150149

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class LCNDBConnection implements Connection,ILCNResource<Connection> {
2828

2929
private Logger logger = LoggerFactory.getLogger(LCNDBConnection.class);
3030

31-
private volatile int state = 0;
31+
private volatile int state = 1;
3232

3333
private Connection connection;
3434

@@ -46,6 +46,7 @@ public class LCNDBConnection implements Connection,ILCNResource<Connection> {
4646

4747

4848
public LCNDBConnection(Connection connection, DataSourceService dataSourceService, TxTransactionLocal transactionLocal, ICallClose<LCNDBConnection> runnable) {
49+
logger.info("init lcn connection ! ");
4950
this.connection = connection;
5051
this.runnable = runnable;
5152
this.dataSourceService = dataSourceService;
@@ -102,6 +103,10 @@ protected void closeConnection() throws SQLException {
102103
@Override
103104
public void close() throws SQLException {
104105

106+
if(connection==null||connection.isClosed()){
107+
return;
108+
}
109+
105110
if(hasClose){
106111
hasClose = false;
107112
return;
@@ -136,6 +141,7 @@ public void close() throws SQLException {
136141
if (state == 1) {
137142
if (hasGroup) {
138143
//加入队列的连接,仅操作连接对象,不处理事务
144+
logger.info("connection hasGroup -> "+hasGroup);
139145
return;
140146
}
141147

@@ -215,14 +221,13 @@ public TxTask getWaitTask() {
215221
@Override
216222
public void setAutoCommit(boolean autoCommit) throws SQLException {
217223

218-
if(!autoCommit){
219-
logger.info("setAutoCommit - >" +autoCommit);
224+
if(!autoCommit) {
225+
logger.info("setAutoCommit - >" + autoCommit);
220226
connection.setAutoCommit(autoCommit);
221227

222228
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
223229
txTransactionLocal.setAutoCommit(autoCommit);
224230
}
225-
226231
}
227232

228233

0 commit comments

Comments
 (0)