mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -18,7 +18,6 @@ package com.alibaba.druid.filter.stat;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Savepoint;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -45,7 +44,6 @@ import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.stat.JdbcStatContext;
|
||||
import com.alibaba.druid.stat.JdbcStatManager;
|
||||
import com.alibaba.druid.stat.JdbcStatementStat;
|
||||
import com.alibaba.druid.util.TransactionInfo;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
@@ -56,7 +54,6 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
|
||||
public final static String ATTR_SQL = "stat.sql";
|
||||
public final static String ATTR_UPDATE_COUNT = "stat.updteCount";
|
||||
public final static String ATTR_TRANSACTION = "stat.tx";
|
||||
|
||||
protected JdbcDataSourceStat dataSourceStat;
|
||||
|
||||
@@ -72,6 +69,17 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
|
||||
protected int maxSqlStatCount = 1000 * 100;
|
||||
|
||||
public StatFilter(){
|
||||
String property = System.getProperty("druid.stat.maxSqlStatCount");
|
||||
if (property != null && property.length() > 0) {
|
||||
try {
|
||||
maxSqlStatCount = Integer.parseInt(property);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnectionStackTraceEnable() {
|
||||
return connectionStackTraceEnable;
|
||||
}
|
||||
@@ -117,26 +125,6 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
this.dataSourceStat = stat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connection_setAutoCommit(FilterChain chain, ConnectionProxy connection, boolean autoCommit)
|
||||
throws SQLException {
|
||||
Map<String, Object> attributes = connection.getAttributes();
|
||||
|
||||
if (!autoCommit) {
|
||||
TransactionInfo transInfo = (TransactionInfo) attributes.get(ATTR_TRANSACTION);
|
||||
if (transInfo == null) {
|
||||
long transactionId = connection.getDirectDataSource().createTransactionId();
|
||||
transInfo = new TransactionInfo(transactionId);
|
||||
attributes.put(ATTR_TRANSACTION, transInfo);
|
||||
dataSourceStat.getConnectionStat().incrementConnectionCommitCount();
|
||||
}
|
||||
} else {
|
||||
attributes.remove(ATTR_TRANSACTION);
|
||||
}
|
||||
|
||||
chain.connection_setAutoCommit(connection, autoCommit);
|
||||
}
|
||||
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
ConnectionProxy connection = null;
|
||||
{
|
||||
@@ -202,27 +190,15 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
public void connection_commit(FilterChain chain, ConnectionProxy connection) throws SQLException {
|
||||
super.connection_commit(chain, connection);
|
||||
|
||||
handleEndTransaction(connection);
|
||||
|
||||
connectStat.incrementConnectionCommitCount();
|
||||
dataSourceStat.getConnectionStat().incrementConnectionCommitCount();
|
||||
}
|
||||
|
||||
private void handleEndTransaction(ConnectionProxy connection) {
|
||||
Map<String, Object> attributes = connection.getAttributes();
|
||||
|
||||
TransactionInfo transInfo = (TransactionInfo) attributes.remove(ATTR_TRANSACTION);
|
||||
if (transInfo != null) {
|
||||
transInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connection_rollback(FilterChain chain, ConnectionProxy connection) throws SQLException {
|
||||
super.connection_rollback(chain, connection);
|
||||
|
||||
handleEndTransaction(connection);
|
||||
|
||||
connectStat.incrementConnectionRollbackCount();
|
||||
dataSourceStat.getConnectionStat().incrementConnectionRollbackCount();
|
||||
|
||||
@@ -234,8 +210,6 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
throws SQLException {
|
||||
super.connection_rollback(chain, connection, savepoint);
|
||||
|
||||
handleEndTransaction(connection);
|
||||
|
||||
connectStat.incrementConnectionRollbackCount();
|
||||
dataSourceStat.getConnectionStat().incrementConnectionRollbackCount();
|
||||
}
|
||||
|
||||
@@ -52,13 +52,13 @@ import com.alibaba.druid.filter.stat.StatFilter;
|
||||
import com.alibaba.druid.pool.DruidDataSource.ActiveConnectionTraceInfo;
|
||||
import com.alibaba.druid.pool.vendor.NullExceptionSorter;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.TransactionInfo;
|
||||
import com.alibaba.druid.stat.JdbcDataSourceStat;
|
||||
import com.alibaba.druid.stat.JdbcStatManager;
|
||||
import com.alibaba.druid.util.ConcurrentIdentityHashMap;
|
||||
import com.alibaba.druid.util.DruidLoaderUtils;
|
||||
import com.alibaba.druid.util.Histogram;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.TransactionInfo;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
|
||||
@@ -53,9 +53,9 @@ import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
|
||||
import com.alibaba.druid.proxy.DruidDriver;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig;
|
||||
import com.alibaba.druid.proxy.jdbc.TransactionInfo;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.TransactionInfo;
|
||||
|
||||
/**
|
||||
* @author ljw<ljw2083@alibaba-inc.com>
|
||||
|
||||
@@ -44,7 +44,7 @@ import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey;
|
||||
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.util.TransactionInfo;
|
||||
import com.alibaba.druid.proxy.jdbc.TransactionInfo;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.alibaba.druid.pool.WrapperAdapter;
|
||||
import com.alibaba.druid.proxy.jdbc.CallableStatementProxyImpl;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.TransactionInfo;
|
||||
|
||||
public class MultiDataSourceConnection extends WrapperAdapter implements Connection, ConnectionProxy {
|
||||
|
||||
@@ -51,11 +52,13 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
private boolean closed = false;
|
||||
|
||||
private TransactionInfo transcationInfo;
|
||||
|
||||
public MultiDataSourceConnection(MultiDataSource multiDataSource, long id){
|
||||
this.multiDataSource = multiDataSource;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public DataSourceHolder getDataSourceHolder() {
|
||||
return dataSourceHolder;
|
||||
}
|
||||
@@ -116,6 +119,15 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
} else {
|
||||
this.autoCommit = Boolean.valueOf(autoCommit);
|
||||
}
|
||||
|
||||
if (!autoCommit) {
|
||||
if (transcationInfo == null) {
|
||||
long transactionId = this.getDirectDataSource().createTransactionId();
|
||||
transcationInfo = new TransactionInfo(transactionId);
|
||||
}
|
||||
} else {
|
||||
transcationInfo = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -136,6 +148,10 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
if (conn != null) {
|
||||
conn.commit();
|
||||
}
|
||||
|
||||
if (transcationInfo != null) {
|
||||
transcationInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -143,6 +159,10 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
if (conn != null) {
|
||||
conn.rollback();
|
||||
}
|
||||
|
||||
if (transcationInfo != null) {
|
||||
transcationInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -150,12 +170,12 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
this.closed = true;
|
||||
|
||||
|
||||
multiDataSource.afterConnectionClosed(this);
|
||||
}
|
||||
|
||||
@@ -166,11 +186,11 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
@Override
|
||||
public DatabaseMetaData getMetaData() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.getMetaData();
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.getMetaData();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -246,6 +266,10 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
@Override
|
||||
public void clearWarnings() throws SQLException {
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
if (conn != null) {
|
||||
conn.clearWarnings();
|
||||
}
|
||||
@@ -293,38 +317,42 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
@Override
|
||||
public Savepoint setSavepoint() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.setSavepoint();
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.setSavepoint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Savepoint setSavepoint(String name) throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.setSavepoint(name);
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.setSavepoint(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback(Savepoint savepoint) throws SQLException {
|
||||
if (conn != null) {
|
||||
conn.rollback(savepoint);
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
conn.rollback(savepoint);
|
||||
|
||||
if (transcationInfo != null) {
|
||||
transcationInfo.setEndTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
|
||||
if (conn != null) {
|
||||
conn.releaseSavepoint(savepoint);
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
conn.releaseSavepoint(savepoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -445,47 +473,47 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
@Override
|
||||
public Clob createClob() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.createClob();
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.createClob();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Blob createBlob() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.createBlob();
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.createBlob();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NClob createNClob() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.createNClob();
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.createNClob();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLXML createSQLXML() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.createSQLXML();
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.createSQLXML();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(int timeout) throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.isValid(timeout);
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
return true;
|
||||
return conn.isValid(timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -535,20 +563,20 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
@Override
|
||||
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.createArrayOf(typeName, elements);
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.createArrayOf(typeName, elements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.createStruct(typeName, attributes);
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLException("connection not init");
|
||||
return conn.createStruct(typeName, attributes);
|
||||
}
|
||||
|
||||
// /////
|
||||
@@ -615,6 +643,10 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
}
|
||||
|
||||
public void abort(Executor executor) throws SQLException {
|
||||
if (conn == null) {
|
||||
throw new SQLException("connection not init");
|
||||
}
|
||||
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
}
|
||||
|
||||
@@ -626,4 +658,9 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionInfo getTransactionInfo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.util;
|
||||
package com.alibaba.druid.proxy.jdbc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
Reference in New Issue
Block a user