/*
 * Copyright (C) 2004-2005  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.lib;

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

final class ConnectionPool
{
	// TODO: allow changing pool size
	// TODO: gather pool effectivity statistics
	
	private final Connection[] pool = new Connection[10];
	private int size = 0;
	private final Object lock = new Object();
	private final Properties properties;

	ConnectionPool(final Properties properties)
	{
		this.properties = properties;
	}

	final Connection getConnection() throws SQLException
	{
		synchronized(lock)
		{
			if(size>0)
			{
				//System.out.println("connection pool: fetch "+(size-1));
				return pool[--size];
			}
			else
			{
				// TODO: may be one could do this outside the synchronzed block ??
				return createConnection();
			}
		}
	}

	private final Connection createConnection() throws SQLException
	{
		final String url = properties.getDatabaseUrl();
		final String user = properties.getDatabaseUser();
		final String password = properties.getDatabasePassword();

		//System.out.println("connection pool: CREATE");
		return DriverManager.getConnection(url, user, password);
	}

	final void putConnection(final Connection connection) throws SQLException
	{
		synchronized(lock)
		{
			if(size<pool.length)
			{
				//System.out.println("connection pool: store "+size);
				pool[size++] = connection;
				return;
			}
		}
		
		//System.out.println("connection pool: CLOSE ");

		// Important to do this outside the synchronized block!
		connection.close();
	}

}

@@ -1149,16 +1149,17 @@
 ject();%0A
+%0A
 %09private
@@ -1165,37 +1165,86 @@
 e final 
-Properties properties
+String url;%0A%09private final String user;%0A%09private final String password
 ;%0A%0A%09Conn
@@ -1293,16 +1293,22 @@
 %0A%09%09this.
+url = 
 properti
@@ -1309,29 +1309,129 @@
 operties
- = properties
+.getDatabaseUrl();%0A%09%09this.user = properties.getDatabaseUser();%0A%09%09this.password = properties.getDatabasePassword()
 ;%0A%09%7D%0A%0A%09f
@@ -1820,170 +1820,8 @@
 n%0A%09%7B
-%0A%09%09final String url = properties.getDatabaseUrl();%0A%09%09final String user = properties.getDatabaseUser();%0A%09%09final String password = properties.getDatabasePassword();
 %0A%0A%09%09

save connection properties directly