首页

分享datanucleus实现常见数据库连接池bonecp、c3p0、dbcp及proxool自定义数据源工厂类DataSourceFactory实现源码

标签:bonecp,c3p0,dbcp,proxool,datasource,datanucleus,connectionpool,数据源,连接池     发布时间:2018-01-16   

一、前言

通过datanucleus开源包datanucleus-connectionpool-2.0.3.jar中的org.datanucleus.store.rdbms.datasource包定义数据工厂类接口org.datanucleus.store.rdbms.datasource.DataNucleusDataSourceFactory,并通过bonecpc3p0dbcpproxool的具体org.datanucleus.store.rdbms.datasource.bonecp.BoneCPDataSourceFactory、org.datanucleus.store.rdbms.datasource.bonecp.C3P0DataSourceFactory、org.datanucleus.store.rdbms.datasource.bonecp.DBCPDataSourceFactory、org.datanucleus.store.rdbms.datasource.bonecp.ProxoolDataSourceFactory数据源实现工厂类。

二、源码说明

1.BoneCPDataSourceFactory

package org.datanucleus.store.rdbms.datasource.bonecp;@b@@b@import com.jolbox.bonecp.BoneCPConfig;@b@import com.jolbox.bonecp.BoneCPDataSource;@b@import javax.sql.DataSource;@b@import org.datanucleus.ClassLoaderResolver;@b@import org.datanucleus.OMFContext;@b@import org.datanucleus.PersistenceConfiguration;@b@import org.datanucleus.store.StoreManager;@b@import org.datanucleus.store.rdbms.datasource.DataNucleusDataSourceFactory;@b@import org.datanucleus.store.rdbms.datasource.DatastoreDriverNotFoundException;@b@import org.datanucleus.util.ClassUtils;@b@@b@public class BoneCPDataSourceFactory@b@  implements DataNucleusDataSourceFactory@b@{@b@  public DataSource makePooledDataSource(OMFContext omfCtx)@b@  {@b@    int size;@b@    PersistenceConfiguration conf = omfCtx.getPersistenceConfiguration();@b@    StoreManager storeMgr = omfCtx.getStoreManager();@b@    String dbDriver = storeMgr.getConnectionDriverName();@b@    String dbURL = storeMgr.getConnectionURL();@b@    String dbUser = storeMgr.getConnectionUserName();@b@    String dbPassword = storeMgr.getConnectionPassword();@b@    ClassLoaderResolver clr = omfCtx.getClassLoaderResolver(null);@b@    try@b@    {@b@      Class.forName(dbDriver);@b@    }@b@    catch (ClassNotFoundException cnfe)@b@    {@b@      try@b@      {@b@        clr.classForName(dbDriver);@b@      }@b@      catch (RuntimeException e)@b@      {@b@        throw new DatastoreDriverNotFoundException(dbDriver);@b@      }@b@@b@    }@b@@b@    ClassUtils.assertClassForJarExistsInClasspath(clr, "com.jolbox.bonecp.BoneCPDataSource", "bonecp.jar");@b@@b@    BoneCPDataSource ds = new BoneCPDataSource();@b@@b@    if (conf.hasProperty("datanucleus.connectionPool.maxStatements"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.maxStatements");@b@      if (size >= 0)@b@      {@b@        ds.setStatementsCacheSize(size);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.maxPoolSize"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.maxPoolSize");@b@      if (size >= 0)@b@      {@b@        ds.setMaxConnectionsPerPartition(size);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.minPoolSize"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.minPoolSize");@b@      if (size >= 0)@b@      {@b@        ds.setMinConnectionsPerPartition(size);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.maxIdle"))@b@    {@b@      int value = conf.getIntProperty("datanucleus.connectionPool.maxIdle");@b@      if (value > 0)@b@      {@b@        ds.setIdleMaxAge(value);@b@      }@b@    }@b@@b@    ds.setJdbcUrl(dbURL);@b@    ds.setUsername(dbUser);@b@    ds.setPassword(dbPassword);@b@@b@    return ds;@b@  }@b@}

2.C3P0DataSourceFactory

