⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmspool.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        while(con == null) {
            synchronized(m_availableConnections) {
                if( (m_availableConnections.size() <= 0) && (m_connectionAmount < m_maxConn) ) {
                    // create new connections
                    createConnections(m_increaseRate);
                } else if(m_availableConnections.size() > 0) {
                    // return the available connection
                    con = (Connection) m_availableConnections.pop();
                } else {
                    // no connection available - have to wait
                    // wait until there are available connections
                    if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                          A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": no connections available - have to wait");
                    }
                    try {
                          m_availableConnections.wait();
                    } catch(InterruptedException iExc) {
                          // ignore the exception
                    }
                }
            }
        }
        // done it - we have a connection
        if(testConnection(con)) {
            return con;
        } else {
            synchronized(m_availableConnections) {
                // the connection is invalid - destroy it
                ((CmsConnection)con).closeOriginalConnection();
                m_connectionAmount --;

                // create a new one
                try {
                    createConnections(1);
                    m_availableConnections.notify();
                } catch(SQLException exc) {
                    if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                        A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": unable to create new connection for broken one");
                    }
                }
            }
            // now return a new one
            return getConnection();
        }
    }

    /**
     * Puts a connection back to the pool.
     */
    public void putConnection(CmsConnection con) {
        boolean alive = false;
        try {
            // check, if the connection is available
            if(!con.isClosed()) {
                con.clearWarnings();
                // this connection is alive
                alive = true;
            }
        } catch(SQLException exc) {
            // ignore the exception, alive is false
        }

        if((con.getEstablishedTime() + (m_maxage)) < System.currentTimeMillis()) {
            // this connection is to old. destroy it and create a new-one!
            alive = false;
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": connection is to old, destroy it.");
            }
        }

        synchronized(m_availableConnections) {
            if(alive) {
                // put the connection to the available connections
                m_availableConnections.push(con);
                m_availableConnections.notify();
            } else {
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": connection was broken");
                }
                // no, the connection is dead -> trhow it away and close it
                con.closeOriginalConnection();
                m_connectionAmount --;

                // create a new one
                try {
                    createConnections(1);
                    m_availableConnections.notify();
                } catch(SQLException exc) {
                    if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                        A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": unable to create new connection for broken one");
                    }
                }
            }
        }
    }

    /**
     * Creates the needed connections, if possible.
     * @param amount - the amount of connections to create.
     * @exception SQLException if a database-access error occurs.
     */
    private void createConnections(int amount) throws SQLException {
        for(int i = 0; (i < amount) && (m_connectionAmount < m_maxConn); i++) {
            // create another connection
            m_availableConnections.push(createConnection());
            m_connectionAmount++;
        }
    }

    /**
     * Creates one connection.
     * @return the new created connection.
     * @exception SQLException if a database-access error occurs.
     */
    private Connection createConnection() throws SQLException {
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": creating new connection. Current Amount is:" + m_connectionAmount);
        }
        Connection con = null;
        Properties props = new Properties();
        props.setProperty("user", m_user);
        props.setProperty("password", m_password);
        con = m_originalDriver.connect(m_url, props);
        CmsConnection retValue = new CmsConnection(con, this);
        return retValue;
    }

    /**
     * Returns a string representation of this object.
     */
    public String toString() {
        StringBuffer output=new StringBuffer();
        output.append("[" + this.getClass().getName() + "]:");
        output.append(m_driver);
        output.append(", ");
        output.append(m_url);
        output.append(", ");
        output.append(m_user);
        output.append(", ");
        output.append(m_minConn);
        output.append(", ");
        output.append(m_maxConn);
        output.append(", ");
        output.append(m_increaseRate);
        output.append(".");
        return output.toString();
    }

    /**
     * Destroys this pool.
     */
    public void destroy() {
        synchronized(m_availableConnections) {
            while(m_availableConnections.size() > 0) {
                ((CmsConnection) m_availableConnections.pop()).closeOriginalConnection();
                m_connectionAmount--;
            }
        }
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": destroyed");
        }
    }

    /**
     * Test the connection by executing an select statement. This is done
     * only if the statement is defined in opencms.properties. Normally there
     * is no need for this testing - but in difficult environments (like firewalls)
     * you can use it to find out whats going wrong.
     * @param con The connection to test.
     * @returns true if the connection could be tested without an SQLException.
     * Returns true if there is no test-statement defined in opencms.properties.
     * Returns false if there was an SQLException by executing the statement.
     */
    protected boolean testConnection(java.sql.Connection con) {
        if((m_conTestQuery == null) || (m_conTestQuery == "")) {
            // no test should be performed - return true - con ok!
            return true;
        }
        ResultSet res = null;
        PreparedStatement stmnt = null;
        boolean retValue = true;
        try {
            stmnt = con.prepareStatement(m_conTestQuery);
            res = stmnt.executeQuery();
            res.next();
        } catch(SQLException exc) {
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": testConnection failed:\n" + com.opencms.util.Utils.getStackTrace(exc) + "\n\n" + com.opencms.util.Utils.getStackTrace(new Exception()));
            }
            retValue = false;
        } finally {
            try {
                res.close();
            } catch(Exception exc) {
                // ignore
            }
            try {
                stmnt.close();
            } catch(Exception exc) {
                // ignore
            }
        }
        return retValue;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -