ConnectionOraclePool.java 3.94 KB
package com.sitech.database;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;

import com.sitech.util.Formater;


/** 
 * @author zhangdj Email:Mirror_Jean@qq.com 
 * @version , Oct 22, 2010 
 */
public class ConnectionOraclePool {
	private static int iDrivertype = 0;

	private static String sUserName;

	private static String sPassword;

	private static String sUrl;

	private static String sClassforName;

	private static boolean waitingcloseconnection = false;

	private static ConnectionOraclePool instance = new ConnectionOraclePool();

	static private java.sql.Connection conn = null;

	static private java.util.Date connDate = null;

	static private int connCount = 0;

	static private int currconnCount = 0;

	private boolean bCommitstate;

	private ConnectionOraclePool() {
	}

	public static ConnectionOraclePool getInstance() {
		return instance;
	}

	public java.sql.Connection Connection() throws SQLException {
		return conn;
	}

	public synchronized void increase(int i) {
		connCount++;
		currconnCount = currconnCount + i;
	}

	synchronized public java.sql.Connection createConnection()
			throws SQLException {
		if (waitingcloseconnection) {
			try {
				while (waitingcloseconnection) {
					Thread.sleep(1);
				}
			} catch (Exception e) {
			}
		}
		increase(1);
		if (conn != null && !conn.isClosed())
			return conn;
		try {
			Class.forName(sClassforName).newInstance();
			conn = DriverManager.getConnection(sUrl, sUserName, sPassword);
			connDate = new Date();
			connCount = 0;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}

	synchronized public void closeConn() throws SQLException {
		Date preRefresh = Formater.computeDate(new Date(), 'f', -10);// l��10������Ч
		if (preRefresh.after(connDate) || connCount > 500) {
			if (conn != null) {
				if (currconnCount > 1) {
					waitingcloseconnection = true;
					try {
						int i = 0;
						while (currconnCount > 1 && i < 10) {
							Thread.sleep(80); // �ȴ�50���룬��ǰ����ִ����ϣ���Լ2���ɱ������������κ������Ҫ��2������ɡ�
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				conn.close();
				conn = null;
				increase(-1);
				waitingcloseconnection = false;
			}
		}
	}

	protected void finalize() throws Throwable {
		if (conn != null) {
			conn.close();
			conn = null;
		}
	}

	public java.sql.Statement createStatement() throws SQLException {

		if (conn != null)
			return conn.createStatement();
		else
			return null;
	}

	public java.sql.Statement createScrollStatement() throws SQLException {

		if (conn != null)
			if (iDrivertype == 0)
				return conn.createStatement();
			else
				return conn.createStatement(
						java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
						java.sql.ResultSet.CONCUR_READ_ONLY);
		else
			return null;
	}

	public java.sql.PreparedStatement createPreparedStatement(String sql)
			throws SQLException {

		if (conn != null)
			return conn.prepareStatement(sql);
		else
			return null;
	}

	/* execute Prepared Statement */

	public void executePreparedStmt(java.sql.PreparedStatement pstmt)
			throws SQLException {
		if (conn != null) {
			pstmt.executeUpdate();
		} else
			return;

	}

	/* execute Prepared Statement */

	public java.sql.CallableStatement createPreparedCall(String sql)
			throws SQLException {
		if (conn != null) {
			return conn.prepareCall(sql);
		} else
			return null;

	}

	public void beginTransaction() throws SQLException {

		if (conn != null) {
			bCommitstate = conn.getAutoCommit();
			conn.setAutoCommit(false);
		}
	}

	public void endTransaction() throws SQLException {

		if (conn != null) {
			if (bCommitstate != conn.getAutoCommit())
				conn.setAutoCommit(bCommitstate);
		}
	}

	public void commit() throws SQLException {

		if (conn != null)
			conn.commit();
	}

	public void rollback() throws SQLException {

		if (conn != null)
			conn.rollback();
	}
	
}