一、前言
关于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 exceptions, String sql, List 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 appendToExceptionList(List 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 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$ 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 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 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 exceptions, String sql, Map 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 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 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 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 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@}