com.flexoodb.pool.ConnectionPoolFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.flexoodb.pool.ConnectionPoolFactory.java

Source

/*
 * ConnectionPoolFactory.java
 *
 * Created on Mar 19, 2009 7:01:46 PM
 *
 * Copyright (C) 2009 Jayson Yu
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

package com.flexoodb.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.commons.pool.PoolableObjectFactory;

/**
 * implementation responsible for instantiating additional Connection instances.
 *
 * @author      Jayson Yu
 * @since       1.0
 */
public class ConnectionPoolFactory implements PoolableObjectFactory {

    private String classname, dbsurl, username, password;

    /**
    * create factory instance.
    *
    * @param  odbclass odbc classname.
    * @param  odbcurl odbc url.
    * @param  user username for the connection.
    * @param  pass username's password.
     */
    public ConnectionPoolFactory(String odbclass, String odbcurl, String user, String pass) {
        classname = odbclass;
        dbsurl = odbcurl;
        username = user;
        password = pass;

    }

    /**
    * create factory instance.
    *
    * @return returns an available connection.
    * @see java.sql.Connection
    */
    public Object makeObject() {

        Connection con = null;
        int tries = 10;
        boolean connected = false;
        for (int i = 0; i < tries && !connected; i++) {
            try {
                Class.forName(this.classname).newInstance();
                con = (Connection) DriverManager.getConnection(this.dbsurl, this.username, this.password);
                connected = true;
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    Thread.sleep(2000);
                } catch (Exception f) {
                }
            }
        }

        if (!connected) {

            //_log.warn("Could not connect to "+this.dbsurl);
        }
        return con;
    }

    public void passivateObject(Object r) {
    }

    public void activateObject(Object r) {
    }

    public boolean validateObject(Object r) {
        boolean isok = false;
        PreparedStatement stmt = null;
        try {
            Connection con = null;
            con = (Connection) r;
            stmt = con.prepareStatement("select count(*) from INTIGRIXLOOKUPNOTABLE");
            isok = true;
        } catch (Exception e) {
            if (e.getMessage().indexOf("does not exist") >= 0 || e.getMessage().indexOf("Table not found") >= 0) {
                isok = true;
            } else {
                //_log.error(e);
            }
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                }
            }
        }
        return isok;
    }

    public void destroyObject(Object r) {
        try {
            ((Connection) r).close();
        } catch (Exception e) {
            //_log.error(e.getMessage(),e);
        }
    }
}