All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apithefire.sql.c3p0.C3p0ConnectionPool Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apithefire.sql.c3p0;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.apithefire.sql.RuntimeSqlException;
import org.apithefire.sql.connect.ConnectionPool;
import org.apithefire.sql.connect.ConnectionProvider;
import org.apithefire.sql.connect.ConnectionProviderWrapper;
import org.apithefire.util.lang.BuilderFactory;

import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PoolBackedDataSource;
import com.mchange.v2.c3p0.WrapperConnectionPoolDataSource;

/**
 * A C3P0
 * connection pool.
 * 
 * @author Chew Boon Aik
 */
public class C3p0ConnectionPool implements ConnectionPool {
	
	/**
	 * A {@link C3p0ConnectionPool} builder.
	 * 
	 * @author Chew Boon Aik
	 */
	public static class C3p0ConnectionPoolBuilder implements
			ConnectionPoolBuilder {
		
		private ConnectionProvider connectionProvider;
		private Integer minPoolSize;
		private Integer maxPoolSize;

		private C3p0ConnectionPoolBuilder() {
		}
		
		public C3p0ConnectionPoolBuilder setConnectionProvider(
				ConnectionProvider connectionProvider) {
			this.connectionProvider = connectionProvider;
			return this;
		}
		
		public C3p0ConnectionPoolBuilder setMinPoolSize(int minPoolSize) {
			this.minPoolSize = minPoolSize;
			return this;
		}

		public C3p0ConnectionPoolBuilder setMaxPoolSize(int maxPoolSize) {
			this.maxPoolSize = maxPoolSize;
			return this;
		}

		/**
		 * Builds a new {@link C3p0ConnectionPool}.
		 * 
		 * @return the build {@link C3p0ConnectionPool}
		 * @see com.mchange.v2.c3p0.DataSources#pooledDataSource
		 */
		public C3p0ConnectionPool build() {
			C3p0ConnectionPool pool = new C3p0ConnectionPool();
			
			if (connectionProvider == null) {
				throw new RuntimeSqlException("Connection provider is not set.");
			}
			pool.connectionProvider = connectionProvider;
			pool.pbds = new PoolBackedDataSource();
			pool.wcpds = new WrapperConnectionPoolDataSource();
			pool.wcpds.setNestedDataSource(ConnectionProviderWrapper.wrap(
					connectionProvider));
			
			// TODO: validate pool size?
			if (minPoolSize != null) {
				pool.wcpds.setMinPoolSize(minPoolSize);
			}
			if (maxPoolSize != null) {
				pool.wcpds.setMaxPoolSize(maxPoolSize);
			}
			try {
				pool.pbds.setConnectionPoolDataSource(pool.wcpds);
			} catch (PropertyVetoException e) {
				throw new RuntimeSqlException(e);
			}
			return pool;
		}
		
	}
	
	private ConnectionProvider connectionProvider;
	private WrapperConnectionPoolDataSource wcpds;
	private PoolBackedDataSource pbds;
	
	private C3p0ConnectionPool() {
	}
	
	/**
	 * Returns a new {@link C3p0ConnectionPoolBuilder}.
	 */
	@BuilderFactory
	public static C3p0ConnectionPoolBuilder newBuilder() {
		return new C3p0ConnectionPoolBuilder();
	}

	public int getMinPoolSize() {
		return wcpds.getMinPoolSize();
	}

	public int getMaxPoolSize() {
		return wcpds.getMaxPoolSize();
	}

	public synchronized Connection getConnection() {
		if (connectionProvider == null) {
			throw new RuntimeSqlException("Connection pool is closed.");
		}
		try {
			return pbds.getConnection();
		} catch (SQLException e) {
			throw new RuntimeSqlException(e);
		}
	}

	public synchronized void close() {
		try {
			DataSources.destroy(pbds);
			connectionProvider.close();
			connectionProvider = null;
		} catch (SQLException e) {
			throw new RuntimeSqlException(e);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy