Skip to content

Commit

Permalink
start 复用优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xlorne committed Dec 1, 2017
1 parent 5fd2046 commit 1837afe
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Object execute(final ProceedingJoinPoint point, final TxTransactionInfo i
txTransactionLocal.setGroupId(txGroupId);
txTransactionLocal.setHasStart(false);
txTransactionLocal.setKid(kid);
txTransactionLocal.setHasIsGroup(isHasIsGroup);
txTransactionLocal.setMaxTimeOut(info.getMaxTimeOut());
TxTransactionLocal.setCurrent(txTransactionLocal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ protected ILCNResource loadConnection(){
}
ILCNResource old = pools.get(txTransactionLocal.getGroupId());
if (old != null) {
old.setHasIsGroup(true);

txTransactionLocal.setHasIsGroup(true);
TxTransactionLocal.setCurrent(txTransactionLocal);
return old;
}
return null;
Expand Down Expand Up @@ -135,10 +131,9 @@ protected C initLCNConnection(C connection) {
}

if(StringUtils.isNotEmpty(txTransactionLocal.getGroupId())){
if (!txTransactionLocal.isHasStart()) {
logger.info("lcn transaction ");
return createConnection(txTransactionLocal, connection);
}

logger.info("lcn transaction ");
return createConnection(txTransactionLocal, connection);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,5 @@ public interface ILCNResource<T> {
String getGroupId();


/**
* 是否是重复调用执行
* @param isGroup 是否是组合
*/
void setHasIsGroup(boolean isGroup);


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.codingapi.tx.datasource.relational;

import com.codingapi.tx.aop.bean.TxTransactionLocal;
import com.codingapi.tx.framework.thread.HookRunnable;
import org.slf4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;

/**
Expand Down Expand Up @@ -35,6 +38,18 @@ public void run0() {
thread.start();
}


protected void setAutoCommitMethod(boolean autoCommit, Logger logger, Connection connection) throws SQLException {
if(!autoCommit) {
logger.info("setAutoCommit - >" + autoCommit);
connection.setAutoCommit(autoCommit);

TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
txTransactionLocal.setAutoCommit(autoCommit);
}
}


protected abstract void transaction() throws SQLException;

protected abstract void closeConnection() throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,17 @@ public class LCNDBConnection extends AbstractTransactionThread implements Connec

private int maxOutTime;

private boolean hasGroup = false;

private String groupId;

private TxTask waitTask;

private boolean hasClose = false;


public LCNDBConnection(Connection connection, DataSourceService dataSourceService, TxTransactionLocal transactionLocal, ICallClose<ILCNResource> runnable) {
public LCNDBConnection(Connection connection, DataSourceService dataSourceService, ICallClose<ILCNResource> runnable) {
logger.info("init lcn connection ! ");
this.connection = connection;
this.runnable = runnable;
this.dataSourceService = dataSourceService;
TxTransactionLocal transactionLocal = TxTransactionLocal.current();
groupId = transactionLocal.getGroupId();
maxOutTime = transactionLocal.getMaxTimeOut();

Expand All @@ -60,29 +57,20 @@ public LCNDBConnection(Connection connection, DataSourceService dataSourceServic
}


public void setHasIsGroup(boolean isGroup) {
hasGroup = isGroup;
}


@Override
public void commit() throws SQLException {
logger.info("commit label");

state = 1;

close();
hasClose = true;
}

@Override
public void rollback() throws SQLException {
logger.info("rollback label");

state = 0;

close();
hasClose = true;
}

protected void closeConnection() throws SQLException {
Expand All @@ -98,12 +86,8 @@ public void close() throws SQLException {
return;
}

if(hasClose){
hasClose = false;
return;
}

if(connection.getAutoCommit()){
if(connection.getAutoCommit()) {

closeConnection();

Expand All @@ -112,33 +96,38 @@ public void close() throws SQLException {
logger.info("now transaction over ! ");

return;
}else {

logger.info("now transaction state is " + state + ", (1:commit,0:rollback) groupId:" + groupId);

if (state == 0) {
//再嵌套时,第一次成功后面出现回滚。
if (waitTask != null && waitTask.isAwait() && !waitTask.isRemove()) {
//通知第一个连接回滚事务。
waitTask.setState(0);
waitTask.signalTask();
} else {
connection.rollback();
closeConnection();
}

logger.info("rollback transaction ,groupId:" + groupId);
}
if (state == 1) {
if (hasGroup) {
//加入队列的连接,仅操作连接对象,不处理事务
logger.info("connection hasGroup -> "+hasGroup);
return;
}

startRunnable();
}

logger.info("now transaction state is " + state + ", (1:commit,0:rollback) groupId:" + groupId);

if (state == 0) {
//再嵌套时,第一次成功后面出现回滚。
// if (waitTask != null && waitTask.isAwait() && !waitTask.isRemove()) {
// //通知第一个连接回滚事务。
// waitTask.setState(0);
// waitTask.signalTask();
// } else {
// connection.rollback();
// closeConnection();
// }

connection.rollback();
closeConnection();

logger.info("rollback transaction ,groupId:" + groupId);
}
if (state == 1) {
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
boolean hasGroup = (txTransactionLocal!=null)?txTransactionLocal.isHasIsGroup():false;
if (hasGroup) {
//加入队列的连接,仅操作连接对象,不处理事务
logger.info("connection hasGroup -> "+hasGroup);
return;
}

startRunnable();
}

}

@Override
Expand Down Expand Up @@ -194,19 +183,11 @@ public TxTask getWaitTask() {

@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {

if(!autoCommit) {
logger.info("setAutoCommit - >" + autoCommit);
connection.setAutoCommit(autoCommit);

TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
txTransactionLocal.setAutoCommit(autoCommit);
}
setAutoCommitMethod(autoCommit,logger,connection);
}




/*****default*******/

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,19 @@ public class LCNStartConnection extends AbstractTransactionThread implements Con

private Connection connection;

private TxTransactionLocal txTransactionLocal;

private ICallClose<ILCNResource> subNowCount;

private String groupId;

private TxTask waitTask;

private boolean isGroup;

private boolean hasClose = false;

private volatile int state = 1;

public LCNStartConnection(Connection connection,TxTransactionLocal txTransactionLocal, ICallClose<ILCNResource> subNowCount) {
public LCNStartConnection(Connection connection, ICallClose<ILCNResource> subNowCount) {
this.connection = connection;
this.txTransactionLocal = txTransactionLocal;
this.subNowCount = subNowCount;

TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
groupId = txTransactionLocal.getGroupId();
TaskGroup taskGroup = TaskGroupManager.getInstance().createTask(txTransactionLocal.getGroupId(),txTransactionLocal.getType());
waitTask = taskGroup.getCurrent();
Expand All @@ -59,31 +53,21 @@ public String getGroupId() {
return groupId;
}

@Override
public void setHasIsGroup(boolean isGroup) {
this.isGroup = isGroup;
}



@Override
public void commit() throws SQLException {
connection.commit();
logger.info("commit label");

state = 1;

close();
hasClose = true;
}

@Override
public void rollback() throws SQLException {
connection.rollback();
logger.info("rollback label");

state=0;

close();
hasClose = true;
}

@Override
Expand All @@ -93,25 +77,37 @@ public void close() throws SQLException {
return;
}

if(hasClose){
hasClose = false;
return;
}

if(connection.getAutoCommit()) {

closeConnection();

//没有开启事务控制

if(isGroup){
//加入队列的连接,仅操作连接对象,不处理事务
logger.info("start connection hasGroup -> "+isGroup);
logger.info("now transaction over ! ");

return;
}



if(state==0){
connection.rollback();
closeConnection();
return;
}

startRunnable();
if(state == 1) {
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
boolean isGroup = (txTransactionLocal != null) ? txTransactionLocal.isHasIsGroup() : false;
if (isGroup) {
//加入队列的连接,仅操作连接对象,不处理事务
logger.info("start connection hasGroup -> " + isGroup);

return;
}
startRunnable();
}
}

@Override
Expand Down Expand Up @@ -152,6 +148,12 @@ protected void closeConnection() throws SQLException{



@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
setAutoCommitMethod(autoCommit,logger,connection);
}



/**
* default
Expand Down Expand Up @@ -180,10 +182,6 @@ public String nativeSQL(String sql) throws SQLException {
return connection.nativeSQL(sql);
}

@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
connection.setAutoCommit(autoCommit);
}

@Override
public boolean getAutoCommit() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public void setDataSource(DataSource dataSource) {
protected Connection createLcnConnection(Connection connection, TxTransactionLocal txTransactionLocal) {
nowCount++;
if(txTransactionLocal.isHasStart()){
LCNStartConnection lcnStartConnection = new LCNStartConnection(connection,txTransactionLocal,subNowCount);
LCNStartConnection lcnStartConnection = new LCNStartConnection(connection,subNowCount);
logger.info("get new start connection - > "+txTransactionLocal.getGroupId());
pools.put(txTransactionLocal.getGroupId(), lcnStartConnection);
return lcnStartConnection;
}else {
LCNDBConnection lcn = new LCNDBConnection(connection, dataSourceService, txTransactionLocal, subNowCount);
LCNDBConnection lcn = new LCNDBConnection(connection, dataSourceService, subNowCount);
pools.put(txTransactionLocal.getGroupId(), lcn);
logger.info("get new connection ->" + txTransactionLocal.getGroupId());
return lcn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ public String getGroupId() {
}


@Override
public void setHasIsGroup(boolean isGroup) {
hasGroup = isGroup;
}


public LCNRedisConnection(RedisConnection redisConnection, DataSourceService dataSourceService, TxTransactionLocal transactionLocal, ICallClose<LCNRedisConnection> runnable) {
this.redisConnection = redisConnection;
this.runnable = runnable;
Expand Down

0 comments on commit 1837afe

Please sign in to comment.