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

📄 dbpool.java

📁 数据库连接池
💻 JAVA
字号:
package common.db;

import java.io.File;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

/**
 * DBPool主要是为了解耦使用从各种连接池或hibernate的session
 * 获取JDBC Connection的问题
 * @author lixu
 *
 */
public abstract class DBPool {
	/**********配置文件常量定义***********************/
	public static final String URI ="db.uri";
	public static final String USER ="db.user";
	public static final String PASSWORD ="db.password";
	public static final String DRIVER ="db.driver";
	public static final String ALIAS ="db.alias";
	public static final String MAX_CONN ="db.conn.max";
	public static final String MIN_CONN ="db.conn.min";
	public static final String TEST_SQL ="db.test.sql";
	
	private static DBPool instance;
	
	/**
	 * 获取DBPool实例
	 * 仅从系统上下文中检查common.db.DBPool的设置
	 * 获取类名后动态加载
	 * @return
	 */
	public static DBPool newInstance()
	{
		String classFullName = System.getProperty("common.db.DBPool", "common.db.ProxoolDBPoolProgrammatically");
		try
		{
			if(instance == null)
				instance = (DBPool)Class.forName(classFullName).newInstance();
		}
		catch(Exception e)
		{
			System.err.println("DBPool.newInstance error:"+e.getMessage());
			throw new RuntimeException(e);
		}
		
		return instance;
	}
	
	/**
	 * 通过XML配置文件初始化连接池
	 * @param xml
	 * @throws Exception
	 */
	public void init(File xml) throws Exception
	{
		throw new UnsupportedOperationException("Unsupport init with xml file!");
	}

	/**
	 * 通过Properties配置文件初始化连接池
	 * @param propFile
	 * @throws Exception
	 */
	public void init(String propFile) throws Exception
	{
		throw new UnsupportedOperationException("Unsupport init with xml file!");
	}
	
	
	/**
	 * 通过属性列表初始化连接池
	 * @param props
	 * @throws Exception
	 */
	public void init(Properties props) throws Exception
	{
		throw new UnsupportedOperationException("Unsupport init with Properties!");
	}
	
	/**
	 * 创建一个JDBC Connection
	 * @return
	 * @throws Exception
	 */
	public abstract Connection open() throws Exception;
	
	/**
	 * 销毁整个连接池
	 * @param reason 销毁的原因
	 */
	public abstract void destroyPool(String reason);
	
	/**
	 * 关闭一个连接
	 * @param conn
	 */
	public void close(Connection conn)
	{
		try
		{
			if(conn!=null)
				conn.close();
		}
		catch(Exception e)
		{
			e.printStackTrace(System.err);
		}
	}
	
	/**
	 * 关闭游标工具
	 * @param state
	 */
	public static void close(Statement state)
	{
		try
		{
			if(state!=null)
				state.close();
		}
		catch(Exception e)
		{
			e.printStackTrace(System.err);
		}
	}
}

⌨️ 快捷键说明

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