Question

I have a scenario, wherein I need to dynamically switch the data source during application run time.

For this I have JDBCManager class which maintains a map of pooled datasources (C3P0), on request of a particular spring jdbctemplate, I looked at key and return appropriate jdbctemplate with data source. as below

public JdbcTemplate getJdbcTemplate(long targetId) throws DataAccessDeniedException {
    return new JdbcTemplate(this.map.get(targetId));

}

I have not configured any transaction manager in my config, nor I have attached any transaction manager while I pragmatically creating a Datasourec as below

DriverManagerDataSource dataSource = null;
    dataSource = new DriverManagerDataSource();
    // DataSource dataSource = DataSources.unpooledDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(userId);
    dataSource.setPassword(userPassword);

    DataSource pooledDataSource;
    try {
        Map<String, Object> overrides = new HashMap<String, Object>();

        overrides.put("minPoolSize", minPoolSize);
        overrides.put("maxPoolSize", maxPoolSize);
        overrides.put("acquireIncrement", acquireIncrement);
        overrides.put("maxIdleTime", maxIdleTime);
        overrides.put("idleConnectionTestPeriod", idleConnectionTestPeriod);
        overrides.put("maxIdleTimeExcessConnections", maxIdleTimeExcessConnections);
        overrides.put("numHelperThreads", numHelperThreads);
        overrides.put("unreturnedConnectionTimeout", unreturnedConnectionTimeout);
        overrides.put("checkoutTimeout", checkoutTimeout);

        // create the PooledDataSource with overrides
        pooledDataSource = DataSources.pooledDataSource(dataSource, overrides);
    } catch (SQLException e) {
        logger.error("Could not create pooled datasource for target: " + targetId);
        throw new RuntimeException(e);
    }        
    // cache the pooledDataSource
    this.map.put(targetId, pooledDataSource);

    return pooledDataSource;
}

Currently I am facing issue wherein After few minutes of processing my c3p0 connection pool runs out of connections and everything starts failing with following error

Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@65d43d1e -- timeout at awaitAvailable()
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1416)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:756)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:683)
... 61 more

I suspect something is wrong with transactions or there is connection leak.

My doubt is will jdbctemplate release / manage connections appropriately when there is no transaction manager configured?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top