package org.datanucleus.store.rdbms.datasource.c3p0;@b@@b@import com.mchange.v2.c3p0.ComboPooledDataSource;@b@import java.beans.PropertyVetoException;@b@import javax.sql.DataSource;@b@import org.datanucleus.ClassLoaderResolver;@b@import org.datanucleus.OMFContext;@b@import org.datanucleus.PersistenceConfiguration;@b@import org.datanucleus.store.StoreManager;@b@import org.datanucleus.store.rdbms.datasource.DataNucleusDataSourceFactory;@b@import org.datanucleus.store.rdbms.datasource.DatastoreDriverNotFoundException;@b@import org.datanucleus.store.rdbms.datasource.DatastorePoolException;@b@import org.datanucleus.util.ClassUtils;@b@@b@public class C3P0DataSourceFactory@b@  implements DataNucleusDataSourceFactory@b@{@b@  public DataSource makePooledDataSource(OMFContext omfCtx)@b@  {@b@    int size;@b@    PersistenceConfiguration conf = omfCtx.getPersistenceConfiguration();@b@    StoreManager storeMgr = omfCtx.getStoreManager();@b@    String dbDriver = storeMgr.getConnectionDriverName();@b@    String dbURL = storeMgr.getConnectionURL();@b@    String dbUser = storeMgr.getConnectionUserName();@b@    String dbPassword = storeMgr.getConnectionPassword();@b@    ClassLoaderResolver clr = omfCtx.getClassLoaderResolver(null);@b@    try@b@    {@b@      Class.forName(dbDriver);@b@    }@b@    catch (ClassNotFoundException cnfe)@b@    {@b@      try@b@      {@b@        clr.classForName(dbDriver);@b@      }@b@      catch (RuntimeException e)@b@      {@b@        throw new DatastoreDriverNotFoundException(dbDriver);@b@      }@b@@b@    }@b@@b@    ClassUtils.assertClassForJarExistsInClasspath(clr, "com.mchange.v2.c3p0.ComboPooledDataSource", "c3p0.jar");@b@@b@    ComboPooledDataSource ds = new ComboPooledDataSource();@b@@b@    if (conf.hasProperty("datanucleus.connectionPool.maxStatements"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.maxStatements");@b@      if (size >= 0)@b@      {@b@        ds.setMaxStatements(size);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.maxPoolSize"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.maxPoolSize");@b@      if (size >= 0)@b@      {@b@        ds.setMaxPoolSize(size);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.minPoolSize"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.minPoolSize");@b@      if (size >= 0)@b@      {@b@        ds.setMinPoolSize(size);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.initialPoolSize"))@b@    {@b@      size = conf.getIntProperty("datanucleus.connectionPool.initialPoolSize");@b@      if (size >= 0)@b@      {@b@        ds.setInitialPoolSize(size);@b@      }@b@@b@    }@b@@b@    try@b@    {@b@      ds.setDriverClass(dbDriver);@b@    }@b@    catch (PropertyVetoException pve)@b@    {@b@      throw new DatastorePoolException("C3P0", dbDriver, dbURL, pve);@b@    }@b@@b@    ds.setJdbcUrl(dbURL);@b@    ds.setUser(dbUser);@b@    ds.setPassword(dbPassword);@b@@b@    return ds;@b@  }@b@}

3.DBCPDataSourceFactory

