
package net.kldp.logviewer.db.common.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import net.kldp.logviewer.common.log.LogViews;

import org.apache.log4j.Logger;


/**
 * <PRE>
 * 
 * Title : Connection Pool 객체의 초기화 및 생성 및 사용을 할수 있도록 도와준다.
 * 
 * Comment :
 * 
 * History :
 * 
 * </PRE>
 * 
 * @version 0.1, 2005. 9. 15.
 * @author henry
 */
public class ConnectionResource implements ConnectionContext {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogViews
			.getLogger(ConnectionResource.class);

	/** Data Base Connection pool name을 구축한다 */
	private String poolName= "";	
	private Connection connection = null;
	private boolean isAutoCommit = false;

	public ConnectionResource() {}
	
	/** 
	 * Connection Pool Name을 지정한다.
	 * 
	 * @param name
	 */
	protected void setPoolName(String name) {
		this.poolName = name;
	}

	/**
	 * Connection pool object를 초기화한다.
	 * 
	 * @param isAutoCommit transaction 을 생성한다.
	 * @throws Exception
	 */
	public void init(boolean isAutoCommit) throws Exception {
		this.isAutoCommit = isAutoCommit;
		
		try {
			connection = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + poolName);
			
		} catch(SQLException sqle) {

			// Connection 객체가 없으므로 Connection Pool 객체를 생성한다.
			ConnectionInitialize conInit = new ConnectionInitialize();
			conInit.init(poolName);
			
			// 등록한 이름으로 드라이버를 다시 찾는다.
			connection = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + poolName);
		}
		
		// Autocommit 으로 실행할것인지 여부
		if (isAutoCommit) {
		    connection.setAutoCommit(true);
		} else {
		    connection.setAutoCommit(false);
	//	    connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		}
		
		if (connection == null) {
			throw new Exception("Connection Pool 객체를 초기화 하는데 실패했습니다.");
		}
		
//		logger.debug("Connection Pool 객체를 초기화 했습니다.");
	}

	/**
	 * Connection return
	 */
	public Connection getConnection() {
		return connection;
	}

	/**
	 * rollback
	 */
	public void rollback() {
		if (!isAutoCommit) {
			if (connection != null) {
				try { connection.rollback(); } catch (Exception e) {}
			} // connection if end
		} // transaction end if
	}

	/**
	 * commit
	 */
	public void commit() {
		if (!isAutoCommit) {
			if (connection != null) {
				try { connection.commit(); } catch (Exception e) {}
			} // connection if end
		} // transaction end if
	}

	/**
	 * release
	 */
	public void release() {
		if (connection != null) {
			if (!isAutoCommit) {
				try { connection.setAutoCommit(true); } catch (Exception e) {}
			}
			try { connection.close(); } catch (Exception e) {}
		}
	}
	
    public void printDataSourceStats() throws SQLException {
//        BasicDataSource bds = (BasicDataSource) ds;
//        System.out.println("NumActive: " + bds.getNumActive());
//        System.out.println("NumIdle: " + bds.getNumIdle());
    	
        
    }

//    public static void shutdownDataSource(DataSource ds) throws SQLException {
//        BasicDataSource bds = (BasicDataSource) ds;
//        bds.close();
//    }
}
