📄 conncache.java
字号:
package com.yhcms.db;import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Date;import java.util.Properties;import com.yhcms.utils.DateUtils;import org.apache.commons.dbcp.*;import org.apache.commons.pool.ObjectPool;/** * <p>Title:数据库链接池</p> * <li>根据配置文件,初始化数据库连接池</li> * <b>CopyRight: yyhweb[由由华网]</b> * @author stephen * @version YH-2.0 */public class ConnCache{ private String sysPath; private String email; private BasicDataSource basicDS; private ObjectPool objPool; private static ConnCache connCache = null; private ConnCache() throws Exception { sysPath = ""; email = "yyhweb@126.com"; basicDS = null; objPool = null; init(); } /** * @return 获得一个链接池类的实例 * @throws Exception */ public static synchronized ConnCache getInstance() throws Exception { if(connCache == null){ connCache = new ConnCache(); } return connCache; } private void init() throws Exception { if(basicDS == null){ InputStream inputstream = null; Properties properties = null; try { // 加载数据库属性配置文件 inputstream = getClass().getResourceAsStream("/properties/db.properties"); properties = new Properties(); properties.load(inputstream); String dbDriver = properties.getProperty("dbDriver"); String dbUrl = properties.getProperty("dbUrl"); String dbUsername = properties.getProperty("dbUsername"); String dbPassword = properties.getProperty("dbPassword"); sysPath = properties.getProperty("Path", ""); email = properties.getProperty("connEmail", "yyhweb@126.com"); boolean flag = Boolean.valueOf(properties.getProperty("dbRemoveAbandoned", "false")).booleanValue(); boolean flag1 = Boolean.valueOf(properties.getProperty("dbPoolPreparedStatements", "false")).booleanValue(); int i = Integer.parseInt(properties.getProperty("dbMaxOpenPreparedStatements", "50")); int j = Integer.parseInt(properties.getProperty("dbInitialSize", "3")); int k = Integer.parseInt(properties.getProperty("dbMaxActive", "20")); int l = Integer.parseInt(properties.getProperty("dbMaxIdle", "10")); int i1 = Integer.parseInt(properties.getProperty("dbMinIdle", "1")); long l1 = Long.parseLong(properties.getProperty("dbMaxWait", "-1")); basicDS = new BasicDataSource(); basicDS.setDriverClassName(dbDriver); basicDS.setUrl(dbUrl); basicDS.setUsername(dbUsername); basicDS.setPassword(dbPassword); basicDS.setPoolPreparedStatements(flag1); basicDS.setMaxOpenPreparedStatements(i); basicDS.setInitialSize(j); basicDS.setMaxActive(k); basicDS.setMaxIdle(l); basicDS.setMinIdle(i1); basicDS.setMaxWait(l1); basicDS.setRemoveAbandoned(flag); }catch(Exception exception) { throw new Exception("Error while initializing Connection Cache in ConnectionCache: " + exception.toString()); }finally{ inputstream.close(); } } } /** * @return 获得一个数据库的链接 * @throws SQLException */ public Connection getConnection() throws SQLException { return basicDS.getConnection(); } /** * @return 系统路径 */ public String getSysPath() { return sysPath; } /** * @return 获得系统管理员Email */ public String getEmail() { return email; } /** * @throws SQLException * @throws Exception */ public void close() throws SQLException, Exception { try { if(connCache != null){ connCache = null; } if(basicDS != null) { basicDS.close(); basicDS = null; } } finally { if(objPool != null) { objPool.close(); objPool = null; } } } /** * @return 当前活动的链接数据库的数目 */ public String getConnNum(){ if(basicDS != null) { StringBuffer stringbuffer = new StringBuffer(); stringbuffer.append(DateUtils.getDate2LStr2(new Date())); stringbuffer.append("==> NumActive: " + basicDS.getNumActive()); stringbuffer.append(" NumIdle: " + basicDS.getNumIdle()); return stringbuffer.toString(); } else { return "Can't get Connection Status."; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -