Java Code Examples for org.apache.commons.dbcp.BasicDataSource#isClosed()

The following examples show how to use org.apache.commons.dbcp.BasicDataSource#isClosed() . 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: DynamicDBUtil.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 通过 dbKey ,获取数据源
 *
 * @param dbKey
 * @return
 */
public static BasicDataSource getDbSourceByDbKey(final String dbKey) {
    //获取多数据源配置
    DynamicDataSourceModel dbSource = DataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
    //先判断缓存中是否存在数据库链接
    BasicDataSource cacheDbSource = DataSourceCachePool.getCacheBasicDataSource(dbKey);
    if (cacheDbSource != null && !cacheDbSource.isClosed()) {
        log.debug("--------getDbSourceBydbKey------------------从缓存中获取DB连接-------------------");
        return cacheDbSource;
    } else {
        BasicDataSource dataSource = getJdbcDataSource(dbSource);
        DataSourceCachePool.putCacheBasicDataSource(dbKey, dataSource);
        log.info("--------getDbSourceBydbKey------------------创建DB数据库连接-------------------");
        return dataSource;
    }
}
 
Example 2
Source File: DynamicDBUtil.java    From jeecg with Apache License 2.0 6 votes vote down vote up
/**
 * 通过dbkey,获取数据源
 * @param dbKey
 * @return
 */
public static BasicDataSource getDbSourceBydbKey(final String dbKey) {
	//获取多数据源配置
	DynamicDataSourceEntity dynamicSourceEntity = ResourceUtil.getCacheDynamicDataSourceEntity(dbKey);
	//先判断缓存中是否存在数据库链接
	BasicDataSource cacheDbSource = ResourceUtil.getCacheBasicDataSource(dbKey);
	if(cacheDbSource!=null && !cacheDbSource.isClosed()){
		logger.debug("--------getDbSourceBydbKey------------------从缓存中获取DB连接-------------------");
		return cacheDbSource;
	}else{
		BasicDataSource dataSource = getJdbcDataSource(dynamicSourceEntity);
		ResourceUtil.putCacheBasicDataSource(dbKey, dataSource);
		logger.info("--------getDbSourceBydbKey------------------创建DB数据库连接-------------------");
		return dataSource;
	}
}
 
Example 3
Source File: DynamicDBUtil.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭数据库连接池
 *
 * @param dbKey
 * @return
 */
public static void closeDbKey(final String dbKey) {
    BasicDataSource dataSource = getDbSourceByDbKey(dbKey);
    try {
        if (dataSource != null && !dataSource.isClosed()) {
            dataSource.getConnection().commit();
            dataSource.getConnection().close();
            dataSource.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: MockInitialContextFactory.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the datasource given JNDI name and files.
 *
 * @param datasourceName
 * @param clazz
 * @param files
 */
public static BasicDataSource initializeDatasource(String datasourceName, Class clazz, String[] files) throws
        TestCreationException {

    Map<String, Object> jndiObjectsMap = jndiContextData.get();
    if (jndiObjectsMap != null) {
        BasicDataSource basicDataSource = (BasicDataSource) jndiObjectsMap.get(datasourceName);
        if (basicDataSource != null && !basicDataSource.isClosed()) {
            return basicDataSource;
        }
    }
    BasicDataSource dataSource = createDb(datasourceName, clazz, files);
    addContextLookup(datasourceName, dataSource);
    return dataSource;
}
 
Example 5
Source File: DynamicDBUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 关闭数据库连接池
 *  @param dbKey
 * @return 
 * @return
 */
public static void closeDBkey(final String dbKey){
	BasicDataSource dataSource = getDbSourceBydbKey(dbKey);
	try {
		if(dataSource!=null && !dataSource.isClosed()){
			dataSource.getConnection().commit();
			dataSource.getConnection().close();
			dataSource.close();
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
}