减少重复代码

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@449 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-07-15 09:31:25 +00:00
parent 777832ac03
commit 49646878a2
5 changed files with 24 additions and 36 deletions
@@ -931,9 +931,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if (lastConnectionConnectError != null) {
map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime());
map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage());
StringWriter buf = new StringWriter();
lastConnectionConnectError.printStackTrace(new PrintWriter(buf));
map.put("ConnectionConnectErrorLastStackTrace", buf.toString());
map.put("ConnectionConnectErrorLastStackTrace", JdbcUtils.getStackTrace(lastConnectionConnectError));
} else {
map.put("ConnectionConnectErrorLastTime", null);
map.put("ConnectionConnectErrorLastMessage", null);
@@ -954,9 +952,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
map.put("StatementLastErrorTime", stat.getStatementStat().getLastErrorTime());
map.put("StatementLastErrorMessage", lastStatementError.getMessage());
StringWriter buf = new StringWriter();
lastStatementError.printStackTrace(new PrintWriter(buf));
map.put("StatementLastErrorStackTrace", buf.toString());
map.put("StatementLastErrorStackTrace", JdbcUtils.getStackTrace(lastStatementError));
} else {
map.put("StatementLastErrorTime", null);
map.put("StatementLastErrorMessage", null);
@@ -980,9 +976,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
Throwable lastResultSetError = stat.getResultSetStat().getLastError();
if (lastResultSetError != null) {
map.put("ResultSetLastErrorMessage", lastResultSetError.getMessage());
StringWriter buf = new StringWriter();
lastResultSetError.printStackTrace(new PrintWriter(buf));
map.put("ResultSetLastErrorStackTrace", buf.toString());
map.put("ResultSetLastErrorStackTrace", JdbcUtils.getStackTrace(lastResultSetError));
} else {
map.put("ResultSetLastErrorMessage", null);
map.put("ResultSetLastErrorStackTrace", null);
@@ -992,9 +986,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
Throwable lastConnectionError = stat.getConnectionStat().getErrorLast();
if (lastConnectionError != null) {
map.put("ConnectionErrorLastMessage", lastConnectionError.getMessage());
StringWriter buf = new StringWriter();
lastConnectionError.printStackTrace(new PrintWriter(buf));
map.put("ConnectionErrorLastStackTrace", buf.toString());
map.put("ConnectionErrorLastStackTrace", JdbcUtils.getStackTrace(lastConnectionError));
} else {
map.put("ConnectionErrorLastMessage", null);
map.put("ConnectionErrorLastStackTrace", null);
@@ -219,9 +219,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
if (lastConnectionConnectError != null) {
map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime());
map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage());
StringWriter buf = new StringWriter();
lastConnectionConnectError.printStackTrace(new PrintWriter(buf));
map.put("ConnectionConnectErrorLastStackTrace", buf.toString());
map.put("ConnectionConnectErrorLastStackTrace", JdbcUtils.getStackTrace(lastConnectionConnectError));
} else {
map.put("ConnectionConnectErrorLastTime", null);
map.put("ConnectionConnectErrorLastMessage", null);
@@ -242,9 +240,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
map.put("StatementLastErrorTime", stat.getStatementStat().getLastErrorTime());
map.put("StatementLastErrorMessage", lastStatementError.getMessage());
StringWriter buf = new StringWriter();
lastStatementError.printStackTrace(new PrintWriter(buf));
map.put("StatementLastErrorStackTrace", buf.toString());
map.put("StatementLastErrorStackTrace", JdbcUtils.getStackTrace(lastStatementError));
} else {
map.put("StatementLastErrorTime", null);
map.put("StatementLastErrorMessage", null);
@@ -268,9 +264,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
Throwable lastResultSetError = stat.getResultSetStat().getLastError();
if (lastResultSetError != null) {
map.put("ResultSetLastErrorMessage", lastResultSetError.getMessage());
StringWriter buf = new StringWriter();
lastResultSetError.printStackTrace(new PrintWriter(buf));
map.put("ResultSetLastErrorStackTrace", buf.toString());
map.put("ResultSetLastErrorStackTrace", JdbcUtils.getStackTrace(lastResultSetError));
} else {
map.put("ResultSetLastErrorMessage", null);
map.put("ResultSetLastErrorStackTrace", null);
@@ -280,9 +274,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
Throwable lastConnectionError = stat.getConnectionStat().getErrorLast();
if (lastConnectionError != null) {
map.put("ConnectionErrorLastMessage", lastConnectionError.getMessage());
StringWriter buf = new StringWriter();
lastConnectionError.printStackTrace(new PrintWriter(buf));
map.put("ConnectionErrorLastStackTrace", buf.toString());
map.put("ConnectionErrorLastStackTrace", JdbcUtils.getStackTrace(lastConnectionError));
} else {
map.put("ConnectionErrorLastMessage", null);
map.put("ConnectionErrorLastStackTrace", null);
@@ -29,6 +29,7 @@ import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import com.alibaba.druid.util.JMXUtils;
import com.alibaba.druid.util.JdbcUtils;
/**
* @author wenshao<szujobs@hotmail.com>
@@ -557,9 +558,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
map.put("LastErrorMessage", lastError.getMessage());
map.put("LastErrorClass", lastError.getClass().getName());
StringWriter buf = new StringWriter();
lastError.printStackTrace(new PrintWriter(buf));
map.put("LastErrorStackTrace", buf.toString());
map.put("LastErrorStackTrace", JdbcUtils.getStackTrace(lastError));
map.put("LastErrorTime", new Date(executeErrorLastTime));
} else {
map.put("LastErrorMessage", null);
@@ -74,8 +74,7 @@ public final class JMXUtils {
public static CompositeType getThrowableCompositeType() throws JMException {
if (THROWABLE_COMPOSITE_TYPE == null) {
THROWABLE_COMPOSITE_TYPE = new CompositeType("Throwable", "Throwable", THROWABLE_COMPOSITE_INDEX_NAMES, THROWABLE_COMPOSITE_INDEX_DESCRIPTIONS,
THROWABLE_COMPOSITE_INDEX_TYPES);
THROWABLE_COMPOSITE_TYPE = new CompositeType("Throwable", "Throwable", THROWABLE_COMPOSITE_INDEX_NAMES, THROWABLE_COMPOSITE_INDEX_DESCRIPTIONS, THROWABLE_COMPOSITE_INDEX_TYPES);
}
return THROWABLE_COMPOSITE_TYPE;
@@ -91,10 +90,7 @@ public final class JMXUtils {
map.put("class", error.getClass().getName());
map.put("message", error.getMessage());
StringWriter buf = new StringWriter();
error.printStackTrace(new PrintWriter(buf));
map.put("stackTrace", buf.toString());
map.put("stackTrace", JdbcUtils.getStackTrace(error));
return new CompositeDataSupport(getThrowableCompositeType(), map);
}
@@ -19,6 +19,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;
@@ -39,7 +40,8 @@ import com.alibaba.druid.logging.LogFactory;
* @author wenshao<szujobs@hotmail.com>
*/
public final class JdbcUtils {
private final static Log LOG = LogFactory.getLog(JdbcUtils.class);
private final static Log LOG = LogFactory.getLog(JdbcUtils.class);
private static final Properties driverUrlMapping = new Properties();
@@ -426,12 +428,12 @@ public final class JdbcUtils {
throw new SQLException("unkow jdbc driver : " + rawUrl);
}
}
public static String getDbType(String rawUrl, String driverClassName) {
if (rawUrl == null) {
return null;
}
if (rawUrl.startsWith("jdbc:derby:")) {
return "derby";
} else if (rawUrl.startsWith("jdbc:mysql:")) {
@@ -488,4 +490,11 @@ public final class JdbcUtils {
return null;
}
}
public static String getStackTrace(Throwable ex) {
StringWriter buf = new StringWriter();
ex.printStackTrace(new PrintWriter(buf));
return buf.toString();
}
}