DBPool 使用

dbpool的配置文件:dbpool.properties
drivers=oracle.jdbc.driver.OracleDriver
logfile=btgwdbpool.log

btgwdbpool.url=jdbc:oracle:thin:@192.168.26.188:1521:****
btgwdbpool.user=****
btgwdbpool.password=****
btgwdbpool.maxpool=5
btgwdbpool.maxconn=10
btgwdbpool.expiry=0
btgwdbpool.init=2
btgwdbpool.validator=snaq.db.AutoCommitValidator
btgwdbpool.cache=false
btgwdbpool.debug=false


操作类:DBConnectionManager.java
public class DBConnectionManager {

	private static DBConnectionManager connMgr;

	private static ConnectionPoolManager cpm = null;

	private static String propesFile = "conf/dbpool.properties";

	private static String poolName = "btgwdbpool";

	private Connection conn = null;

	private DBConnectionManager() {
		try {
			propesFile = "conf/dbpool.properties";
			poolName = "btgwdbpool";
			cpm = ConnectionPoolManager.getInstance(new File(propesFile));
		} catch (IOException e) {
			
		}
	}

	public static DBConnectionManager getInstance() {
		if (connMgr == null) {
			connMgr = new DBConnectionManager();
		}
		return connMgr;
	}

	public Connection getConnection() {
		try {
			conn = cpm.getConnection(poolName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public void finalize() {
		if (cpm != null)
			cpm.release();
	}

	public static void closeConn(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeSt(Statement st) {
		try {
			if (st != null) {
				st.close();
				st = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeRs(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(oracle,cache,jdbc)