mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -31,4 +31,6 @@ public interface ConnectionProxy extends Connection, WrapperProxy {
|
||||
DataSourceProxy getDirectDataSource();
|
||||
|
||||
Date getConnectedTime();
|
||||
|
||||
TransactionInfo getTransactionInfo();
|
||||
}
|
||||
|
||||
@@ -52,6 +52,8 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
|
||||
|
||||
private final long connectedTime;
|
||||
|
||||
private TransactionInfo transcationInfo;
|
||||
|
||||
public ConnectionProxyImpl(DataSourceProxy dataSource, Connection connection, Properties properties, long id){
|
||||
super(connection, id);
|
||||
this.dataSource = dataSource;
|
||||
@@ -71,7 +73,7 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
|
||||
public Connection getConnectionRaw() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
||||
public Connection getRawObject() {
|
||||
return connection;
|
||||
}
|
||||
@@ -97,6 +99,10 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
|
||||
@Override
|
||||
public void commit() throws SQLException {
|
||||
createChain().connection_commit(this);
|
||||
|
||||
if (transcationInfo != null) {
|
||||
transcationInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -268,15 +274,32 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
|
||||
@Override
|
||||
public void rollback() throws SQLException {
|
||||
createChain().connection_rollback(this);
|
||||
|
||||
if (transcationInfo != null) {
|
||||
transcationInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback(Savepoint savepoint) throws SQLException {
|
||||
createChain().connection_rollback(this, savepoint);
|
||||
|
||||
if (transcationInfo != null) {
|
||||
transcationInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAutoCommit(boolean autoCommit) throws SQLException {
|
||||
if (!autoCommit) {
|
||||
if (transcationInfo == null) {
|
||||
long transactionId = this.dataSource.createTransactionId();
|
||||
transcationInfo = new TransactionInfo(transactionId);
|
||||
}
|
||||
} else {
|
||||
transcationInfo = null;
|
||||
}
|
||||
|
||||
createChain().connection_setAutoCommit(this, autoCommit);
|
||||
}
|
||||
|
||||
@@ -350,7 +373,12 @@ public class ConnectionProxyImpl extends WrapperProxyImpl implements ConnectionP
|
||||
if (iface == Connection.class) {
|
||||
return (T) connection;
|
||||
}
|
||||
|
||||
|
||||
return super.unwrap(iface);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionInfo getTransactionInfo() {
|
||||
return transcationInfo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.proxy.jdbc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TransactionInfo {
|
||||
|
||||
private final long id;
|
||||
private final List<String> sqlList = new ArrayList<String>();
|
||||
private final long startTimeMillis;
|
||||
private long endTimeMillis;
|
||||
|
||||
public TransactionInfo(long id){
|
||||
this.id = id;
|
||||
this.startTimeMillis = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<String> getSqlList() {
|
||||
return sqlList;
|
||||
}
|
||||
|
||||
public long getStartTimeMillis() {
|
||||
return startTimeMillis;
|
||||
}
|
||||
|
||||
public long getEndTimeMillis() {
|
||||
return endTimeMillis;
|
||||
}
|
||||
|
||||
public void setEndTimeMillis() {
|
||||
if (endTimeMillis == 0) {
|
||||
endTimeMillis = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
public void setEndTimeMillis(long endTimeMillis) {
|
||||
this.endTimeMillis = endTimeMillis;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user