// Copyright (c) 2007 Keith D Gregory
package com.kdgregory.example.ref.phantom;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.Queue;
/**
* Manages a pool of database connections, using phantom references to
* return the connections to the pool if the caller forgets. To make
* this work, we use a {@link PooledConnection}, which wraps the actual
* connection and delegates most operations. The user is given this
* object, and we maintain a phantom reference to it, along with a
* strong reference to the actual connection. When the phantom gets
* queued, we return the actual connection to the pool.
*
* Since this is demo code, we create the maximum number of connections
* when the pool is constructed, and don't try to regenerate connections
* that get closed.
*/
public class ConnectionPool
{
private Queue _pool = new LinkedList();
private ReferenceQueue