org.apache.tomcat.dbcp.dbcp.BasicDataSource Java Examples

The following examples show how to use org.apache.tomcat.dbcp.dbcp.BasicDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestJdbcWrapper.java    From javamelody with Apache License 2.0 6 votes vote down vote up
/** Test.
 * @throws Exception e */
@Test
public void testRewrapDataSource() throws Exception {
	final BasicDataSource tomcatDataSource = new BasicDataSource();
	tomcatDataSource.setUrl(H2_DATABASE_URL);
	rewrapDataSource(tomcatDataSource);
	final org.apache.commons.dbcp.BasicDataSource dbcpDataSource = new org.apache.commons.dbcp.BasicDataSource();
	dbcpDataSource.setUrl(H2_DATABASE_URL);
	rewrapDataSource(dbcpDataSource);
	final org.apache.tomcat.dbcp.dbcp2.BasicDataSource tomcat2DataSource = new org.apache.tomcat.dbcp.dbcp2.BasicDataSource();
	tomcat2DataSource.setUrl(H2_DATABASE_URL);
	rewrapDataSource(tomcat2DataSource);
	final org.apache.commons.dbcp2.BasicDataSource dbcp2DataSource = new org.apache.commons.dbcp2.BasicDataSource();
	dbcp2DataSource.setUrl(H2_DATABASE_URL);
	rewrapDataSource(dbcp2DataSource);
	final DataSource dataSource = createNiceMock(DataSource.class);
	rewrapDataSource(dataSource);
}
 
Example #2
Source File: JdbcWrapperHelper.java    From javamelody with Apache License 2.0 6 votes vote down vote up
static void pullDataSourceProperties(String name, DataSource dataSource) {
	// CHECKSTYLE:ON
	final String dataSourceClassName = dataSource.getClass().getName();
	if ("org.apache.tomcat.dbcp.dbcp.BasicDataSource".equals(dataSourceClassName)
			&& dataSource instanceof BasicDataSource) {
		pullTomcatDbcpDataSourceProperties(name, dataSource);
	} else if ("org.apache.tomcat.dbcp.dbcp2.BasicDataSource".equals(dataSourceClassName)
			&& dataSource instanceof org.apache.tomcat.dbcp.dbcp2.BasicDataSource) {
		pullTomcatDbcp2DataSourceProperties(name, dataSource);
	} else if ("org.apache.commons.dbcp.BasicDataSource".equals(dataSourceClassName)
			&& dataSource instanceof org.apache.commons.dbcp.BasicDataSource) {
		pullCommonsDbcpDataSourceProperties(name, dataSource);
	} else if ("org.apache.commons.dbcp2.BasicDataSource".equals(dataSourceClassName)
			&& dataSource instanceof org.apache.commons.dbcp2.BasicDataSource) {
		pullCommonsDbcp2DataSourceProperties(name, dataSource);
	} else if ("org.apache.tomcat.jdbc.pool.DataSource".equals(dataSourceClassName)
			&& dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
		pullTomcatJdbcDataSourceProperties(name, dataSource);
	}
}
 
Example #3
Source File: DataSourceFactory.java    From Oceanus with Apache License 2.0 5 votes vote down vote up
@Override
public Object create(DataNodeConfig config) {
	if (config.getId() == null) {
		return null;
	}
	BasicDataSource dataSource = new BasicDataSource();
	dataSource.setMinEvictableIdleTimeMillis(1000 * 60 * 10);
	dataSource.setNumTestsPerEvictionRun(5);
	dataSource.setRemoveAbandoned(true);
	dataSource.setRemoveAbandonedTimeout(10);
	dataSource.setTimeBetweenEvictionRunsMillis(1000 * 60 * 5);
	dataSource.setTestOnBorrow(false);
	dataSource.setTestOnReturn(false);
	dataSource.setTestWhileIdle(true);
	dataSource.setValidationQuery("SELECT 1");
	
	Map<String, String> properties=getProperties(config);
	if(logger.isDebugEnabled()){
		logger.debug("datanode id=["+config.getId()+"] properties="+properties);
	}
	for (Map.Entry<String, String> entry :properties .entrySet()) {
		try {
			BeanUtils.setProperty(dataSource,
					entry.getKey(), entry.getValue());
		} catch (Exception e) {
			logger.error("create DataSource error!", e);
		}

	}
	
	return dataSource;
}
 
