datasourcemanager.java~28~

来自「一个自己做的公司网站和办公职员管理系统。」· JAVA~28~ 代码 · 共 153 行

JAVA~28~
153
字号
package ws.woa.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import java.lang.Exception;
import java.util.Set;

/**
 * system.xml偐傜僨乕僞儀乕僗傊偺愙懕忣曬傪撉傒崬傒丄曐帩偟傑偡丅
 * 傑偨丄傾僾儕働乕僔儑儞傊偺Connection僆僽僕僃僋僩偺嫙媼傪峴偄傑偡丅
 *
 * @author Wang
 */
public class DataSourceManager {

    private static DataSourceManager _instance = null;
    private HashMap hmJDBC = new HashMap();

    /**
     * DataSourceManager偺僀儞僗僞儞僗傪庢摼偟傑偡丅
     *
     * @return DataSourceManager偺僀儞僗僞儞僗
     */
    public static synchronized DataSourceManager getInstance()
                   throws FileNotFoundException, IOException, SAXException {
        if(_instance==null){
            _instance = new DataSourceManager();
        }
        return _instance;
    }

    /**
     * 僔儞僌儖僩儞僷僞乕儞揔梡偺偨傔偺僾儔僀儀乕僩僐儞僗僩儔僋僞
     */
    private DataSourceManager()
                   throws IOException, SAXException, FileNotFoundException {
        String file = ControllerServlet._context.getRealPath("/WEB-INF/system.xml");
        DataSourceSAXHandler handler = new DataSourceSAXHandler();
        XMLReader parser = new SAXParser();
        parser.setContentHandler(handler);
        parser.setErrorHandler(handler);
        parser.parse(new InputSource(new FileInputStream(file)));
    }

    /**
     * DataSourceManager偺廔椆張棟丅
     *
     * Added by Wang for JDBC Pool.
     */
    public void destroy() {
        try {
            Set set = this.hmJDBC.keySet();
            Object[] keys = set.toArray();
            for(int i=0; i<keys.length; i++) {
                ((DataSourceJDBC)this.hmJDBC.get((String)keys[i])).destroy();
            }
        } catch (Exception ex) {
            // DataSourceManager Destroy Error.
        }
    }

    /**
     * JDBC僨乕僞僜乕僗偐傜DB僐僱僋僔儑儞傪庢摼偟傑偡丅
     *
     * @param name 僨乕僞僜乕僗柤
     * @return Connection
     */
    public Connection getConnection(String name)
            throws Exception, ApplicationException {
        DataSourceJDBC ds = (DataSourceJDBC)this.hmJDBC.get(name);
        if(ds==null){
            // 僨乕僞僜乕僗偑掕媊偝傟偰偄側偄
        }
        return ds.getConnection();
    }

    /**
     * JDBC僨乕僞僜乕僗偐傜DB僐僱僋僔儑儞傪儕儕乕僗偟傑偡丅
     *
     * Added by Wang for JDBC Pool.
     */
    public void releaseConnection(String name, Connection conn) {
        ((DataSourceJDBC)this.hmJDBC.get(name)).releaseConnection(conn);
    }

    /**
     * JDBC僨乕僞僜乕僗傪捛壛偟傑偡丅
     *
     * @param name 僨乕僞僜乕僗柤
     * @param ds   JDBC僨乕僞僜乕僗
     */
    public void addDataSourceJDBC(String name,DataSourceJDBC ds){
        this.hmJDBC.put(name,ds);
    }

    ///////////////////////////////////////////////////////////////////////
    /**
     * system.xml傪夝愅偟丄僨乕僞僜乕僗忣曬傪拪弌偡傞撪晹僋儔僗丅
     */
    class DataSourceSAXHandler extends DefaultHandler {

    	/**
         * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
         */
        public void startElement(String uri,String local,String raw,Attributes attrs) {
            if(raw.equals("data-source")){
                String type = attrs.getValue("type");
                if(type.toLowerCase().equals("jdbc")){
                    setDataSourceJDBC(attrs);
                }
            }
        }

        /**
         * JDBC僨乕僞僜乕僗偺愝掕
         *
         * @param丂attrs
         */
        private void setDataSourceJDBC(Attributes attrs) {
            String name       = attrs.getValue("name");
            String user       = attrs.getValue("user");
            String password   = attrs.getValue("password");
            String driver     = attrs.getValue("driver");
            String url        = attrs.getValue("url");
            String autoCommit = attrs.getValue("auto-commit");
            String connNum = attrs.getValue("conn-number"); // Added by Wang for JDBC Pool.

            boolean commit = true;
            if(autoCommit!=null && autoCommit.equals("false")){
                commit = false;
            }

            // Modified by Wang for JDBC Pool.
            //DataSourceJDBC ds = new DataSourceJDBC(user,password,url,driver,commit);
            DataSourceJDBC ds = new DataSourceJDBC(user,password,url,driver,commit,connNum);
            addDataSourceJDBC(name,ds);
        }

    }
}

⌨️ 快捷键说明

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