//文件: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();
}
}
}
