一聚教程网:一个值得你收藏的教程网站

热门教程

一个连接池的例子(来自JIVE)(4)

时间:2022-07-02 18:19:52 编辑:袖梨 来源:一聚教程网

//文件: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, I'll change this to load up the provider dynamically.
                    connectionProvider = new DbConnectionDefaultPool();
                    connectionProvider.start();
                }
            }
        }
        Connection con = connectionProvider.getConnection();

热门栏目