druid handleException执行流程源码解析

 更新时间:2023年09月27日 14:03:09   作者:codecraft  
这篇文章主要为大家介绍了druid handleException执行流程源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下druid的handleException

prepareStatement

com/alibaba/druid/pool/DruidPooledConnection.java

public PreparedStatement prepareStatement(String sql) throws SQLException {
        checkState();
        PreparedStatementHolder stmtHolder = null;
        PreparedStatementKey key = new PreparedStatementKey(sql, getCatalog(), MethodType.M1);
        boolean poolPreparedStatements = holder.isPoolPreparedStatements();
        if (poolPreparedStatements) {
            stmtHolder = holder.getStatementPool().get(key);
        }
        if (stmtHolder == null) {
            try {
                stmtHolder = new PreparedStatementHolder(key, conn.prepareStatement(sql));
                holder.getDataSource().incrementPreparedStatementCount();
            } catch (SQLException ex) {
                handleException(ex, sql);
            }
        }
        initStatement(stmtHolder);
        DruidPooledPreparedStatement rtnVal = new DruidPooledPreparedStatement(this, stmtHolder);
        holder.addTrace(rtnVal);
        return rtnVal;
    }
DruidPooledConnection的prepareStatement会catch住SQLException然后执行handleException

executeQuery

com/alibaba/druid/pool/DruidPooledPreparedStatement.java

public ResultSet executeQuery() throws SQLException {
        checkOpen();
        incrementExecuteQueryCount();
        transactionRecord(sql);
        oracleSetRowPrefetch();
        conn.beforeExecute();
        try {
            ResultSet rs = stmt.executeQuery();
            if (rs == null) {
                return null;
            }
            DruidPooledResultSet poolableResultSet = new DruidPooledResultSet(this, rs);
            addResultSetTrace(poolableResultSet);
            return poolableResultSet;
        } catch (Throwable t) {
            errorCheck(t);
            throw checkException(t);
        } finally {
            conn.afterExecute();
        }
    }
executeQuery在catch到Throwable时会执行throw checkException(t)

checkException

com/alibaba/druid/pool/DruidPooledStatement.java

protected SQLException checkException(Throwable error) throws SQLException {
        String sql = null;
        if (this instanceof DruidPooledPreparedStatement) {
            sql = ((DruidPooledPreparedStatement) this).getSql();
        }
        handleSocketTimeout(error);
        exceptionCount++;
        return conn.handleException(error, sql);
    }
checkException这里会执行conn.handleException(error, sql)

handleException

public SQLException handleException(Throwable t, String sql) throws SQLException {
        final DruidConnectionHolder holder = this.holder;
        //
        if (holder != null) {
            DruidAbstractDataSource dataSource = holder.getDataSource();
            dataSource.handleConnectionException(this, t, sql);
        }
        if (t instanceof SQLException) {
            throw (SQLException) t;
        }
        throw new SQLException("Error", t);
    }
handleException这里是委托给了dataSource.handleConnectionException(this, t, sql);

handleConnectionException

com/alibaba/druid/pool/DruidDataSource.java

public void handleConnectionException(DruidPooledConnection pooledConnection,
                                          Throwable t,
                                          String sql) throws SQLException {
        final DruidConnectionHolder holder = pooledConnection.getConnectionHolder();
        if (holder == null) {
            return;
        }
        errorCountUpdater.incrementAndGet(this);
        lastError = t;
        lastErrorTimeMillis = System.currentTimeMillis();
        if (t instanceof SQLException) {
            SQLException sqlEx = (SQLException) t;
            // broadcastConnectionError
            ConnectionEvent event = new ConnectionEvent(pooledConnection, sqlEx);
            for (ConnectionEventListener eventListener : holder.getConnectionEventListeners()) {
                eventListener.connectionErrorOccurred(event);
            }
            // exceptionSorter.isExceptionFatal
            if (exceptionSorter != null && exceptionSorter.isExceptionFatal(sqlEx)) {
                handleFatalError(pooledConnection, sqlEx, sql);
            }
            throw sqlEx;
        } else {
            throw new SQLException("Error", t);
        }
    }
handleConnectionException方法在exceptionSorter.isExceptionFatal(sqlEx)为true时会执行handleFatalError

isExceptionFatal

com/alibaba/druid/pool/vendor/MySqlExceptionSorter.java

