enhance management

This commit is contained in:
wenshao
2011-12-08 09:58:32 +08:00
parent e27532ce32
commit a92d21cfab
12 changed files with 178 additions and 155 deletions
@@ -60,9 +60,7 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
@Override
public ResultSet executeQuery() throws SQLException {
if (closed) {
throw new SQLException();
}
checkOpen();
if (mockConnection != null && mockConnection.getDriver() != null) {
return mockConnection.getDriver().createResultSet(this);
@@ -77,9 +75,7 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
@Override
public int executeUpdate() throws SQLException {
if (closed) {
throw new SQLException();
}
checkOpen();
return 0;
}
@@ -171,13 +167,7 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
@Override
public void clearParameters() throws SQLException {
if (closed) {
throw new SQLException();
}
if (this.mockConnection != null && this.mockConnection.isClosed()) {
throw new SQLException("No operations allowed after connection closed.");
}
checkOpen();
parameters.clear();
}
@@ -194,17 +184,14 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
@Override
public boolean execute() throws SQLException {
if (closed) {
throw new SQLException();
}
checkOpen();
return false;
}
@Override
public void addBatch() throws SQLException {
if (closed) {
throw new SQLException();
}
checkOpen();
}
@Override
@@ -234,9 +221,7 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
@Override
public ResultSetMetaData getMetaData() throws SQLException {
if (closed) {
throw new SQLException();
}
checkOpen();
return resultSetMetaData;
}
@@ -268,10 +253,7 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
if (closed) {
throw new SQLException();
}
checkOpen();
return metadata;
}
@@ -52,11 +52,19 @@ public class MockStatement implements Statement {
mockConnection = (MockConnection) connection;
}
}
protected void checkOpen() throws SQLException, MockConnectionClosedException {
if (closed) {
throw new SQLException();
}
if (this.mockConnection != null && this.mockConnection.isClosed()) {
throw new MockConnectionClosedException();
}
}
public int getResultSetType() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return resultSetType;
}
@@ -107,9 +115,7 @@ public class MockStatement implements Statement {
@Override
public ResultSet executeQuery(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
if (mockConnection != null && mockConnection.getDriver() != null) {
return mockConnection.getDriver().executeQuery(this, sql);
@@ -120,9 +126,7 @@ public class MockStatement implements Statement {
@Override
public int executeUpdate(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return 0;
}
@@ -134,46 +138,35 @@ public class MockStatement implements Statement {
@Override
public int getMaxFieldSize() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return maxFieldSize;
}
@Override
public void setMaxFieldSize(int max) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
this.maxFieldSize = max;
}
@Override
public int getMaxRows() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return maxRows;
}
@Override
public void setMaxRows(int max) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
this.maxRows = max;
}
@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
this.escapeProcessing = enable;
}
@@ -183,41 +176,31 @@ public class MockStatement implements Statement {
@Override
public int getQueryTimeout() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return queryTimeout;
}
@Override
public void setQueryTimeout(int seconds) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
this.queryTimeout = seconds;
}
@Override
public void cancel() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
}
@Override
public SQLWarning getWarnings() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return warnings;
}
@Override
public void clearWarnings() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
warnings = null;
}
@@ -228,9 +211,7 @@ public class MockStatement implements Statement {
@Override
public void setCursorName(String name) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
cursorName = name;
}
@@ -241,9 +222,7 @@ public class MockStatement implements Statement {
@Override
public boolean execute(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
if (ERROR_SQL.equals(sql)) {
throw new SQLException();
@@ -254,18 +233,14 @@ public class MockStatement implements Statement {
@Override
public ResultSet getResultSet() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return new MockResultSet(this);
}
@Override
public int getUpdateCount() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return updateCount;
}
@@ -276,153 +251,115 @@ public class MockStatement implements Statement {
@Override
public boolean getMoreResults() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return false;
}
@Override
public void setFetchDirection(int direction) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
this.fetchDirection = direction;
}
@Override
public int getFetchDirection() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return fetchDirection;
}
@Override
public void setFetchSize(int rows) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
this.fetchSize = rows;
}
@Override
public int getFetchSize() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return fetchSize;
}
@Override
public int getResultSetConcurrency() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return resultSetConcurrency;
}
@Override
public void addBatch(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
}
@Override
public void clearBatch() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
}
@Override
public int[] executeBatch() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return new int[0];
}
@Override
public Connection getConnection() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return connection;
}
@Override
public boolean getMoreResults(int current) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return false;
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return new MockResultSet(this);
}
@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return 0;
}
@Override
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return 0;
}
@Override
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return 0;
}
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return false;
}
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return false;
}
@Override
public boolean execute(String sql, String[] columnNames) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return false;
}
@Override
public int getResultSetHoldability() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return resultSetHoldability;
}
@@ -437,16 +374,12 @@ public class MockStatement implements Statement {
@Override
public void setPoolable(boolean poolable) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
}
@Override
public boolean isPoolable() throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
checkOpen();
return false;
}
@@ -394,11 +394,11 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public int getMaxOpenPreparedStatements() {
return maxOpenPreparedStatements;
return maxPoolPreparedStatementPerConnectionSize;
}
public void setMaxOpenPreparedStatements(int maxOpenPreparedStatements) {
this.maxOpenPreparedStatements = maxOpenPreparedStatements;
this.maxPoolPreparedStatementPerConnectionSize = maxOpenPreparedStatements;
}
public boolean isLogAbandoned() {
@@ -963,12 +963,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
public List<String> getActiveConnectionStackTrace() {
List<String> list = new ArrayList<String>();
for (ActiveConnectionTraceInfo traceInfo : this.activeConnections.values()) {
StringBuilder buf = new StringBuilder();
for (StackTraceElement item : traceInfo.getStackTrace()) {
buf.append(item.toString());
buf.append("\n");
}
list.add(buf.toString());
list.add(JdbcUtils.toString(traceInfo.getStackTrace()));
}
return list;
@@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.ObjectName;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
@@ -105,9 +106,25 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
private boolean resetStatEnable = true;
private String initStackTrace;
private ObjectName objectName;
public DruidDataSource(){
}
public String getInitStackTrace() {
return initStackTrace;
}
public ObjectName getObjectName() {
return objectName;
}
public void setObjectName(ObjectName objectName) {
this.objectName = objectName;
}
public boolean isResetStatEnable() {
return resetStatEnable;
}
@@ -210,6 +227,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
return;
}
initStackTrace = JdbcUtils.toString(Thread.currentThread().getStackTrace());
this.id = DruidDriver.createDataSourceId();
enable = true;
@@ -329,7 +348,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.unlock();
}
}
@Override
public Connection getConnection() throws SQLException {
return getConnection(maxWait);
@@ -538,7 +557,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if ((!isAutoCommit) && (!isReadOnly)) {
pooledConnection.rollback();
}
// reset holder, restore default settings, clear warnings
holder.reset();
@@ -922,7 +941,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
long currentMillis = System.currentTimeMillis();
List<PoolableConnection> abondonedList = new ArrayList<PoolableConnection>();
for (; iter.hasNext();) {
Map.Entry<PoolableConnection, ActiveConnectionTraceInfo> entry = iter.next();
ActiveConnectionTraceInfo activeInfo = entry.getValue();
@@ -950,7 +969,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
}
}
// multi-check dup close
for (PoolableConnection conn : abondonedList) {
activeConnections.remove(conn);
@@ -264,6 +264,8 @@ public class DruidDataSourceC3P0Adapter implements DataSource, DruidDataSourceC3
}
public void setMaxStatementsPerConnection(int maxStatementsPerConnection) {
dataSource.setPoolPreparedStatements(maxStatementsPerConnection > 0);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxStatementsPerConnection);
}
@@ -272,6 +274,8 @@ public class DruidDataSourceC3P0Adapter implements DataSource, DruidDataSourceC3
}
public void setMaxStatements(int maxStatements) {
dataSource.setPoolPreparedStatements(maxStatements > 0);
dataSource.setMaxOpenPreparedStatements(maxStatements);
}
@@ -88,12 +88,14 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
}
dataSources.put(dataSource, objectName);
dataSource.setObjectName(objectName);
}
public synchronized static void remove(DruidDataSource dataSource) {
ObjectName objectName = dataSources.remove(dataSource);
if (objectName == null) {
LOG.error("unregister mbean failed.");
return;
}
@@ -516,4 +516,13 @@ public final class JdbcUtils {
throw new SQLException(e.getMessage(), e);
}
}
public static String toString(StackTraceElement[] stackTrace) {
StringBuilder buf = new StringBuilder();
for (StackTraceElement item : stackTrace) {
buf.append(item.toString());
buf.append("\n");
}
return buf.toString();
}
}