package epantai.sql;

/**
 * @author rosdi
 *
 */

import java.sql.Connection;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;


public class ConnCache  {

	private DataSource dataSource;
  private static ConnCache _instance = null;

  /**
  * Method which returns a single instance of this class.
  **/
  public synchronized static ConnCache getInstance() throws Exception {
    if ( _instance == null ) {
       _instance = new ConnCache();
    }
    return _instance;
  }

  public Connection getConnection() throws Exception {
    if (dataSource == null) {
      throw new Exception("Connection Cache Not Properly Initialized");
    }

    return dataSource.getConnection();

  }

  /**
  * Private Constructor : This approach makes it easy to implement this class as
  * SingleTon Class.
  *
  * This method initializes Cache if not already initialized.
  **/
  public ConnCache() throws Exception {
		// Initialize the context
    Context initCtx = new InitialContext();
			  	   	  
    if(initCtx == null) {
    	throw new Exception ("BUMP! - No context.");
		}

    dataSource = (DataSource)initCtx.lookup("java:comp/env/jdbc/epantaidbPool");
  }

}
