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

📄 dbconnectionmanager.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        rs.next();                    }                }            }        }        // Otherwise, manually scroll to the correct row.        else {            for (int i = 0; i < rowNumber; i++) {                rs.next();            }        }    }    /**     * Returns the current connection provider. The only case in which this     * method should be called is if more information about the current     * connection provider is needed. Database connections should always be     * obtained by calling the getConnection method of this class.     */    public static ConnectionProvider getConnectionProvider() {        return connectionProvider;    }    /**     * Sets the connection provider. The old provider (if it exists) is shut     * down before the new one is started. A connection provider <b>should     * not</b> be started before being passed to the connection manager     * because the manager will call the start() method automatically.     *     * @param provider the ConnectionProvider that the manager should obtain     *                 connections from.     */    public static void setConnectionProvider(ConnectionProvider provider) {        synchronized (providerLock) {            if (connectionProvider != null) {                connectionProvider.destroy();                connectionProvider = null;            }            connectionProvider = provider;            connectionProvider.start();            // Now, get a connection to determine meta data.            Connection con = null;            try {                con = connectionProvider.getConnection();                setMetaData(con);                // Check to see if the database schema needs to be upgraded.                schemaManager.checkWildfireSchema(con);            }            catch (Exception e) {                Log.error(e);            }            finally {                try {                    if (con != null) {                        con.close();                    }                }                catch (Exception e) {                    Log.error(e);                }            }        }        // Remember what connection provider we want to use for restarts.        JiveGlobals.setXMLProperty("connectionProvider.className", provider.getClass().getName());    }    /**     * Destroys the currennt connection provider. Future calls to     * {@link #getConnectionProvider()} will return <tt>null</tt> until a new     * ConnectionProvider is set, or one is automatically loaded by a call to     * {@link #getConnection()}.     */    public static void destroyConnectionProvider() {        synchronized (providerLock) {            if (connectionProvider != null) {                connectionProvider.destroy();                connectionProvider = null;            }        }    }    /**     * Retrives a large text column from a result set, automatically performing     * streaming if the JDBC driver requires it. This is necessary because     * different JDBC drivers have different capabilities and methods for     * retrieving large text values.     *     * @param rs          the ResultSet to retrieve the text field from.     * @param columnIndex the column in the ResultSet of the text field.     * @return the String value of the text field.     */    public static String getLargeTextField(ResultSet rs, int columnIndex) throws SQLException {        if (isStreamTextRequired()) {            Reader bodyReader = null;            String value = null;            try {                bodyReader = rs.getCharacterStream(columnIndex);                if (bodyReader == null) {                    return null;                }                char[] buf = new char[256];                int len;                StringWriter out = new StringWriter(256);                while ((len = bodyReader.read(buf)) >= 0) {                    out.write(buf, 0, len);                }                value = out.toString();                out.close();            }            catch (Exception e) {                Log.error(e);                throw new SQLException("Failed to load text field");            }            finally {                try {                    if (bodyReader != null) {                        bodyReader.close();                    }                }                catch (Exception e) {                    // Ignore.                }            }            return value;        }        else {            return rs.getString(columnIndex);        }    }    /**     * Sets a large text column in a result set, automatically performing     * streaming if the JDBC driver requires it. This is necessary because     * different JDBC drivers have different capabilities and methods for     * setting large text values.     *     * @param pstmt the PreparedStatement to set the text field in.     * @param parameterIndex the index corresponding to the text field.     * @param value the String to set.     */    public static void setLargeTextField(PreparedStatement pstmt, int parameterIndex,                                         String value) throws SQLException {        if (isStreamTextRequired()) {            Reader bodyReader;            try {                bodyReader = new StringReader(value);                pstmt.setCharacterStream(parameterIndex, bodyReader, value.length());            }            catch (Exception e) {                Log.error(e);                throw new SQLException("Failed to set text field.");            }            // Leave bodyReader open so that the db can read from it. It *should*            // be garbage collected after it's done without needing to call close.        }        else {            pstmt.setString(parameterIndex, value);        }    }    /**     * Sets the max number of rows that should be returned from executing a     * statement. The operation is automatically bypassed if Jive knows that the     * the JDBC driver or database doesn't support it.     *     * @param stmt    the Statement to set the max number of rows for.     * @param maxRows the max number of rows to return.     */    public static void setMaxRows(Statement stmt, int maxRows) {        if (isMaxRowsSupported()) {            try {                stmt.setMaxRows(maxRows);            }            catch (Throwable t) {                // Ignore. Exception may happen if the driver doesn't support                // this operation and we didn't set meta-data correctly.                // However, it is a good idea to update the meta-data so that                // we don't have to incur the cost of catching an exception                // each time.                maxRowsSupported = false;            }        }    }    /**     * Sets the number of rows that the JDBC driver should buffer at a time.     * The operation is automatically bypassed if Jive knows that the     * the JDBC driver or database doesn't support it.     *     * @param rs the ResultSet to set the fetch size for.     * @param fetchSize the fetchSize.     */    public static void setFetchSize(ResultSet rs, int fetchSize) {        if (isFetchSizeSupported()) {            try {                rs.setFetchSize(fetchSize);            }            catch (Throwable t) {                // Ignore. Exception may happen if the driver doesn't support                // this operation and we didn't set meta-data correctly.                // However, it is a good idea to update the meta-data so that                // we don't have to incur the cost of catching an exception                // each time.                fetchSizeSupported = false;            }        }    }    /**     * Returns a SchemaManager instance, which can be used to manage the database     * schema information for Wildfire and plugins.     *     * @return a SchemaManager instance.     */    public static SchemaManager getSchemaManager() {        return schemaManager;    }    /**     * Uses a connection from the database to set meta data information about     * what different JDBC drivers and databases support.     */    private static void setMetaData(Connection con) throws SQLException {        DatabaseMetaData metaData = con.getMetaData();        // Supports transactions?        transactionsSupported = metaData.supportsTransactions();        // Supports subqueries?        subqueriesSupported = metaData.supportsCorrelatedSubqueries();        // Supports scroll insensitive result sets? Try/catch block is a        // workaround for DB2 JDBC driver, which throws an exception on        // the method call.        try {            scrollResultsSupported = metaData.supportsResultSetType(                    ResultSet.TYPE_SCROLL_INSENSITIVE);        }        catch (Exception e) {            scrollResultsSupported = false;        }        // Supports batch updates        batchUpdatesSupported = metaData.supportsBatchUpdates();        // Set defaults for other meta properties        streamTextRequired = false;        maxRowsSupported = true;        fetchSizeSupported = true;        // Get the database name so that we can perform meta data settings.        String dbName = metaData.getDatabaseProductName().toLowerCase();        String driverName = metaData.getDriverName().toLowerCase();        // Oracle properties.        if (dbName.indexOf("oracle") != -1) {            databaseType = DatabaseType.oracle;            streamTextRequired = true;            scrollResultsSupported = false;            // The i-net AUGURO JDBC driver            if (driverName.indexOf("auguro") != -1) {                streamTextRequired = false;                fetchSizeSupported = true;                maxRowsSupported = false;            }        }        // Postgres properties        else if (dbName.indexOf("postgres") != -1) {            databaseType = DatabaseType.postgresql;            // Postgres blows, so disable scrolling result sets.            scrollResultsSupported = false;            fetchSizeSupported = false;        }        // Interbase properties        else if (dbName.indexOf("interbase") != -1) {            databaseType = DatabaseType.interbase;            fetchSizeSupported = false;            maxRowsSupported = false;        }        // SQLServer        else if (dbName.indexOf("sql server") != -1) {            databaseType = DatabaseType.sqlserver;            // JDBC driver i-net UNA properties            if (driverName.indexOf("una") != -1) {                fetchSizeSupported = true;                maxRowsSupported = false;            }        }        // MySQL properties        else if (dbName.indexOf("mysql") != -1) {            databaseType = DatabaseType.mysql;            transactionsSupported = false;        }        // HSQL properties        else if (dbName.indexOf("hsql") != -1) {            databaseType = DatabaseType.hsqldb;            scrollResultsSupported = false;        }        // DB2 properties.        else if (dbName.indexOf("db2") != 1) {            databaseType = DatabaseType.db2;        }    }    /**     * Returns the database type. The possible types are constants of the     * DatabaseType class. Any database that doesn't have its own constant     * falls into the "Other" category.     *     * @return the database type.     */    public static DatabaseType getDatabaseType() {        return databaseType;    }    /**     * Returns true if connection profiling is turned on. You can collect     * profiling statistics by using the static methods of the ProfiledConnection     * class.     *     * @return true if connection profiling is enabled.     */    public static boolean isProfilingEnabled() {        return profilingEnabled;    }    /**     * Turns connection profiling on or off. You can collect profiling     * statistics by using the static methods of the ProfiledConnection     * class.     *     * @param enable true to enable profiling; false to disable.     */    public static void setProfilingEnabled(boolean enable) {        // If enabling profiling, call the start method on ProfiledConnection        if (!profilingEnabled && enable) {            ProfiledConnection.start();        }        // Otherwise, if turning off, call stop method.        else if (profilingEnabled && !enable) {            ProfiledConnection.stop();        }        profilingEnabled = enable;    }    public static boolean isTransactionsSupported() {        return transactionsSupported;    }    public static boolean isStreamTextRequired() {        return streamTextRequired;    }    public static boolean isMaxRowsSupported() {        return maxRowsSupported;    }    public static boolean isFetchSizeSupported() {        return fetchSizeSupported;    }    public static boolean isSubqueriesSupported() {        return subqueriesSupported;    }    public static boolean isScrollResultsSupported() {        return scrollResultsSupported;    }    public static boolean isBatchUpdatesSupported() {        return batchUpdatesSupported;    }    /**     * A class that identifies the type of the database that Jive is connected     * to. In most cases, we don't want to make any database specific calls     * and have no need to know the type of database we're using. However,     * there are certain cases where it's critical to know the database for     * performance reasons.     */    public static enum DatabaseType {        oracle,        postgresql,        mysql,        hsqldb,        db2,        sqlserver,        interbase,        unknown    }    private DbConnectionManager() {        // Not instantiable.    }}

⌨️ 快捷键说明

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