package org.datanucleus.store.rdbms.datasource.dbcp;@b@@b@import javax.sql.DataSource;@b@import org.apache.commons.dbcp.ConnectionFactory;@b@import org.apache.commons.dbcp.DriverManagerConnectionFactory;@b@import org.apache.commons.dbcp.PoolableConnectionFactory;@b@import org.apache.commons.dbcp.PoolingDataSource;@b@import org.apache.commons.pool.KeyedObjectPoolFactory;@b@import org.apache.commons.pool.ObjectPool;@b@import org.apache.commons.pool.impl.GenericObjectPool;@b@import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory;@b@import org.datanucleus.ClassLoaderResolver;@b@import org.datanucleus.OMFContext;@b@import org.datanucleus.PersistenceConfiguration;@b@import org.datanucleus.store.StoreManager;@b@import org.datanucleus.store.rdbms.datasource.DataNucleusDataSourceFactory;@b@import org.datanucleus.store.rdbms.datasource.DatastoreDriverNotFoundException;@b@import org.datanucleus.store.rdbms.datasource.DatastorePoolException;@b@import org.datanucleus.util.ClassUtils;@b@@b@public class DBCPDataSourceFactory@b@  implements DataNucleusDataSourceFactory@b@{@b@  public DataSource makePooledDataSource(OMFContext omfCtx)@b@  {@b@    int value;@b@    PersistenceConfiguration conf = omfCtx.getPersistenceConfiguration();@b@    StoreManager storeMgr = omfCtx.getStoreManager();@b@    String dbDriver = storeMgr.getConnectionDriverName();@b@    String dbURL = storeMgr.getConnectionURL();@b@    String dbUser = storeMgr.getConnectionUserName();@b@    if (dbUser == null)@b@    {@b@      dbUser = "";@b@    }@b@    String dbPassword = storeMgr.getConnectionPassword();@b@    if (dbPassword == null)@b@    {@b@      dbPassword = "";@b@    }@b@    ClassLoaderResolver clr = omfCtx.getClassLoaderResolver(null);@b@    try@b@    {@b@      Class.forName(dbDriver);@b@    }@b@    catch (ClassNotFoundException cnfe)@b@    {@b@      try@b@      {@b@        clr.classForName(dbDriver);@b@      }@b@      catch (RuntimeException e)@b@      {@b@        throw new DatastoreDriverNotFoundException(dbDriver);@b@      }@b@@b@    }@b@@b@    ClassUtils.assertClassForJarExistsInClasspath(clr, "org.apache.commons.pool.ObjectPool", "commons-pool.jar");@b@@b@    ClassUtils.assertClassForJarExistsInClasspath(clr, "org.apache.commons.dbcp.ConnectionFactory", "commons-dbcp.jar");@b@@b@    ObjectPool connectionPool = new GenericObjectPool(null);@b@@b@    if (conf.hasProperty("datanucleus.connectionPool.maxIdle"))@b@    {@b@      value = conf.getIntProperty("datanucleus.connectionPool.maxIdle");@b@      if (value > 0)@b@      {@b@        ((GenericObjectPool)connectionPool).setMaxIdle(value);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.minIdle"))@b@    {@b@      value = conf.getIntProperty("datanucleus.connectionPool.minIdle");@b@      if (value > 0)@b@      {@b@        ((GenericObjectPool)connectionPool).setMinIdle(value);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.maxActive"))@b@    {@b@      value = conf.getIntProperty("datanucleus.connectionPool.maxActive");@b@      if (value > 0)@b@      {@b@        ((GenericObjectPool)connectionPool).setMaxActive(value);@b@      }@b@    }@b@    if (conf.hasProperty("datanucleus.connectionPool.maxWait"))@b@    {@b@      value = conf.getIntProperty("datanucleus.connectionPool.maxWait");@b@      if (value > 0)@b@      {@b@        ((GenericObjectPool)connectionPool).setMaxWait(value);@b@      }@b@    }@b@@b@    if (conf.hasProperty("datanucleus.connectionPool.timeBetweenEvictionRunsMillis"))@b@    {@b@      value = conf.getIntProperty("datanucleus.connectionPool.timeBetweenEvictionRunsMillis");@b@      if (value > 0)@b@      {@b@        ((GenericObjectPool)connectionPool).setTimeBetweenEvictionRunsMillis(value);@b@@b@        int maxIdle = ((GenericObjectPool)connectionPool).getMaxIdle();@b@        int numTestsPerEvictionRun = (int)Math.ceil(maxIdle / 4.0D);@b@        ((GenericObjectPool)connectionPool).setNumTestsPerEvictionRun(numTestsPerEvictionRun);@b@      }@b@    }@b@@b@    if (conf.hasProperty("datanucleus.connectionPool.minEvictableIdleTimeMillis"))@b@    {@b@      value = conf.getIntProperty("datanucleus.connectionPool.minEvictableIdleTimeMillis");@b@      if (value > 0)@b@      {@b@        ((GenericObjectPool)connectionPool).setMinEvictableIdleTimeMillis(value);@b@      }@b@@b@    }@b@@b@    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbURL, dbUser, dbPassword);@b@@b@    KeyedObjectPoolFactory kpf = null;@b@    if (conf.hasProperty("datanucleus.connectionPool.maxStatements"))@b@    {@b@      int value = conf.getIntProperty("datanucleus.connectionPool.maxStatements");@b@      if (value > 0)@b@      {@b@        kpf = new StackKeyedObjectPoolFactory(null, value);@b@      }@b@@b@    }@b@@b@    try@b@    {@b@      String testSQL = null;@b@      if (conf.hasProperty("datanucleus.connectionPool.testSQL"))@b@      {@b@        testSQL = conf.getStringProperty("datanucleus.connectionPool.testSQL");@b@      }@b@      new PoolableConnectionFactory(connectionFactory, connectionPool, kpf, testSQL, false, false);@b@@b@      if (testSQL != null)@b@      {@b@        ((GenericObjectPool)connectionPool).setTestOnBorrow(true);@b@      }@b@    }@b@    catch (Exception e)@b@    {@b@      throw new DatastorePoolException("DBCP", dbDriver, dbURL, e);@b@    }@b@@b@    DataSource ds = new PoolingDataSource(connectionPool);@b@@b@    return ds;@b@  }@b@}

