欢迎光临
我们一直在努力

一个连接池的例子(来自JIVE)(4)-JSP教程,数据库相关

建站超值云服务器,限时71元/月

//文件:dbconnectionmanager.java

package com.qingtuo.db.pool;

import java.sql.*;
import java.io.*;
import java.util.*;

/**
* central manager of database connections.
*/
public class dbconnectionmanager {

    private static dbconnectionprovider connectionprovider;
    private static object providerlock = new object();

    /**
     * returns a database connection from the currently active connection
     * provider.
     */
    public static connection getconnection() {
        if (connectionprovider == null) {
            synchronized (providerlock) {
                if (connectionprovider == null) {
                    //create the connection provider — for now, this is hardcoded. for
                    //the next beta, ill change this to load up the provider dynamically.
                    connectionprovider = new dbconnectiondefaultpool();
                    connectionprovider.start();
                }
            }
        }
        connection con = connectionprovider.getconnection();
        if (con == null) {
            system.err.println("warning: dbconnectionmanager.getconnection() failed to obtain a connection.");
        }
        return con;
    }

    /**
     * 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 dbconnectionprovider getdbconnectionprovider() {
        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.
     */
    public static void setdbconnectionprovider(dbconnectionprovider provider) {
        synchronized (providerlock) {
            if (connectionprovider != null) {
                connectionprovider.destroy();
                connectionprovider = null;
            }
            connectionprovider = provider;
            provider.start();
        }
    }

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个连接池的例子(来自JIVE)(4)-JSP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章