Package anvil.database.jdbc

Source Code of anvil.database.jdbc.JDBCFactory

/*
* $Id: JDBCFactory.java,v 1.3 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.database.jdbc;

import anvil.server.PoolPreferences;
import anvil.database.PooledConnection;
import anvil.database.ConnectionPool;
import anvil.database.ConnectionFactory;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

/**
* Factory used by <code>ConnectionPool</code> to create the
* actual <code>PooledConnection</code> instances.
*
* @see PooledConnection
* @see ConnectionPool
* @version $Revision: 1.3 $
* @author Jani Lehtim�ki
*/
public class JDBCFactory implements ConnectionFactory
{

    private ConnectionPool pool;
   
    /**
     * Count of connections created.
     */
    private long count = 0;

    /**
     * Initializes new factory.
     *
     * @param url      URL (connection string)
     * @param username Username
     * @param password Password 
     */
    public void initialize(ConnectionPool pool)
    {
        this.pool = pool;
        String driverClassname = pool.getDriver();
        try {
          Class driverClass = Class.forName(driverClassname);
          if (driverClass != null) {
            Driver driver = (Driver)driverClass.newInstance();
            DriverManager.registerDriver(driver);
            pool.getZone().log().info("JDBCFactory for pool '"+pool.getName()+"' initialized, driver=" + driverClass);
          }
        } catch (Exception exception) {
          pool.getZone().log().error("Couldn't initialize driver: " + driverClassname, exception);
        }
    }


    /**
     * Creates new connection.
     *
     * @param toPool Connection pool
     * @return <code>PooledConnection</code>
     * @throws SQLException If something goes wrong
     */
    public PooledConnection create() throws Exception {
        long started = System.currentTimeMillis();
        Connection connection;
        connection = DriverManager.getConnection(pool.getURL(), pool.getUsername(), pool.getPassword());
        count++;
        JDBCConnection connectionImpl =
          new JDBCConnection(pool, pool.getName() + '-' + count, connection);
        pool.connectionCreated(connectionImpl, System.currentTimeMillis() - started);
        return connectionImpl;
    }
}
TOP

Related Classes of anvil.database.jdbc.JDBCFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.