You can get the details from the below answer,
https://stackoverflow.com/a/51838455/1529092
So basically you have to do something like this,
@Bean
public ComboPooledDataSource dataSource(){
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass(env.getProperty("db.driver"));
dataSource.setJdbcUrl(env.getProperty("db.url"));
dataSource.setUser(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
dataSource.setMinPoolSize(Integer.parseInt(env.getProperty("minPoolSize")));
dataSource.setMaxPoolSize(Integer.parseInt(env.getProperty("maxPoolSize")));
dataSource.setMaxIdleTime(Integer.parseInt(env.getProperty("maxIdleTime")));
dataSource.setMaxStatements(Integer.parseInt(env.getProperty("maxStatements")));
dataSource.setMaxStatementsPerConnection(Integer.parseInt(env.getProperty("maxStatementsPerConnection")));
dataSource.setMaxIdleTimeExcessConnections(10000);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return dataSource;
}
Edit:
As per documentation, the timeout properties are different in each framework, so in this case, the time out is handled by,
- maxConnectionAge
- maxIdleTime
- maxIdleTimeExcessConnections
Managing Pool Size and Connection Age Go To Top
Different applications have different needs with regard to trade-offs
between performance, footprint, and reliability. C3P0 offers a wide
variety of options for controlling how quickly pools that have grown
large under load revert to minPoolSize, and whether "old" Connections
in the pool should be proactively replaced to maintain their
reliablity.
- maxConnectionAge
- maxIdleTime
- maxIdleTimeExcessConnections
By default, pools will never expire Connections. If you wish Connections to be expired over time in order
to maintain "freshness", set maxIdleTime and/or maxConnectionAge.
maxIdleTime defines how many seconds a Connection should be permitted
to go unused before being culled from the pool. maxConnectionAge
forces the pool to cull any Connections that were acquired from the
database more than the set number of seconds in the past.
maxIdleTimeExcessConnections is about minimizing the number of
Connections held by c3p0 pools when the pool is not under load. By
default, c3p0 pools grow under load, but only shrink if Connections
fail a Connection test or are expired away via the parameters
described above. Some users want their pools to quickly release
unnecessary Connections after a spike in usage that forces a large
pool size. You can achieve this by setting
maxIdleTimeExcessConnections to a value much shorter than maxIdleTime,
forcing Connections beyond your set minimum size to be released if
they sit idle for more than a short period of time.
Some general advice about all of these timeout parameters: Slow down!
The point of Connection pooling is to bear the cost of acquiring a
Connection only once, and then to reuse the Connection many, many
times. Most databases support Connections that remain open for hours
at a time. There's no need to churn through all your Connections every
few seconds or minutes. Setting maxConnectionAge or maxIdleTime to
1800 (30 minutes) is quite aggressive. For most databases, several
hours may be more appropriate. You can ensure the reliability of your
Connections by testing them, rather than by tossing them. (see
Configuring Connection Testing.) The only one of these parameters that
should generally be set to a few minutes or less is
maxIdleTimeExcessConnections.