defaultconnectionprovider.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 939 行 · 第 1/3 页

JAVA
939
字号
/** * $RCSfile: DefaultConnectionProvider.java,v $ * $Revision: 1.3 $ * $Date: 2002/04/30 17:53:52 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import java.sql.*;import java.util.*;import java.io.*;import java.text.*;import java.util.Date;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;/** * Default Jive connection provider. It uses the excellent connection pool * available from http://www.javaexchange.com. This connection provider is a * a good choice unless you can use a container-managed one.<p> * * The log file for this connection provider is always stored at * <tt>[jiveHome]/logs/DefaultConnectionProvider.log</tt>, so you should ensure * that the <tt>[jiveHome]/logs</tt> directory exists. */public class DefaultConnectionProvider implements ConnectionProvider {    private ConnectionPool connectionPool = null;    private String driver;    private String serverURL;    private String username;    private String password;    private int minConnections = 3;    private int maxConnections = 10;    private double connectionTimeout = 0.5;    /**     * MySQL doesn't currently support Unicode. However, a workaround is     * implemented in the mm.mysql JDBC driver. Setting the Jive property     * database.mysql.useUnicode to true will turn this feature on.     */    private boolean mysqlUseUnicode;    private Object initLock = new Object();    /**     * Creates a new DefaultConnectionProvider.     */    public DefaultConnectionProvider() {        loadProperties();    }    public boolean isPooled() {        return true;    }    public Connection getConnection() throws SQLException {        if (connectionPool == null) {            // block until the init has been done            synchronized(initLock) {                // if still null, something has gone wrong                if (connectionPool == null) {                    System.err.println(                        "Warning: DbConnectionDefaultPool.getConnection() was" +                        "called before the internal pool has been initialized."                    );                    return null;                }            }        }        return new ConnectionWrapper(connectionPool.getConnection(), connectionPool);    }    public void start() {        //acquire lock so that no connections can be returned.        synchronized (initLock) {            String logPath = JiveGlobals.getJiveHome() + File.separator +                    "logs" + File.separator + "DefaultConnectionProvider.log";            try {                connectionPool = new ConnectionPool(driver, serverURL, username, password,                    minConnections, maxConnections, logPath, connectionTimeout);            }            catch (IOException ioe) {                System.err.println("Error starting DbConnectionDefaultPool: " + ioe);                ioe.printStackTrace();            }        }    }    public void restart() {        // Kill off pool.        destroy();        // Reload properties.        loadProperties();        // Start a new pool.        start();    }    public void destroy() {        if (connectionPool != null) {            try {                connectionPool.destroy(1);            }            catch (Exception e) {                e.printStackTrace();            }        }        //Release reference to connectionPool        connectionPool = null;    }    public void finalize() {        destroy();    }    /**     * Returns the JDBC driver classname used to make database connections.     * For example: org.gjt.mm.mysql.Driver     *     * @return the JDBC driver classname.     */    public String getDriver() {        return driver;    }    /**     * Sets the JDBC driver classname used to make database connections.     * For example: org.gjt.mm.mysql.Driver     *     * @param driver the fully qualified JDBC driver name.     */    public void setDriver(String driver) {        this.driver = driver;        saveProperties();    }    /**     * Returns the JDBC connection URL used to make database connections.     *     * @return the JDBC connection URL.     */    public String getServerURL() {        return serverURL;    }    /**     * Sets the JDBC connection URL used to make database connections.     *     * @param serverURL the JDBC connection URL.     */    public void setServerURL(String serverURL) {        this.serverURL = serverURL;        saveProperties();    }    /**     * Returns the username used to connect to the database. In some cases,     * a username is not needed so this method will return null.     *     * @return the username used to connect to the datbase.     */    public String getUsername() {        return username;    }    /**     * Sets the username used to connect to the database. In some cases, a     * username is not needed so null should be passed in.     *     * @param username the username used to connect to the database.     */    public void setUsername(String username) {        this.username = username;        saveProperties();    }    /**     * Returns the password used to connect to the database. In some cases,     * a password is not needed so this method will return null.     *     * @return the password used to connect to the database.     */    public String getPassword() {        return password;    }    /**     * Sets the password used to connect to the database. In some cases, a     * password is not needed so null should be passed in.     *     * @param password the password used to connect to the database.     */    public void setPassword(String password) {        this.password = password;        saveProperties();    }    /**     * Returns the minimum number of connections that the pool will use. This     * should probably be at least three.     *     * @return the minimum number of connections in the pool.     */    public int getMinConnections() {        return minConnections;    }    /**     * Sets the minimum number of connections that the pool will use. This     * should probably be at least three.     *     * @param minConnections the minimum number of connections in the pool.     */    public void setMinConnections(int minConnections) {        this.minConnections = minConnections;        saveProperties();    }    /**     * Returns the maximum number of connections that the pool will use. The     * actual number of connections in the pool will vary between this value     * and the minimum based on the current load.     *     * @return the max possible number of connections in the pool.     */    public int getMaxConnections() {        return maxConnections;    }    /**     * Sets the maximum number of connections that the pool will use. The     * actual number of connections in the pool will vary between this value     * and the minimum based on the current load.     *     * @param maxConnections the max possible number of connections in the pool.     */    public void setMaxConnections(int maxConnections) {        this.maxConnections = maxConnections;        saveProperties();    }    /**     * Returns the amount of time between connection recycles in days. For     * example, a value of .5 would correspond to recycling the connections     * in the pool once every half day.     *     * @return the amount of time in days between connection recycles.     */    public double getConnectionTimeout() {        return connectionTimeout;    }    /**     * Sets the amount of time between connection recycles in days. For     * example, a value of .5 would correspond to recycling the connections     * in the pool once every half day.     *     * @param connectionTimeout the amount of time in days between connection     *      recycles.     */    public void setConnectionTimeout(double connectionTimeout) {        this.connectionTimeout = connectionTimeout;        saveProperties();    }    /**     * Load properties that already exist from Jive properties.     */    private void loadProperties() {        driver = JiveGlobals.getJiveProperty("database.defaultProvider.driver");        serverURL = JiveGlobals.getJiveProperty("database.defaultProvider.serverURL");        username = JiveGlobals.getJiveProperty("database.defaultProvider.username");        password = JiveGlobals.getJiveProperty("database.defaultProvider.password");        String minCons = JiveGlobals.getJiveProperty(                "database.defaultProvider.minConnections");        String maxCons = JiveGlobals.getJiveProperty(                "database.defaultProvider.maxConnections");        String conTimeout = JiveGlobals.getJiveProperty(                "database.defaultProvider.connectionTimeout");        // See if we should use Unicode under MySQL        mysqlUseUnicode = Boolean.valueOf(                JiveGlobals.getJiveProperty("database.mysql.useUnicode")).booleanValue();        try {            if (minCons != null) {                minConnections = Integer.parseInt(minCons);            }            if (maxCons != null) {                maxConnections = Integer.parseInt(maxCons);            }            if (conTimeout != null) {                connectionTimeout = Double.parseDouble(conTimeout);            }        }        catch (Exception e) {            System.err.println("Error: could not parse default pool properties. " +                "Make sure the values exist and are correct.");            e.printStackTrace();        }    }    /**     * Save properties as Jive properties.     */    private void saveProperties() {        JiveGlobals.setJiveProperty("database.defaultProvider.driver", driver);        JiveGlobals.setJiveProperty("database.defaultProvider.serverURL", serverURL);        JiveGlobals.setJiveProperty("database.defaultProvider.username",

⌨️ 快捷键说明

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