Example #4
Source File: Cause3PersistenceConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #5
Source File: Cause2PersistenceConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #6
Source File: Cause1PersistenceConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #7
Source File: Cause1NonTransientConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #8
Source File: Cause4NonTransientConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #9
Source File: Cause5NonTransientConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #10
Source File: PersistenceConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource restDataSource() {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}
 
Example #11
Source File: DataSourceWrapper.java    From Oceanus with Apache License 2.0 5 votes vote down vote up
public DataSourceWrapper(Properties properties){
	BasicDataSource dbcpDataSource = new BasicDataSource();
	for (Map.Entry<Object, Object> entry : properties .entrySet()) {
		try {
			BeanUtils.setProperty(dbcpDataSource,
					entry.getKey().toString(), entry.getValue().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	this.datasource = dbcpDataSource;
}
 
Example #12
Source File: JdbcWrapperHelper.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static void pullCommonsDbcp2DataSourceProperties(String name, DataSource dataSource) {
	// si dbcp et si dataSource standard, alors on récupère des infos
	final org.apache.commons.dbcp2.BasicDataSource dbcp2DataSource = (org.apache.commons.dbcp2.BasicDataSource) dataSource;
	final BasicDataSourcesProperties properties = DBCP_BASIC_DATASOURCES_PROPERTIES;
	// basicDataSource.getNumActive() est en théorie égale à USED_CONNECTION_COUNT à un instant t,
	// numIdle + numActive est le nombre de connexions ouvertes dans la bdd pour ce serveur à un instant t

	// les propriétés généralement importantes en premier (se méfier aussi de testOnBorrow)
	properties.put(name, MAX_ACTIVE_PROPERTY_NAME, dbcp2DataSource.getMaxTotal());
	properties.put(name, "poolPreparedStatements", dbcp2DataSource.isPoolPreparedStatements());

	properties.put(name, "defaultCatalog", dbcp2DataSource.getDefaultCatalog());
	properties.put(name, "defaultAutoCommit", dbcp2DataSource.getDefaultAutoCommit());
	properties.put(name, "defaultReadOnly", dbcp2DataSource.getDefaultReadOnly());
	properties.put(name, "defaultTransactionIsolation",
			dbcp2DataSource.getDefaultTransactionIsolation());
	properties.put(name, "driverClassName", dbcp2DataSource.getDriverClassName());
	properties.put(name, "initialSize", dbcp2DataSource.getInitialSize());
	properties.put(name, "maxIdle", dbcp2DataSource.getMaxIdle());
	properties.put(name, "maxOpenPreparedStatements",
			dbcp2DataSource.getMaxOpenPreparedStatements());
	properties.put(name, "maxWait", dbcp2DataSource.getMaxWaitMillis());
	properties.put(name, "minEvictableIdleTimeMillis",
			dbcp2DataSource.getMinEvictableIdleTimeMillis());
	properties.put(name, "minIdle", dbcp2DataSource.getMinIdle());
	properties.put(name, "numTestsPerEvictionRun", dbcp2DataSource.getNumTestsPerEvictionRun());
	properties.put(name, "testOnBorrow", dbcp2DataSource.getTestOnBorrow());
	properties.put(name, "testOnReturn", dbcp2DataSource.getTestOnReturn());
	properties.put(name, "testWhileIdle", dbcp2DataSource.getTestWhileIdle());
	properties.put(name, "timeBetweenEvictionRunsMillis",
			dbcp2DataSource.getTimeBetweenEvictionRunsMillis());
	properties.put(name, "validationQuery", dbcp2DataSource.getValidationQuery());
}
 
Example #13
Source File: JdbcWrapperHelper.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static void pullTomcatDbcp2DataSourceProperties(String name, DataSource dataSource) {
	// si tomcat et si dataSource standard, alors on récupère des infos
	final org.apache.tomcat.dbcp.dbcp2.BasicDataSource tomcatDbcp2DataSource = (org.apache.tomcat.dbcp.dbcp2.BasicDataSource) dataSource;
	final BasicDataSourcesProperties properties = TOMCAT_BASIC_DATASOURCES_PROPERTIES;
	// basicDataSource.getNumActive() est en théorie égale à USED_CONNECTION_COUNT à un instant t,
	// numIdle + numActive est le nombre de connexions ouvertes dans la bdd pour ce serveur à un instant t

	// les propriétés généralement importantes en premier (se méfier aussi de testOnBorrow)
	properties.put(name, MAX_ACTIVE_PROPERTY_NAME, tomcatDbcp2DataSource.getMaxTotal());
	properties.put(name, "poolPreparedStatements",
			tomcatDbcp2DataSource.isPoolPreparedStatements());

	properties.put(name, "defaultCatalog", tomcatDbcp2DataSource.getDefaultCatalog());
	properties.put(name, "defaultAutoCommit", tomcatDbcp2DataSource.getDefaultAutoCommit());
	properties.put(name, "defaultReadOnly", tomcatDbcp2DataSource.getDefaultReadOnly());
	properties.put(name, "defaultTransactionIsolation",
			tomcatDbcp2DataSource.getDefaultTransactionIsolation());
	properties.put(name, "driverClassName", tomcatDbcp2DataSource.getDriverClassName());
	properties.put(name, "initialSize", tomcatDbcp2DataSource.getInitialSize());
	properties.put(name, "maxIdle", tomcatDbcp2DataSource.getMaxIdle());
	properties.put(name, "maxOpenPreparedStatements",
			tomcatDbcp2DataSource.getMaxOpenPreparedStatements());
	properties.put(name, "maxWait", tomcatDbcp2DataSource.getMaxWaitMillis());
	properties.put(name, "minEvictableIdleTimeMillis",
			tomcatDbcp2DataSource.getMinEvictableIdleTimeMillis());
	properties.put(name, "minIdle", tomcatDbcp2DataSource.getMinIdle());
	properties.put(name, "numTestsPerEvictionRun",
			tomcatDbcp2DataSource.getNumTestsPerEvictionRun());
	properties.put(name, "testOnBorrow", tomcatDbcp2DataSource.getTestOnBorrow());
	properties.put(name, "testOnReturn", tomcatDbcp2DataSource.getTestOnReturn());
	properties.put(name, "testWhileIdle", tomcatDbcp2DataSource.getTestWhileIdle());
	properties.put(name, "timeBetweenEvictionRunsMillis",
			tomcatDbcp2DataSource.getTimeBetweenEvictionRunsMillis());
	properties.put(name, "validationQuery", tomcatDbcp2DataSource.getValidationQuery());
}
 
Example #14
Source File: JdbcWrapperHelper.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static void pullCommonsDbcpDataSourceProperties(String name, DataSource dataSource) {
	// si dbcp et si dataSource standard, alors on récupère des infos
	final org.apache.commons.dbcp.BasicDataSource dbcpDataSource = (org.apache.commons.dbcp.BasicDataSource) dataSource;
	final BasicDataSourcesProperties properties = DBCP_BASIC_DATASOURCES_PROPERTIES;
	// basicDataSource.getNumActive() est en théorie égale à USED_CONNECTION_COUNT à un instant t,
	// numIdle + numActive est le nombre de connexions ouvertes dans la bdd pour ce serveur à un instant t

	// les propriétés généralement importantes en premier (se méfier aussi de testOnBorrow)
	properties.put(name, MAX_ACTIVE_PROPERTY_NAME, dbcpDataSource.getMaxActive());
	properties.put(name, "poolPreparedStatements", dbcpDataSource.isPoolPreparedStatements());

	properties.put(name, "defaultCatalog", dbcpDataSource.getDefaultCatalog());
	properties.put(name, "defaultAutoCommit", dbcpDataSource.getDefaultAutoCommit());
	properties.put(name, "defaultReadOnly", dbcpDataSource.getDefaultReadOnly());
	properties.put(name, "defaultTransactionIsolation",
			dbcpDataSource.getDefaultTransactionIsolation());
	properties.put(name, "driverClassName", dbcpDataSource.getDriverClassName());
	properties.put(name, "initialSize", dbcpDataSource.getInitialSize());
	properties.put(name, "maxIdle", dbcpDataSource.getMaxIdle());
	properties.put(name, "maxOpenPreparedStatements",
			dbcpDataSource.getMaxOpenPreparedStatements());
	properties.put(name, "maxWait", dbcpDataSource.getMaxWait());
	properties.put(name, "minEvictableIdleTimeMillis",
			dbcpDataSource.getMinEvictableIdleTimeMillis());
	properties.put(name, "minIdle", dbcpDataSource.getMinIdle());
	properties.put(name, "numTestsPerEvictionRun", dbcpDataSource.getNumTestsPerEvictionRun());
	properties.put(name, "testOnBorrow", dbcpDataSource.getTestOnBorrow());
	properties.put(name, "testOnReturn", dbcpDataSource.getTestOnReturn());
	properties.put(name, "testWhileIdle", dbcpDataSource.getTestWhileIdle());
	properties.put(name, "timeBetweenEvictionRunsMillis",
			dbcpDataSource.getTimeBetweenEvictionRunsMillis());
	properties.put(name, "validationQuery", dbcpDataSource.getValidationQuery());
}
 
Example #15
Source File: JdbcWrapperHelper.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static void pullTomcatDbcpDataSourceProperties(String name, DataSource dataSource) {
	// si tomcat et si dataSource standard, alors on récupère des infos
	final BasicDataSource tomcatDbcpDataSource = (BasicDataSource) dataSource;
	final BasicDataSourcesProperties properties = TOMCAT_BASIC_DATASOURCES_PROPERTIES;
	// basicDataSource.getNumActive() est en théorie égale à USED_CONNECTION_COUNT à un instant t,
	// numIdle + numActive est le nombre de connexions ouvertes dans la bdd pour ce serveur à un instant t

	// les propriétés généralement importantes en premier (se méfier aussi de testOnBorrow)
	properties.put(name, MAX_ACTIVE_PROPERTY_NAME, tomcatDbcpDataSource.getMaxActive());
	properties.put(name, "poolPreparedStatements",
			tomcatDbcpDataSource.isPoolPreparedStatements());

	properties.put(name, "defaultCatalog", tomcatDbcpDataSource.getDefaultCatalog());
	properties.put(name, "defaultAutoCommit", tomcatDbcpDataSource.getDefaultAutoCommit());
	properties.put(name, "defaultReadOnly", tomcatDbcpDataSource.getDefaultReadOnly());
	properties.put(name, "defaultTransactionIsolation",
			tomcatDbcpDataSource.getDefaultTransactionIsolation());
	properties.put(name, "driverClassName", tomcatDbcpDataSource.getDriverClassName());
	properties.put(name, "initialSize", tomcatDbcpDataSource.getInitialSize());
	properties.put(name, "maxIdle", tomcatDbcpDataSource.getMaxIdle());
	properties.put(name, "maxOpenPreparedStatements",
			tomcatDbcpDataSource.getMaxOpenPreparedStatements());
	properties.put(name, "maxWait", tomcatDbcpDataSource.getMaxWait());
	properties.put(name, "minEvictableIdleTimeMillis",
			tomcatDbcpDataSource.getMinEvictableIdleTimeMillis());
	properties.put(name, "minIdle", tomcatDbcpDataSource.getMinIdle());
	properties.put(name, "numTestsPerEvictionRun",
			tomcatDbcpDataSource.getNumTestsPerEvictionRun());
	properties.put(name, "testOnBorrow", tomcatDbcpDataSource.getTestOnBorrow());
	properties.put(name, "testOnReturn", tomcatDbcpDataSource.getTestOnReturn());
	properties.put(name, "testWhileIdle", tomcatDbcpDataSource.getTestWhileIdle());
	properties.put(name, "timeBetweenEvictionRunsMillis",
			tomcatDbcpDataSource.getTimeBetweenEvictionRunsMillis());
	properties.put(name, "validationQuery", tomcatDbcpDataSource.getValidationQuery());
}
 
Example #16
Source File: TestJdbcWrapper.java    From javamelody with Apache License 2.0 4 votes vote down vote up
/** Test.
 * @throws Exception e */
@Test
public void testCreateDataSourceProxy() throws Exception {
	// on fait le ménage au cas où TestMonitoringSpringInterceptor ait été exécuté juste avant
	cleanUp();

	assertTrue("getBasicDataSourceProperties0",
			JdbcWrapper.getBasicDataSourceProperties().isEmpty());
	assertEquals("getMaxConnectionCount0", -1, JdbcWrapper.getMaxConnectionCount());

	final org.apache.tomcat.jdbc.pool.DataSource tomcatJdbcDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
	tomcatJdbcDataSource.setUrl(H2_DATABASE_URL);
	tomcatJdbcDataSource.setDriverClassName("org.h2.Driver");
	tomcatJdbcDataSource.setMaxActive(123);
	final DataSource tomcatJdbcProxy = jdbcWrapper.createDataSourceProxy("test2",
			tomcatJdbcDataSource);
	assertNotNull("createDataSourceProxy1", tomcatJdbcProxy);
	tomcatJdbcProxy.getConnection().close();
	assertFalse("getBasicDataSourceProperties1",
			JdbcWrapper.getBasicDataSourceProperties().isEmpty());
	assertEquals("getMaxConnectionCount1", 123, JdbcWrapper.getMaxConnectionCount());

	final org.apache.commons.dbcp.BasicDataSource dbcpDataSource = new org.apache.commons.dbcp.BasicDataSource();
	dbcpDataSource.setUrl(H2_DATABASE_URL);
	dbcpDataSource.setMaxActive(456);
	final DataSource dbcpProxy = jdbcWrapper.createDataSourceProxy(dbcpDataSource);
	assertNotNull("createDataSourceProxy2", dbcpProxy);
	assertFalse("getBasicDataSourceProperties2",
			JdbcWrapper.getBasicDataSourceProperties().isEmpty());
	assertEquals("getMaxConnectionCount2", 456, JdbcWrapper.getMaxConnectionCount());

	final BasicDataSource tomcatDataSource = new BasicDataSource();
	tomcatDataSource.setUrl(H2_DATABASE_URL);
	tomcatDataSource.setMaxActive(789);
	final DataSource tomcatProxy = jdbcWrapper.createDataSourceProxy("test", tomcatDataSource);
	assertNotNull("createDataSourceProxy3", tomcatProxy);
	assertNotNull("getLogWriter2", tomcatProxy.getLogWriter());
	tomcatProxy.getConnection().close();
	assertFalse("getBasicDataSourceProperties3",
			JdbcWrapper.getBasicDataSourceProperties().isEmpty());
	assertEquals("getMaxConnectionCount3", 789, JdbcWrapper.getMaxConnectionCount());

	final org.apache.commons.dbcp2.BasicDataSource dbcp2DataSource = new org.apache.commons.dbcp2.BasicDataSource();
	dbcp2DataSource.setUrl(H2_DATABASE_URL);
	dbcp2DataSource.setMaxTotal(456);
	final DataSource dbcp2Proxy = jdbcWrapper.createDataSourceProxy(dbcp2DataSource);
	assertNotNull("createDataSourceProxy2b", dbcp2Proxy);

	final org.apache.tomcat.dbcp.dbcp2.BasicDataSource tomcat2DataSource = new org.apache.tomcat.dbcp.dbcp2.BasicDataSource();
	tomcat2DataSource.setUrl(H2_DATABASE_URL);
	tomcat2DataSource.setMaxTotal(789);
	final DataSource tomcat2Proxy = jdbcWrapper.createDataSourceProxy("test",
			tomcat2DataSource);
	assertNotNull("createDataSourceProxy3b", tomcat2Proxy);

	final DataSource dataSource2 = new MyDataSource(tomcatDataSource);
	jdbcWrapper.createDataSourceProxy(dataSource2);
}
 
Example #17
Source File: TestJdbcWrapper.java    From javamelody with Apache License 2.0 4 votes vote down vote up
MyDataSource(BasicDataSource tomcatDataSource) {
	this.tomcatDataSource = tomcatDataSource;
}