public class MySqlExceptionSorter implements ExceptionSorter {
    @Override
    public boolean isExceptionFatal(SQLException e) {
        if (e instanceof SQLRecoverableException) {
            return true;
        }
        final String sqlState = e.getSQLState();
        final int errorCode = e.getErrorCode();
        if (sqlState != null && sqlState.startsWith("08")) {
            return true;
        }
        switch (errorCode) {
            // Communications Errors
            case 1040: // ER_CON_COUNT_ERROR
            case 1042: // ER_BAD_HOST_ERROR
            case 1043: // ER_HANDSHAKE_ERROR
            case 1047: // ER_UNKNOWN_COM_ERROR
            case 1081: // ER_IPSOCK_ERROR
            case 1129: // ER_HOST_IS_BLOCKED
            case 1130: // ER_HOST_NOT_PRIVILEGED
                // Authentication Errors
            case 1045: // ER_ACCESS_DENIED_ERROR
                // Resource errors
            case 1004: // ER_CANT_CREATE_FILE
            case 1005: // ER_CANT_CREATE_TABLE
            case 1015: // ER_CANT_LOCK
            case 1021: // ER_DISK_FULL
            case 1041: // ER_OUT_OF_RESOURCES
                // Out-of-memory errors
            case 1037: // ER_OUTOFMEMORY
            case 1038: // ER_OUT_OF_SORTMEMORY
                // Access denied
            case 1142: // ER_TABLEACCESS_DENIED_ERROR
            case 1227: // ER_SPECIFIC_ACCESS_DENIED_ERROR
            case 1023: // ER_ERROR_ON_CLOSE
            case 1290: // ER_OPTION_PREVENTS_STATEMENT
                return true;
            default:
                break;
        }
        // for oceanbase
        if (errorCode >= -9000 && errorCode <= -8000) {
            return true;
        }
        String className = e.getClass().getName();
        if (className.endsWith(".CommunicationsException")) {
            return true;
        }
        String message = e.getMessage();
        if (message != null && message.length() > 0) {
            if (message.startsWith("Streaming result set com.mysql.jdbc.RowDataDynamic")
                    && message.endsWith("is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.")) {
                return true;
            }
            final String errorText = message.toUpperCase();
            if ((errorCode == 0 && (errorText.contains("COMMUNICATIONS LINK FAILURE")) //
                    || errorText.contains("COULD NOT CREATE CONNECTION")) //
                    || errorText.contains("NO DATASOURCE") //
                    || errorText.contains("NO ALIVE DATASOURCE")) {
                return true;
            }
        }
        Throwable cause = e.getCause();
        for (int i = 0; i < 5 && cause != null; ++i) {
            if (cause instanceof SocketTimeoutException) {
                return true;
            }
            className = cause.getClass().getName();
            if (className.endsWith(".CommunicationsException")) {
                return true;
            }
            cause = cause.getCause();
        }
        return false;
    }
    @Override
    public void configFromProperties(Properties properties) {
    }
}
MySqlExceptionSorter针对指定错误码来判断是否fatal

handleFatalError

com/alibaba/druid/pool/DruidDataSource.java

protected final void handleFatalError(DruidPooledConnection conn,
                                          SQLException error,
                                          String sql) throws SQLException {
        final DruidConnectionHolder holder = conn.holder;
        if (conn.isTraceEnable()) {
            activeConnectionLock.lock();
            try {
                if (conn.isTraceEnable()) {
                    activeConnections.remove(conn);
                    conn.setTraceEnable(false);
                }
            } finally {
                activeConnectionLock.unlock();
            }
        }
        long lastErrorTimeMillis = this.lastErrorTimeMillis;
        if (lastErrorTimeMillis == 0) {
            lastErrorTimeMillis = System.currentTimeMillis();
        }
        if (sql != null && sql.length() > 1024) {
            sql = sql.substring(0, 1024);
        }
        boolean requireDiscard = false;
        final ReentrantLock lock = conn.lock;
        lock.lock();
        try {
            if ((!conn.isClosed()) || !conn.isDisable()) {
                conn.disable(error);
                requireDiscard = true;
            }
            lastFatalErrorTimeMillis = lastErrorTimeMillis;
            fatalErrorCount++;
            if (fatalErrorCount - fatalErrorCountLastShrink > onFatalErrorMaxActive) {
                onFatalError = true;
            }
            lastFatalError = error;
            lastFatalErrorSql = sql;
        } finally {
            lock.unlock();
        }
        if (onFatalError && holder != null && holder.getDataSource() != null) {
            ReentrantLock dataSourceLock = holder.getDataSource().lock;
            dataSourceLock.lock();
            try {
                emptySignal();
            } finally {
                dataSourceLock.unlock();
            }
        }
        if (requireDiscard) {
            if (holder.statementTrace != null) {
                holder.lock.lock();
                try {
                    for (Statement stmt : holder.statementTrace) {
                        JdbcUtils.close(stmt);
                    }
                } finally {
                    holder.lock.unlock();
                }
            }
            this.discardConnection(holder);
        }
        // holder.
        LOG.error("{conn-" + holder.getConnectionId() + "} discard", error);
    }
