欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

druid?handleException執(zhí)行流程源碼解析

 更新時間:2023年09月27日 14:03:09   作者:codecraft  
這篇文章主要為大家介紹了druid?handleException執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下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然后執(zhí)行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時會執(zhí)行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這里會執(zhí)行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時會執(zhí)行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方法這里會執(zhí)行conn.disable(error),然后標記requireDiscard為true,最后執(zhí)行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則執(zhí)行emptySignal

小結

druid會在prepareStatement或者執(zhí)行prepareStatement出現(xiàn)異常的時候執(zhí)行conn.handleException,它委托給dataSource.handleConnectionException,后者會在exceptionSorter.isExceptionFatal(sqlEx)為true時會執(zhí)行handleFatalError,handleFatalError方法這里會執(zhí)行conn.disable(error),然后標記requireDiscard為true,最后執(zhí)行discardConnection來關閉連接。通過這樣子來快速清理不可用的連接,避免連接池的連接不可用。

以上就是druid handleException執(zhí)行流程源碼解析的詳細內(nèi)容,更多關于druid handleException執(zhí)行的資料請關注腳本之家其它相關文章!

相關文章

  • 關于SpringBoot單元測試(cobertura生成覆蓋率報告)

    關于SpringBoot單元測試(cobertura生成覆蓋率報告)

    這篇文章主要介紹了關于SpringBoot單元測試(cobertura生成覆蓋率報告),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring整合MyBatis的三種方式

    Spring整合MyBatis的三種方式

    這篇文章主要介紹了Spring整合MyBatis的三種方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • springcloud引入spring-cloud-starter-openfeign失敗的解決

    springcloud引入spring-cloud-starter-openfeign失敗的解決

    這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • spring boot 中設置默認網(wǎng)頁的方法

    spring boot 中設置默認網(wǎng)頁的方法

    這篇文章主要介紹了spring boot 中設置默認網(wǎng)頁的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 混亂的Java日志體系及集成jar包梳理分析

    混亂的Java日志體系及集成jar包梳理分析

    這篇文章主要詳細的為大家梳理分析了剪不斷理還亂的Java日志體系,以及日志系統(tǒng)涉及到的繁雜的各種集成?jar?包,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-03-03
  • SpringBoot項目打成war和jar的區(qū)別說明

    SpringBoot項目打成war和jar的區(qū)別說明

    這篇文章主要介紹了SpringBoot項目打成war和jar的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例

    基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例

    本篇文章主要介紹了基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • java判斷用戶輸入的是否至少含有N位小數(shù)的實例

    java判斷用戶輸入的是否至少含有N位小數(shù)的實例

    下面小編就為大家分享一篇java判斷用戶輸入的是否至少含有N位小數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 如何解決redis的NOAUTH Authentication required異常

    如何解決redis的NOAUTH Authentication required異常

    這篇文章主要介紹了Jedis異常解決:NOAUTH Authentication required,,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值
    2019-07-07
  • Spring中的@Scope注解詳細講解及示例

    Spring中的@Scope注解詳細講解及示例

    這篇文章主要介紹了Spring中的@Scope注解詳細講解及示例,@Scope注解是 Spring IOC 容器中的一個作用域,在 Spring IOC 容器中,他用來配置Bean實例的作用域?qū)ο?需要的朋友可以參考下
    2023-11-11

最新評論