4.ProxoolDataSourceFactory

package org.datanucleus.store.rdbms.datasource.proxool;@b@@b@import java.util.Hashtable;@b@import java.util.Properties;@b@import javax.sql.DataSource;@b@import org.datanucleus.ClassLoaderResolver;@b@import org.datanucleus.OMFContext;@b@import org.datanucleus.PersistenceConfiguration;@b@import org.datanucleus.store.StoreManager;@b@import org.datanucleus.store.rdbms.datasource.DataNucleusDataSourceFactory;@b@import org.datanucleus.store.rdbms.datasource.DatastoreDriverNotFoundException;@b@import org.datanucleus.store.rdbms.datasource.DatastorePoolException;@b@import org.datanucleus.util.ClassUtils;@b@import org.logicalcobwebs.proxool.ProxoolDataSource;@b@import org.logicalcobwebs.proxool.ProxoolException;@b@import org.logicalcobwebs.proxool.ProxoolFacade;@b@@b@public class ProxoolDataSourceFactory@b@  implements DataNucleusDataSourceFactory@b@{@b@  private static int poolNumber = 0;@b@@b@  public DataSource makePooledDataSource(OMFContext omfCtx)@b@  {@b@    PersistenceConfiguration conf = omfCtx.getPersistenceConfiguration();@b@    StoreManager storeMgr = omfCtx.getStoreManager();@b@    String dbDriver = storeMgr.getConnectionDriverName();@b@    String dbURL = storeMgr.getConnectionURL();@b@    String dbUser = storeMgr.getConnectionUserName();@b@    if (dbUser == null)@b@    {@b@      dbUser = "";@b@    }@b@    String dbPassword = storeMgr.getConnectionPassword();@b@    if (dbPassword == null)@b@    {@b@      dbPassword = "";@b@    }@b@    ClassLoaderResolver clr = omfCtx.getClassLoaderResolver(null);@b@    try@b@    {@b@      Class.forName(dbDriver);@b@    }@b@    catch (ClassNotFoundException cnfe)@b@    {@b@      try@b@      {@b@        clr.classForName(dbDriver);@b@      }@b@      catch (RuntimeException e)@b@      {@b@        throw new DatastoreDriverNotFoundException(dbDriver);@b@      }@b@@b@    }@b@@b@    ClassUtils.assertClassForJarExistsInClasspath(clr, "org.apache.commons.logging.Log", "commons-logging.jar");@b@@b@    ClassUtils.assertClassForJarExistsInClasspath(clr, "org.logicalcobwebs.proxool.ProxoolDriver", "proxool.jar");@b@@b@    String alias = "datanucleus" + poolNumber;@b@    try@b@    {@b@      Properties props = new Properties();@b@      if (conf.hasProperty("datanucleus.connectionPool.maxConnections"))@b@      {@b@        int value = conf.getIntProperty("datanucleus.connectionPool.maxConnections");@b@        if (value > 0)@b@        {@b@          props.put("proxool.maximum-connection-count", "" + value);@b@        }@b@        else@b@        {@b@          props.put("proxool.maximum-connection-count", "10");@b@        }@b@      }@b@      else@b@      {@b@        props.put("proxool.maximum-connection-count", "10");@b@      }@b@      if (conf.hasProperty("datanucleus.connectionPool.testSQL"))@b@      {@b@        String value = conf.getStringProperty("datanucleus.connectionPool.testSQL");@b@        props.put("proxool.house-keeping-test-sql", value);@b@      }@b@      else@b@      {@b@        props.put("proxool.house-keeping-test-sql", "SELECT 1");@b@      }@b@@b@      props.setProperty("user", dbUser);@b@      props.setProperty("password", dbPassword);@b@@b@      String url = "proxool." + alias + ":" + dbDriver + ":" + dbURL;@b@      poolNumber += 1;@b@      ProxoolFacade.registerConnectionPool(url, props);@b@    }@b@    catch (ProxoolException pe)@b@    {@b@      pe.printStackTrace();@b@      throw new DatastorePoolException("Proxool", dbDriver, dbURL, pe);@b@    }@b@@b@    DataSource ds = new ProxoolDataSource(alias);@b@@b@    return ds;@b@  }@b@}