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

com.guicedee.guicedpersistence.c3p0.C3P0ConnectionBaseInfo Maven / Gradle / Ivy

Go to download

Enables C3P0 with Hibernate connections in the GuicedPersistence provider Requires JDK 8 and up

There is a newer version: 62
Show newest version
package com.guicedee.guicedpersistence.c3p0;

import com.guicedee.guicedpersistence.c3p0.implementations.C3P0ConnectionPropertiesReader;
import com.guicedee.guicedpersistence.db.ConnectionBaseInfo;
import com.guicedee.guicedpersistence.jpa.JPAConnectionBaseInfo;
import com.guicedee.logger.LogFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;

import javax.sql.DataSource;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class C3P0ConnectionBaseInfo
		extends JPAConnectionBaseInfo
{
	private static final Logger log = LogFactory.getLog(C3P0ConnectionBaseInfo.class.getName());

	@Override
	public ConnectionBaseInfo populateFromProperties(ParsedPersistenceXmlDescriptor unit, Properties filteredProperties)
	{
		ConnectionBaseInfo cbi = super.populateFromProperties(unit, filteredProperties);
		C3P0ConnectionPropertiesReader.getPersistenceUnitSpecificMappings()
		                              .put(unit.getName(), new C3P0ConnectionPropertiesReader());
		return cbi;
	}

	@Override
	public DataSource toPooledDatasource()
	{
		ComboPooledDataSource cpds = new ComboPooledDataSource();
		if (getDriverClass() == null)
		{
			try
			{
				cpds.setDriverClass(getDriverClass());
			}
			catch (Exception e)
			{
				log.log(Level.WARNING, "Cannot set driver class for C3P0 Combo Pooled Data Source", e);
			}
		}

		if (getUrl() != null)
		{
			cpds.setJdbcUrl(getUrl());
		}
		if (getUsername() != null)
		{
			cpds.setUser("root");
		}
		if (getPassword() != null)
		{
			cpds.setPassword("root");
		}

		// the settings below are optional -- c3p0 can work with defaults
		if (getMinPoolSize() != null)
		{
			cpds.setMinPoolSize(getMinPoolSize());
		}
		else
		{
			cpds.setMinPoolSize(5);
		}
		if (getAcquireIncrement() != null)
		{
			cpds.setAcquireIncrement(getAcquireIncrement());
		}
		else
		{
			cpds.setAcquireIncrement(5);
		}
		if (getMaxPoolSize() != null)
		{
			cpds.setMaxPoolSize(getMaxPoolSize());
		}
		else
		{
			cpds.setMaxPoolSize(20);
		}

		if (getPreparedStatementCacheSize() != null)
		{
			cpds.setMaxStatements(getPreparedStatementCacheSize());
		}
		else
		{
			cpds.setMaxStatements(180);
		}

		return cpds;
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy