Package org.infinispan.loaders.jdbc.connectionfactory

Source Code of org.infinispan.loaders.jdbc.connectionfactory.PooledConnectionFactory

package org.infinispan.loaders.jdbc.connectionfactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import org.infinispan.loaders.CacheLoaderException;
import org.infinispan.loaders.jdbc.JdbcUtil;
import org.infinispan.loaders.jdbc.logging.Log;
import org.infinispan.commons.util.FileLookupFactory;
import org.infinispan.util.logging.LogFactory;

import java.beans.PropertyVetoException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
* Pooled connection factory based on C3P0. For a complete configuration reference, look <a
* href="http://www.mchange.com/projects/c3p0/index.html#configuration">here</a>. The connection pool can be configured
* in various ways, as described <a href="http://www.mchange.com/projects/c3p0/index.html#configuration_files">here</a>.
* The simplest way is by having an <tt>c3p0.properties</tt> file in the classpath. If no such file is found, default,
* hardcoded values will be used.
*
* @author Mircea.Markus@jboss.com
* @author Tristan Tarrant
*/
public class PooledConnectionFactory extends ConnectionFactory {

   private static final Log log = LogFactory.getLog(PooledConnectionFactory.class, Log.class);
   private ComboPooledDataSource pooledDataSource;

   @Override
   public void start(ConnectionFactoryConfig config, ClassLoader classLoader) throws CacheLoaderException {
      logFileOverride(classLoader);
      pooledDataSource = new ComboPooledDataSource();
      pooledDataSource.setProperties(new Properties());
      try {
         /* Since c3p0 does not throw an exception when it fails to load a driver we attempt to do so here
          * Also, c3p0 does not allow specifying a custom classloader, so use c3p0's
          */
         Class.forName(config.getDriverClass(), true, ComboPooledDataSource.class.getClassLoader());
         pooledDataSource.setDriverClass(config.getDriverClass()); //loads the jdbc driver
      } catch (Exception e) {
         log.errorInstantiatingJdbcDriver(config.getDriverClass(), e);
         throw new CacheLoaderException(String.format(
               "Error while instatianting JDBC driver: '%s'", config.getDriverClass()), e);
      }
      pooledDataSource.setJdbcUrl(config.getConnectionUrl());
      pooledDataSource.setUser(config.getUserName());
      pooledDataSource.setPassword(config.getPassword());
      if (log.isTraceEnabled()) {
         log.tracef("Started connection factory with config: %s", config);
      }
   }

   private void logFileOverride(ClassLoader classLoader) {
      URL propsUrl = FileLookupFactory.newInstance().lookupFileLocation("c3p0.properties", classLoader);
      URL xmlUrl = FileLookupFactory.newInstance().lookupFileLocation("c3p0-config.xml", classLoader);
      if (log.isDebugEnabled() && propsUrl != null) {
         log.debugf("Found 'c3p0.properties' in classpath: %s", propsUrl);
      }
      if (log.isDebugEnabled() && xmlUrl != null) {
         log.debugf("Found 'c3p0-config.xml' in classpath: %s", xmlUrl);
      }
   }

   @Override
   public void stop() {
      try {
         DataSources.destroy(pooledDataSource);
         if (log.isDebugEnabled()) {
            log.debug("Successfully stopped PooledConnectionFactory.");
         }
      }
      catch (SQLException sqle) {
         log.couldNotDestroyC3p0ConnectionPool(pooledDataSource!=null?pooledDataSource.toString():null, sqle);
      }
   }

   @Override
   public Connection getConnection() throws CacheLoaderException {
      try {
         logBefore(true);
         Connection connection = pooledDataSource.getConnection();
         logAfter(connection, true);
         return connection;
      } catch (SQLException e) {
         throw new CacheLoaderException("Failed obtaining connection from PooledDataSource", e);
      }
   }

   @Override
   public void releaseConnection(Connection conn) {
      logBefore(false);
      JdbcUtil.safeClose(conn);
      logAfter(conn, false);
   }

   public ComboPooledDataSource getPooledDataSource() {
      return pooledDataSource;
   }

   private void logBefore(boolean checkout) {
      if (log.isTraceEnabled()) {
         String operation = checkout ? "checkout" : "release";
         try {
            log.tracef("DataSource before %s (NumBusyConnectionsAllUsers) : %d, (NumConnectionsAllUsers) : %d",
                       operation, pooledDataSource.getNumBusyConnectionsAllUsers(), pooledDataSource.getNumConnectionsAllUsers());
         } catch (SQLException e) {
            log.sqlFailureUnexpected(e);
         }
      }
   }

   private void logAfter(Connection connection, boolean checkout)  {
      if (log.isTraceEnabled()) {
         String operation = checkout ? "checkout" : "release";
         try {
            log.tracef("DataSource after %s (NumBusyConnectionsAllUsers) : %d, (NumConnectionsAllUsers) : %d",
                      operation, pooledDataSource.getNumBusyConnectionsAllUsers(), pooledDataSource.getNumConnectionsAllUsers());
         } catch (SQLException e) {
            log.sqlFailureUnexpected(e);
         }
         log.tracef("Connection %s : %s", operation, connection);
      }
   }
}
TOP

Related Classes of org.infinispan.loaders.jdbc.connectionfactory.PooledConnectionFactory

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.