@Test public void testBasicHappyPath() throws Exception { ObjectPool<PoolableConnection> pool = ConnectionPoolFactory.createConnectionPool(dataSource); Assert.assertTrue(pool != null); Connection conn = pool.borrowObject(); Assert.assertTrue(conn != null); Assert.assertTrue(!conn.isClosed() ); }
public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) { Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed."); Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed"); Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed"); Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed"); try { dataSourceType.getDriver().newInstance(); } catch (Exception e) { throw new MonetaException("Data source JDBC driver can't be instantiated", e) .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName()); } Properties connectionProps = new Properties(); connectionProps.putAll(dataSourceType.getJdbcConnectionProperties()); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( dataSourceType.getConnectionUrl(),connectionProps); ObjectName poolName= null; try { poolName=new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName()); } catch (MalformedObjectNameException e) { throw new MonetaException("Data source name not valid", e) .addContextValue("Data source name", dataSourceType.getDataSourceName()); } PoolableConnectionFactory poolableConnectionFactory= new PoolableConnectionFactory(connectionFactory,poolName); Set<String> unusedConnectionPoolPropSet = new HashSet<String>(); Map<String, String> propMap = new HashMap<String,String>(dataSourceType.getConnectionPoolProperties()); PropertyDescriptor pDesc = null; for (String propName: propMap.keySet()) { pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName, propMap.get(propName)); if (pDesc == null) { unusedConnectionPoolPropSet.add(propName); } } GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory); poolableConnectionFactory.setPool(pool); for (String propName: new HashSet<String>(unusedConnectionPoolPropSet)) { pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName)); if (pDesc != null) { unusedConnectionPoolPropSet.remove(propName); } } if (unusedConnectionPoolPropSet.size() > 0) { MonetaException me = new MonetaException("Invalid connection pool properties detected"); for (String propName: unusedConnectionPoolPropSet) { me.addContextValue("pool property name", propName); } throw me; } return pool; }
ConnectionPoolFactory { public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) { Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed."); Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed"); Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed"); Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed"); try { dataSourceType.getDriver().newInstance(); } catch (Exception e) { throw new MonetaException("Data source JDBC driver can't be instantiated", e) .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName()); } Properties connectionProps = new Properties(); connectionProps.putAll(dataSourceType.getJdbcConnectionProperties()); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( dataSourceType.getConnectionUrl(),connectionProps); ObjectName poolName= null; try { poolName=new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName()); } catch (MalformedObjectNameException e) { throw new MonetaException("Data source name not valid", e) .addContextValue("Data source name", dataSourceType.getDataSourceName()); } PoolableConnectionFactory poolableConnectionFactory= new PoolableConnectionFactory(connectionFactory,poolName); Set<String> unusedConnectionPoolPropSet = new HashSet<String>(); Map<String, String> propMap = new HashMap<String,String>(dataSourceType.getConnectionPoolProperties()); PropertyDescriptor pDesc = null; for (String propName: propMap.keySet()) { pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName, propMap.get(propName)); if (pDesc == null) { unusedConnectionPoolPropSet.add(propName); } } GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory); poolableConnectionFactory.setPool(pool); for (String propName: new HashSet<String>(unusedConnectionPoolPropSet)) { pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName)); if (pDesc != null) { unusedConnectionPoolPropSet.remove(propName); } } if (unusedConnectionPoolPropSet.size() > 0) { MonetaException me = new MonetaException("Invalid connection pool properties detected"); for (String propName: unusedConnectionPoolPropSet) { me.addContextValue("pool property name", propName); } throw me; } return pool; } }
ConnectionPoolFactory { public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) { Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed."); Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed"); Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed"); Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed"); try { dataSourceType.getDriver().newInstance(); } catch (Exception e) { throw new MonetaException("Data source JDBC driver can't be instantiated", e) .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName()); } Properties connectionProps = new Properties(); connectionProps.putAll(dataSourceType.getJdbcConnectionProperties()); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( dataSourceType.getConnectionUrl(),connectionProps); ObjectName poolName= null; try { poolName=new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName()); } catch (MalformedObjectNameException e) { throw new MonetaException("Data source name not valid", e) .addContextValue("Data source name", dataSourceType.getDataSourceName()); } PoolableConnectionFactory poolableConnectionFactory= new PoolableConnectionFactory(connectionFactory,poolName); Set<String> unusedConnectionPoolPropSet = new HashSet<String>(); Map<String, String> propMap = new HashMap<String,String>(dataSourceType.getConnectionPoolProperties()); PropertyDescriptor pDesc = null; for (String propName: propMap.keySet()) { pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName, propMap.get(propName)); if (pDesc == null) { unusedConnectionPoolPropSet.add(propName); } } GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory); poolableConnectionFactory.setPool(pool); for (String propName: new HashSet<String>(unusedConnectionPoolPropSet)) { pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName)); if (pDesc != null) { unusedConnectionPoolPropSet.remove(propName); } } if (unusedConnectionPoolPropSet.size() > 0) { MonetaException me = new MonetaException("Invalid connection pool properties detected"); for (String propName: unusedConnectionPoolPropSet) { me.addContextValue("pool property name", propName); } throw me; } return pool; }  }
ConnectionPoolFactory { public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) { Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed."); Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed"); Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed"); Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed"); try { dataSourceType.getDriver().newInstance(); } catch (Exception e) { throw new MonetaException("Data source JDBC driver can't be instantiated", e) .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName()); } Properties connectionProps = new Properties(); connectionProps.putAll(dataSourceType.getJdbcConnectionProperties()); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( dataSourceType.getConnectionUrl(),connectionProps); ObjectName poolName= null; try { poolName=new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName()); } catch (MalformedObjectNameException e) { throw new MonetaException("Data source name not valid", e) .addContextValue("Data source name", dataSourceType.getDataSourceName()); } PoolableConnectionFactory poolableConnectionFactory= new PoolableConnectionFactory(connectionFactory,poolName); Set<String> unusedConnectionPoolPropSet = new HashSet<String>(); Map<String, String> propMap = new HashMap<String,String>(dataSourceType.getConnectionPoolProperties()); PropertyDescriptor pDesc = null; for (String propName: propMap.keySet()) { pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName, propMap.get(propName)); if (pDesc == null) { unusedConnectionPoolPropSet.add(propName); } } GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory); poolableConnectionFactory.setPool(pool); for (String propName: new HashSet<String>(unusedConnectionPoolPropSet)) { pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName)); if (pDesc != null) { unusedConnectionPoolPropSet.remove(propName); } } if (unusedConnectionPoolPropSet.size() > 0) { MonetaException me = new MonetaException("Invalid connection pool properties detected"); for (String propName: unusedConnectionPoolPropSet) { me.addContextValue("pool property name", propName); } throw me; } return pool; }  static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType); }
ConnectionPoolFactory { public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) { Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed."); Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed"); Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed"); Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed"); try { dataSourceType.getDriver().newInstance(); } catch (Exception e) { throw new MonetaException("Data source JDBC driver can't be instantiated", e) .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName()); } Properties connectionProps = new Properties(); connectionProps.putAll(dataSourceType.getJdbcConnectionProperties()); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( dataSourceType.getConnectionUrl(),connectionProps); ObjectName poolName= null; try { poolName=new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName()); } catch (MalformedObjectNameException e) { throw new MonetaException("Data source name not valid", e) .addContextValue("Data source name", dataSourceType.getDataSourceName()); } PoolableConnectionFactory poolableConnectionFactory= new PoolableConnectionFactory(connectionFactory,poolName); Set<String> unusedConnectionPoolPropSet = new HashSet<String>(); Map<String, String> propMap = new HashMap<String,String>(dataSourceType.getConnectionPoolProperties()); PropertyDescriptor pDesc = null; for (String propName: propMap.keySet()) { pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName, propMap.get(propName)); if (pDesc == null) { unusedConnectionPoolPropSet.add(propName); } } GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory); poolableConnectionFactory.setPool(pool); for (String propName: new HashSet<String>(unusedConnectionPoolPropSet)) { pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName)); if (pDesc != null) { unusedConnectionPoolPropSet.remove(propName); } } if (unusedConnectionPoolPropSet.size() > 0) { MonetaException me = new MonetaException("Invalid connection pool properties detected"); for (String propName: unusedConnectionPoolPropSet) { me.addContextValue("pool property name", propName); } throw me; } return pool; }  static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType);  }