/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.pool.connection;

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

import org.huihoo.jfox.pool.ObjectPool;

/**
 * wrap a physical connection and make it can put into pool
 * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
 */

public class SimplePoolableConnection implements PoolableConnection{

  private ObjectPool pool = null;
  private Connection conn = null;
  private boolean using = false;


  public SimplePoolableConnection(Connection conn) {
    this.conn = conn;
  }

  public ObjectPool getPool() {
    return pool;
  }

  public void setPool(ObjectPool pool) {
    this.pool = pool;
  }

  public void reallyClose() {
    try {
      conn.close();
    }
    catch(SQLException e){
      e.printStackTrace();
    }
  }

  public void activate() throws Exception {
//    System.out.println("SimplePoolableConnection.active");
    this.setUsing(true);
  }

  public void passivate() throws Exception {
    this.setUsing(false);
  }

  public void setUsing(boolean b) {
    using = b;
  }

  public boolean isUsing() {
    return using;
  }

  public Connection getConnection() {
    return conn;
  }

  // override java.sql.Connection.close() method
  // just restore itself to pool ,and setUsint false
  public void close() {
    this.setUsing(false);
    pool.restoreObject(this);
  }

}
