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

📄 sqlmanager.java

📁 Java Database connection pool
💻 JAVA
字号:
/* *  PoolMan Java Object Pooling and Caching Library *  Copyright (C) 1999-2001 The Code Studio * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  The full license is located at the root of this distribution *  in the LICENSE file. */package com.codestudio.util;import com.codestudio.PoolManConstants;import com.codestudio.sql.PoolManDataSource;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Enumeration;public class SQLManager extends PoolManager {    /**     * Singleton is instantiated here in order to bypass the     * double-check locking problem in many VM's.     */    private static SQLManager myself = new SQLManager();    private String configFile = PoolManConstants.XML_CONFIG_FILE;    /**     * This method retrieves the singleton SQLManager instance     * created when the class was first loaded.     * @returns SQLManager     */    public static SQLManager getInstance() {        return myself;    }    /**     * If a configuration file other than the default is specified,     * then the singleton instance will be recreated using the new     * file. Subsequent invocations of this method with the same     * file name passed as a parameter will not cause a recreation,     * it will simply return the singleton created based on the previous     * parsing of that config file.     *     * @returns SQLManager     * @params String The name of the file to use for configuration.     */    public static SQLManager getInstance(String confFile) {        if (!confFile.equals(myself.getConfigFile())) {            synchronized (SQLManager.class) {                if (!confFile.equals(myself.getConfigFile())) {                    myself = null;                    myself = new SQLManager();                    myself.setConfigFile(confFile);                }            }        }        return myself;    }    /*      TO DO: ADD THIS AGAIN    public static SQLManager getInstance(Properties p) {}    */    /* Private Constructors. */    private SQLManager() {        super();    }    public JDBCPool createPool(PoolMetaData metad) {        JDBCPool jpool = new JDBCPool(metad);        addPool(metad.getName(), jpool);        return jpool;    }    public void setConfigFile(String filename) {        this.configFile = filename;    }    public String getConfigFile() {        return this.configFile;    }    private void assertLoaded() {        try {            if (this.pools.size() < 1) {                synchronized (SQLManager.class) {                    if (this.pools.size() < 1) {                        new com.codestudio.management.PoolManBootstrap(this.configFile);                    }                }            }        } catch (Exception e) {            throw new RuntimeException("Fatal Error while attempting " + " to Configure PoolMan: " + e.getMessage() + " " + e.toString());        }    }    /**     * Overridden implementation ensures the config is loaded.     */    public ObjectPool getPool(String name) {        assertLoaded();        return super.getPool(name);    }    public ObjectPool getPoolByJNDIName(String name) {        assertLoaded();        for (Enumeration enum = this.pools.elements(); enum.hasMoreElements();) {            JDBCPool jpool = (JDBCPool) enum.nextElement();            try {                PoolManDataSource ds = (PoolManDataSource) jpool.getDataSource();                if (ds.getJNDIName().equals(name))                    return jpool;            } catch (Exception ne) {            }        }        String errorString = "ERROR: " + name + " does not exist. " + "Please check your " + PoolManConstants.XML_CONFIG_FILE;        throw new NullPointerException(errorString);    }    /**     * Overridden implementation ensures the config is loaded.     */    public Enumeration getAllPoolnames() {        assertLoaded();        return super.getAllPoolnames();    }    public void checkCredentials(String dbname, String user, String passwd) throws SQLException {        assertLoaded();        JDBCPool pool = (JDBCPool) getPool(dbname);        pool.checkCredentials(user, passwd);    }    /**     * Get a connection from the first (default) database connection pool.     */    public Connection requestConnection() throws SQLException {        assertLoaded();        try {            JDBCPool pool = (JDBCPool) this.defaultpool;            return pool.requestConnection();        } catch (NullPointerException ne) {            throw new SQLException("No default pool! Check your poolman.xml");        }    }    /**     * Return a connection to the default pool.     */    public void returnConnection(Connection con) {        try {            con.close();        } catch (SQLException se) {        }    }    /**     * Get a connection from a particular database pool.     */    public Connection requestConnection(String dbname) throws SQLException {        if ((dbname == null) || (dbname.equals("")))            return requestConnection();        assertLoaded();        try {            JDBCPool pool = (JDBCPool) this.pools.get(dbname);            return pool.requestConnection();        } catch (NullPointerException ne) {            throw new SQLException("No pool named " + dbname + "! Check your poolman.xml" +                                   "\n** DEBUG: If the StackTrace contains an " +                                   "InstanceAlreadyExistsException, then you have " +                                   " encountered a ClassLoader linkage problem. " +                                   " Please email poolman@codestudio.com **");        }    }    /**     * Return a connection to a particular database pool. No Longer necessary:     * The con is a handle that will cause the PooledConnection to return to     * the correct pool. Method kept for backwards-compatibility purposes.     */    public void returnConnection(String dbname, Connection con) {        returnConnection(con);    }    /** Static method that closes the statement and result sets in one place;     * this is here as a convenience to shorten the finally block     * in statements.  Both arguments may be null.     * @param statement the statement to be closed     * @param resultSet the resultSet to be closed     */    public static void closeResources(Statement statement, ResultSet resultSet) {        closeResultSet(resultSet);        closeStatement(statement);    }    public void collectResources(Statement s, ResultSet r) {        SQLManager.closeResources(s, r);    }    /** Closes the given statement.  It is here to get rid of     * the extra try block in finally blocks that need to close statements     * @param statement the statement to be closed. May be null.     */    public static void closeStatement(Statement statement) {        try {            if (statement != null)                statement.close();            statement = null;        } catch (SQLException e) {        }    }    /** This method closes the given resultset.  It is here to get     * rid of the extra try block in finally blocks.     * @param rs the ResultSet to be closed. May be null.     */    public static void closeResultSet(ResultSet rs) {        try {            if (rs != null)                rs.close();            rs = null;        } catch (SQLException e) {        }    }}

⌨️ 快捷键说明

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