<commit_before>// Copyright (c) 2001  <NAME> <<EMAIL>>
//
// arch-tag: 6B85CEEA-1110-11D9-B887-000A957659CC

package net.spy.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;

import net.spy.SpyObject;
import net.spy.pool.JDBCPoolFiller;
import net.spy.pool.ObjectPool;
import net.spy.pool.PoolException;
import net.spy.pool.PooledObject;
import net.spy.util.SpyConfig;

/**
 * Connection source to retrieve connections from an ObjectPool.
 *
 * The configuration passed into getConnection() requires the following
 * parameters:
 *
 * <ul>
 *  <li>dbDriverName - Driver to load (i.e. org.postgresql.Driver)</li>
 *  <li>dbSource - dbUser</li>
 *  <li>dbUser - Database username</li>
 *  <li>dbPass - Database password</li>
 * </ul>
 *
 * The following parameters are optional:
 *
 * <ul>
 *  <li>dbPoolName - default: <i>db</i></li>
 *  <li>dbMinConns - minimum number of connections - default 1</li>
 *  <li>dbStartConns - minimum number of connections - default 1</li>
 *  <li>dbYellowLine - the pool's ``yellow line'' percentage
 *      - default 75</li>
 *  <li>dbMaxConns - maximum number of connections - default 5</li>
 *  <li>dbMaxLifeTime - maximum connection lifetime in milliseconds -
 *      default 6 hours</li>
 *  <li>dbPingOnCheckout - if true, ping on object pool checkout
 *      default: true</li>
 * </ul>
 */
public class ObjectPoolConnectionSource extends SpyObject
	implements ConnectionSource {

	private static final int DEFAULT_MAX_CONNS=5;

	// This is the object pool from which connections will be retrieved
	private static ObjectPool pool=null;

	// This is the pooled object that will be held until the connection is
	// finished.
	private PooledObject object=null;

	// The name of the pool referenced by this instance.
	private String poolName=null;

	/**
	 * Get an instance of SpyPoolConnectionSource.
	 */
	public ObjectPoolConnectionSource() {
		super();
	}

	/**
	 * @see ConnectionSource
	 */
	public Connection getConnection(SpyConfig conf) throws SQLException {
		// Get the pool name
		poolName=conf.get("dbPoolName");

		// Verify the pool is properly initialized
		initialize(conf);

		Connection conn=null;
		try {
			// Snatch the pebble from my hand.
			conn=getConn(poolName);
		} catch(PoolException pe) {
			getLogger().warn("Could not get a DB connection", pe);
			throw new DBInitException("Could not get a DB connection:  " + pe);
		}
		return(conn);
	}

	// Do the pool work.
	private Connection getConn(String name)
		throws SQLException, PoolException {

		Connection rv=null;
		object=pool.getObject(name);
		rv=(Connection)object.getObject();

		return(rv);
	}

	/**
	 * @see ConnectionSource
	 */
	public void returnConnection(Connection conn) {
		if(object==null) {
			throw new NullPointerException("Object is null, already returned?");
		}
		object.checkIn();
		object=null;
	}

	// Perform one-time initialization for a config.<commit_msg>Get rid of the instance variable holding the latest pooled object.  wrong<commit_after>// Copyright (c) 2001  <NAME> <<EMAIL>>
//
// arch-tag: 6B85CEEA-1110-11D9-B887-000A957659CC

package net.spy.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Map;
import java.util.WeakHashMap;

import net.spy.SpyObject;
import net.spy.pool.JDBCPoolFiller;
import net.spy.pool.ObjectPool;
import net.spy.pool.PoolException;
import net.spy.pool.PooledObject;
import net.spy.util.SpyConfig;

/**
 * Connection source to retrieve connections from an ObjectPool.
 *
 * The configuration passed into getConnection() requires the following
 * parameters:
 *
 * <ul>
 *  <li>dbDriverName - Driver to load (i.e. org.postgresql.Driver)</li>
 *  <li>dbSource - dbUser</li>
 *  <li>dbUser - Database username</li>
 *  <li>dbPass - Database password</li>
 * </ul>
 *
 * The following parameters are optional:
 *
 * <ul>
 *  <li>dbPoolName - default: <i>db</i></li>
 *  <li>dbMinConns - minimum number of connections - default 1</li>
 *  <li>dbStartConns - minimum number of connections - default 1</li>
 *  <li>dbYellowLine - the pool's ``yellow line'' percentage
 *      - default 75</li>
 *  <li>dbMaxConns - maximum number of connections - default 5</li>
 *  <li>dbMaxLifeTime - maximum connection lifetime in milliseconds -
 *      default 6 hours</li>
 *  <li>dbPingOnCheckout - if true, ping on object pool checkout
 *      default: true</li>
 * </ul>
 */
public class ObjectPoolConnectionSource extends SpyObject
	implements ConnectionSource {

	private static final int DEFAULT_MAX_CONNS=5;

	// This is the object pool from which connections will be retrieved
	private static ObjectPool pool=null;

	private Map<Connection, PooledObject> objects=null;

	// The name of the pool referenced by this instance.
	private String poolName=null;

	/**
	 * Get an instance of SpyPoolConnectionSource.
	 */
	public ObjectPoolConnectionSource() {
		super();
		objects=new WeakHashMap<Connection, PooledObject>();
	}

	/**
	 * @see ConnectionSource
	 */
	public Connection getConnection(SpyConfig conf) throws SQLException {
		// Get the pool name
		poolName=conf.get("dbPoolName");

		// Verify the pool is properly initialized
		initialize(conf);

		Connection conn=null;
		try {
			// Snatch the pebble from my hand.
			conn=getConn(poolName);
		} catch(PoolException pe) {
			getLogger().warn("Could not get a DB connection", pe);
			throw new DBInitException("Could not get a DB connection:  " + pe);
		}
		return(conn);
	}

	// Do the pool work.
	private Connection getConn(String name)
		throws SQLException, PoolException {

		Connection rv=null;
		PooledObject object=pool.getObject(name);
		rv=(Connection)object.getObject();
		objects.put(rv, object);

		return(rv);
	}

	/**
	 * @see ConnectionSource
	 */
	public void returnConnection(Connection conn) {
		PooledObject object=objects.get(conn);
		if(object==null) {
			throw new NullPointerException("Object is null, already returned?");
		}
		object.checkIn();
		objects.remove(conn);
	}

	// Perform one-time initialization for a config.
{'fraction_non_alphanumeric': 0.11741489886531821, 'fraction_numerical': 0.010853478046373951, 'mean_word_length': 3.853950518754988, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1785267', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1881, 'n_words': 549}