一、前言
关于taobao的tddl-common包(5.1.0)的com.taobao.tddl.common.jdbc.TExceptionUtils异常工具类,对SQLException集合异常日志标准化打印、合并拼接处理及自定义打印输出等,详见源码说明。
二、源码说明
package com.taobao.tddl.common.jdbc;@b@@b@import java.sql.SQLException;@b@import java.util.ArrayList;@b@import java.util.Iterator;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.Map;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public class TExceptionUtils@b@{@b@ public static StackTraceElement split = new StackTraceElement("------- one sql exceptions-----", "", "", 0);@b@ public static final String SQL_EXECUTION_ERROR_CONTEXT_LOG = "SQL_EXECUTION_ERROR_CONTEXT_LOG";@b@ private static final String SQL_EXECUTION_ERROR_CONTEXT_MESSAGE = "SQLException ,context is ";@b@ private static final Log log = LogFactory.getLog("SQL_EXECUTION_ERROR_CONTEXT_LOG");@b@@b@ public static void throwSQLException(List<SQLException> exceptions, String sql, List<Object> args)@b@ throws SQLException@b@ {@b@ if ((exceptions != null) && (!(exceptions.isEmpty()))) {@b@ SQLException first = (SQLException)exceptions.get(0);@b@ if (sql != null) {@b@ log.info(new StringBuilder().append("TDDL SQL EXECUTE ERROR REPORTER:").append(getErrorContext(sql, args, "SQLException ,context is ")).toString(), first);@b@ }@b@@b@ int i = 1; for (int n = exceptions.size(); i < n; ++i) {@b@ if (sql != null) {@b@ log.info(new StringBuilder().append("layer:").append(n).append("TDDL SQL EXECUTE ERROR REPORTER :").append(getErrorContext(sql, args, "SQLException ,context is ")).toString(), (Throwable)exceptions.get(i));@b@ }@b@@b@ }@b@@b@ throw mergeException(exceptions);@b@ }@b@ }@b@@b@ public static List<SQLException> appendToExceptionList(List<SQLException> list, SQLException sqlException)@b@ {@b@ if (list == null)@b@ list = new LinkedList();@b@@b@ list.add(sqlException);@b@ return list;@b@ }@b@@b@ public static SQLException mergeException(List<SQLException> exceptions)@b@ {@b@ SQLException first = (SQLException)exceptions.get(0);@b@ List stes = new ArrayList(30 * exceptions.size());@b@@b@ boolean hasSplit = false;@b@ StackTraceElement[] arr$ = first.getStackTrace(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { StackTraceElement ste = arr$[i$];@b@ stes.add(ste);@b@ if (ste == split)@b@ hasSplit = true;@b@ }@b@@b@ if (!(hasSplit))@b@ stes.add(split);@b@@b@ SQLException current = null;@b@ int i = 1; for (int n = exceptions.size(); i < n; ++i)@b@ {@b@ current = (SQLException)exceptions.get(i);@b@@b@ hasSplit = false;@b@ StackTraceElement[] arr$ = current.getStackTrace(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { StackTraceElement ste = arr$[i$];@b@ stes.add(ste);@b@ if (ste == split)@b@ hasSplit = true;@b@ }@b@@b@ if (!(hasSplit))@b@ stes.add(split);@b@@b@ }@b@@b@ first.setStackTrace((StackTraceElement[])stes.toArray(new StackTraceElement[stes.size()]));@b@ return first;@b@ }@b@@b@ public static void throwSQLException(SQLException exception, String sql, List<Object> args) throws SQLException@b@ {@b@ if (sql != null) {@b@ log.info(new StringBuilder().append("TDDL SQL EXECUTE ERROR REPORTER:").append(getErrorContext(sql, args, "SQLException ,context is ")).append("nest Exceptions is ").append(exception.getMessage()).toString(), exception);@b@ }@b@@b@ throw exception;@b@ }@b@@b@ public static String getErrorContext(String sql, List<Object> arguments, String message)@b@ {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(message).append(sql).append("|||arguments:");@b@ printArgument(arguments, sb);@b@ return sb.toString();@b@ }@b@@b@ private static void printArgument(List<Object> parameters, StringBuilder sb) {@b@ Iterator i$;@b@ int i = 0;@b@ if (parameters != null)@b@ for (i$ = parameters.iterator(); i$.hasNext(); ) { Object param = i$.next();@b@@b@ sb.append("[index:").append(i).append("|parameter:").append(param).append("|typeclass:").append((param == null) ? null : param.getClass().getName()).append("]");@b@@b@ ++i;@b@ }@b@ else@b@ sb.append("[empty]");@b@ }@b@@b@ public static void throwSQLException(List<SQLException> exceptions, String sql, Map<Integer, ParameterContext> parameter)@b@ throws SQLException@b@ {@b@ if ((exceptions != null) && (!(exceptions.isEmpty()))) {@b@ SQLException first = (SQLException)exceptions.get(0);@b@ if (sql != null) {@b@ log.info(new StringBuilder().append("TDDL SQL EXECUTE ERROR REPORTER:").append(getErrorContext(sql, parameter, "SQLException ,context is ")).toString(), first);@b@ }@b@@b@ int i = 1; for (int n = exceptions.size(); i < n; ++i) {@b@ if (sql != null) {@b@ log.info(new StringBuilder().append("layer:").append(n).append("TDDL SQL EXECUTE ERROR REPORTER :").append(getErrorContext(sql, parameter, "SQLException ,context is ")).toString(), (Throwable)exceptions.get(i));@b@ }@b@@b@ }@b@@b@ throw mergeException(exceptions);@b@ }@b@ }@b@@b@ public static void throwSQLException(SQLException exception, String sql, Map<Integer, ParameterContext> parameter) throws SQLException@b@ {@b@ if (sql != null) {@b@ log.info(new StringBuilder().append("TDDL SQL EXECUTE ERROR REPORTER:").append(getErrorContext(sql, parameter, "SQLException ,context is ")).append("nest Exceptions is ").append(exception.getMessage()).toString(), exception);@b@ }@b@@b@ throw exception;@b@ }@b@@b@ public static String getErrorContext(String sql, Map<Integer, ParameterContext> parameter, String message)@b@ {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(message).append(sql).append("|||arguments:");@b@ printArgument(parameter, sb);@b@ return sb.toString();@b@ }@b@@b@ private static void printArgument(Map<Integer, ParameterContext> parameter, StringBuilder sb)@b@ {@b@ int i = 0;@b@ if (parameter != null)@b@ for (Object param : parameter.entrySet())@b@ {@b@ sb.append("[index:").append(i).append("|parameter:").append(param).append("|typeclass:").append((param == null) ? null : param.getClass().getName()).append("]");@b@@b@ ++i;@b@ }@b@ else@b@ sb.append("[empty]");@b@ }@b@@b@ public static void printSQLExceptionToErrorLog(Log logger, String message, List<SQLException> sqlExceptions)@b@ {@b@ if ((sqlExceptions != null) && (!(sqlExceptions.isEmpty()))) {@b@ for (SQLException sqlException : sqlExceptions)@b@ logger.error(message, sqlException);@b@@b@ sqlExceptions.clear();@b@ }@b@ }@b@}