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

📄 dbcpconnectionpool.java

📁 使用jsp开发的一个基于HSQLDB的快餐订购管理系统
💻 JAVA
字号:
package com.uiwz.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import com.uiwz.utils.StringUtil;

public class DBCPConnectionPool
{
	private static final Log log = LogFactory.getLog(DBCPConnectionPool.class);

	private BasicDataSource basicDataSource;
	private static DBCPConnectionPool cache = null;

	private DBCPConnectionPool()
	throws SQLException {
		basicDataSource = null;
		init();
	}

	public static DBCPConnectionPool getInstance()
	throws SQLException {
		if(cache == null)
			cache = new DBCPConnectionPool();
		return cache;
	}

	private void init()
	throws SQLException {
		
		if(basicDataSource == null){
			try {
				
				Properties prop = new Properties();
				prop.load(getClass().getResourceAsStream("/dbconn.properties"));
				String dbDriver = prop.getProperty("dbDriver");
				String dbUrl = prop.getProperty("dbUrl");
				String dbUsername = prop.getProperty("dbUsername");
				String dbPassword = prop.getProperty("dbPassword");
				boolean dbPoolPreparedStatements = StringUtil.stob(prop.getProperty("dbPoolPreparedStatements"));
				int dbMaxOpenPreparedStatements = StringUtil.stoi(prop.getProperty("dbMaxOpenPreparedStatements"));
				int dbInitialSize = StringUtil.stoi(prop.getProperty("dbInitialSize"));
				int dbMaxActive = StringUtil.stoi(prop.getProperty("dbMaxActive"));
				int dbMaxIdle = StringUtil.stoi(prop.getProperty("dbMaxIdle"));
				int dbMinIdle = StringUtil.stoi(prop.getProperty("dbMinIdle"));
				int dbMaxWait = StringUtil.stoi(prop.getProperty("dbMaxWait"));

				log.debug(dbDriver);
				log.debug(dbUrl);
				log.debug(dbUsername);
				log.debug(dbPassword);
				//log.debug(dbPoolPreparedStatements);
				//log.debug(dbMaxOpenPreparedStatements);
				//log.debug(dbInitialSize);
				//log.debug(dbMaxActive);
				//log.debug(dbMaxIdle);
				//log.debug(dbMinIdle);
				//log.debug(dbMaxWait);

				basicDataSource = new BasicDataSource();
				basicDataSource.setDriverClassName(dbDriver);
				basicDataSource.setUrl(dbUrl);
				basicDataSource.setUsername(dbUsername);
				basicDataSource.setPassword(dbPassword);
				basicDataSource.setPoolPreparedStatements(dbPoolPreparedStatements);
				basicDataSource.setMaxOpenPreparedStatements(dbMaxOpenPreparedStatements);
				basicDataSource.setInitialSize(dbInitialSize);
				basicDataSource.setMaxActive(dbMaxActive);
				basicDataSource.setMaxIdle(dbMaxIdle);
				basicDataSource.setMinIdle(dbMinIdle);
				basicDataSource.setMaxWait(dbMaxWait);
			}
			catch(Exception exception){
				throw new SQLException("Error while initializing Connection Cache in DBCPConnectionPool: " + exception.toString());
			}
		}
	}

	public synchronized Connection getConnection()
	throws SQLException	{
		return basicDataSource.getConnection();
	}

	public void close()
	throws SQLException, Exception {
		
		try {
			if(cache != null)
				cache = null;
			log.info(" NumActive: " + basicDataSource.getNumActive());
			log.info(" NumIdle: " + basicDataSource.getNumIdle());
			log.info(" TotalMemory: " + Runtime.getRuntime().totalMemory() / 1024L + "K");
			log.info(" FreeMemory: " + Runtime.getRuntime().freeMemory() / 1024L + "K");
			if(basicDataSource != null){
				basicDataSource.close();
				basicDataSource = null;
			}
		}
		catch(Exception e){
			log.error(e);
		}
	}
}

⌨️ 快捷键说明

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