Package br.com.objectos.comuns.relational.jdbc

Source Code of br.com.objectos.comuns.relational.jdbc.C3P0PoolProvider

/*
* Copyright 2011 Objectos, Fábrica de Software LTDA.
*
* Licensed 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 br.com.objectos.comuns.relational.jdbc;

import java.beans.PropertyVetoException;

import br.com.objectos.comuns.sql.JdbcCredentials;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
public class C3P0PoolProvider implements Provider<ComboPooledDataSource> {

  private final JdbcCredentials credentials;

  @Inject
  public C3P0PoolProvider(@C3P0 JdbcCredentials credentials) {
    this.credentials = credentials;
  }

  @Override
  public final ComboPooledDataSource get() {
    ComboPooledDataSource dataSource = new ComboPooledDataSource();

    try {

      dataSource.setDriverClass(credentials.getDriverClass());
      dataSource.setJdbcUrl(credentials.getUrl());
      dataSource.setUser(credentials.getUser());
      dataSource.setPassword(credentials.getPassword());

      configureDataSource(dataSource);

    } catch (PropertyVetoException e) {

      // it is the very first set for the driverClass
      // on a side note: that is why immutability is cool and the opposite
      // (sometimes) is not.

    }

    return dataSource;
  }

  protected void configureDataSource(ComboPooledDataSource dataSource) {
    dataSource.setInitialPoolSize(0);
    dataSource.setMinPoolSize(0);
    dataSource.setMaxPoolSize(50);

    dataSource.setIdleConnectionTestPeriod(3600);
    dataSource.setMaxIdleTime(120);
    dataSource.setPreferredTestQuery("select 1");
  }

}
TOP

Related Classes of br.com.objectos.comuns.relational.jdbc.C3P0PoolProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.