handleFatalError方法这里会执行conn.disable(error),然后标记requireDiscard为true,最后执行discardConnection

discardConnection

com/alibaba/druid/pool/DruidDataSource.java

public void discardConnection(DruidConnectionHolder holder) {
        if (holder == null) {
            return;
        }
        Connection conn = holder.getConnection();
        if (conn != null) {
            JdbcUtils.close(conn);
        }
        lock.lock();
        try {
            if (holder.discard) {
                return;
            }
            if (holder.active) {
                activeCount--;
                holder.active = false;
            }
            discardCount++;
            holder.discard = true;
            if (activeCount <= minIdle) {
                emptySignal();
            }
        } finally {
            lock.unlock();
        }
    }
discardConnection这里会通过JdbcUtils.close(conn)关闭连接,然后加锁判断是否小于等于minIdle,若为true则执行emptySignal

小结

druid会在prepareStatement或者执行prepareStatement出现异常的时候执行conn.handleException,它委托给dataSource.handleConnectionException,后者会在exceptionSorter.isExceptionFatal(sqlEx)为true时会执行handleFatalError,handleFatalError方法这里会执行conn.disable(error),然后标记requireDiscard为true,最后执行discardConnection来关闭连接。通过这样子来快速清理不可用的连接,避免连接池的连接不可用。

以上就是druid handleException执行流程源码解析的详细内容,更多关于druid handleException执行的资料请关注脚本之家其它相关文章!

相关文章

  • java使用poi生成excel的步骤

    java使用poi生成excel的步骤

    2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样,本文重点给大家介绍java使用poi生成excel的相关知识,感兴趣的朋友一起看看吧
    2022-04-04
  • 图文详解Java中的序列化机制

    图文详解Java中的序列化机制

    java中的序列化可能大家像我一样都停留在实现Serializable接口上,对于它里面的一些核心机制没有深入了解过。本文将通过示例带大家深入了解Java中的序列化机制,需要的可以参考一下
    2022-10-10
  • 基于SpringBoot+Redis实现分布式锁

    基于SpringBoot+Redis实现分布式锁

    本文主要介绍了基于SpringBoot+Redis实现分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Java多线程基础

    Java多线程基础

    这篇文章主要介绍Java多线程基础,线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位,多线程指在单个程序中可以同时运行多个不同的线程执行不同的任务,下面来学习具体的详细内容
    2021-10-10
  • MyBatis延迟加载策略深入探究

    MyBatis延迟加载策略深入探究

    本文主要为大家详细介绍下mybatis的延迟加载,从原理上介绍下怎么使用、有什么好处能规避什么问题。感兴趣的小伙伴可以跟随小编一起学习一下
    2022-07-07
  • Javabean基于xstream包实现转XML文档的方法

    Javabean基于xstream包实现转XML文档的方法

    这篇文章主要介绍了Javabean基于xstream包实现转XML文档的方法,结合具体实例形式分析了xstream包用于转换xml文件的具体使用技巧,需要的朋友可以参考下
    2017-05-05
  • SpringMVC使用自定义验证器进行数据验证的方法

    SpringMVC使用自定义验证器进行数据验证的方法

    SpringMVC 提供了强大的数据验证机制,可以方便地验证表单提交的数据,除了自带的验证器之外,SpringMVC 还支持自定义验证器,允许开发者根据业务需求自定义验证规则,本文将介绍如何在 SpringMVC 中使用自定义验证器
    2023-07-07
  • Java FTPClient实现文件上传下载

    Java FTPClient实现文件上传下载

    这篇文章主要为大家详细介绍了Java FTPClient实现文件上传下载的相关资料,需要的朋友可以参考下
    2016-04-04
  • 从0构建Oauth2Server服务之Refreshing-access-tokens

    从0构建Oauth2Server服务之Refreshing-access-tokens

    这篇文章主要为大家介绍了从0构建Oauth2Server服务之Refreshing-access-tokens刷新令牌示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • 使用SpringBoot读取Windows共享文件的代码示例

    使用SpringBoot读取Windows共享文件的代码示例

    在现代企业环境中,文件共享是一个常见的需求,Windows共享文件夹(SMB/CIFS协议)因其易用性和广泛的兼容性,成为了许多企业的首选,在Java应用中,尤其是使用Spring Boot框架时,如何读取Windows共享文件是一个值得探讨的话题,本文介绍了使用SpringBoot读取Windows共享文件
    2024-11-11

最新评论