title: Atomikos, Java 8 and multiple databases
body: <p>We have an application that has been running fine with Java 7 for a few years, but have problems upgrading to Java 8. The database connections seems to be the problem.</p>
  
  <p>This is the case:
  The application accesses more than 200 databases. Or, technically, 200 different schemas on one database installation. We use Atomikos for the data sources. When starting up the application with Java 7, there is probably some sequential iteration over the data sources, because we have set the max pool size to 5. It takes a little bit of time, but really no issues.</p>
  
  <p>When we try running with Java 8, my guess is that it tries to do everything in parallell. The application crashes on startup like this:</p>
  
  <blockquote>
    <p>05:21:39.611 INFO  [main]
    o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA
    container EntityManagerFactory for persistence unit 'database.np'
    05:21:41.389 INFO  [main]
    o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA
    container EntityManagerFactory for persistence unit
    'database.np_000000000_0000' 05:21:41.796 INFO  [main]
    o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA
    container EntityManagerFactory for persistence unit
    'database.np_000000001_0000' 05:22:11.890 WARN  [main]
    c.atomikos.jdbc.AtomikosSQLException - Connection pool exhausted - try
    increasing 'maxPoolSize' and/or 'borrowConnectionTimeout' on the
    DataSourceBean. 05:22:41.991 WARN  [main]
    c.atomikos.jdbc.AtomikosSQLException - Connection pool exhausted - try
    increasing 'maxPoolSize' and/or 'borrowConnectionTimeout' on the
    DataSourceBean. 05:22:41.998 ERROR [main]
    o.h.tool.hbm2ddl.SchemaValidator - HHH000319: Could not get database
    metadata com.atomikos.jdbc.AtomikosSQLException: Connection pool
    exhausted - try increasing 'maxPoolSize' and/or
    'borrowConnectionTimeout' on the DataSourceBean.
            at com.atomikos.jdbc.AtomikosSQLException.throwAtomikosSQLException(AtomikosSQLException.java:46)
    ~[transactions-jdbc-3.9.3.jar:na]
            at com.atomikos.jdbc.AbstractDataSourceBean.throwAtomikosSQLException(AbstractDataSourceBean.java:90)
    ~[transactions-jdbc-3.9.3.jar:na]
            at com.atomikos.jdbc.AbstractDataSourceBean.throwAtomikosSQLException(AbstractDataSourceBean.java:85)
    ~[transactions-jdbc-3.9.3.jar:na]</p>
    
    <p>So, we try the advice in the stacktrace, and increase maxPoolSize to a
    suitable number above 200. Then we can actually start the application,
    but... It seems like a complete overkill, and to make matters worse,
    the application doesn't like having so many connections it doesn't
    really use - the databases/schemas are accessed only now and then. We
    see the following in the log:</p>
    
    <p>11:08:26.970 [Atomikos:0] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceNp': connection idle for 358671ms
    11:08:26.970 [Atomikos:0] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceNp': connection idle for 357953ms
    11:08:26.970 [Atomikos:0] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceNp': connection idle for 357947ms
    11:08:26.970 [Atomikos:0] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceNp': connection idle for 26425ms
    11:08:26.970 [Atomikos:0] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceNp': current size: 1/4
    11:08:29.983 [Atomikos:1] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceRolp': trying to shrink pool
    11:08:29.984 [Atomikos:1] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceRolp': connection idle for
    359790ms | (repeat 200 times...) | atomikos connection pool
    'dataSourceRolp': connection idle for 263450ms 11:07:29.983
    [Atomikos:1] DEBUG c.a.datasource.pool.ConnectionPool - atomikos
    connection pool 'dataSourceRolp': connection idle for 263446ms
    11:07:29.983 [Atomikos:1] DEBUG c.a.datasource.pool.ConnectionPool -
    atomikos connection pool 'dataSourceRolp': connection idle for
    263168ms 11:07:29.983 [Atomikos:1] DEBUG
    c.a.datasource.pool.ConnectionPool - atomikos connection pool
    'dataSourceRolp': current size: 1/208</p>
  </blockquote>
  
  <p>Any thoughts? For legal reasons, we need to have the 200+ schemas separate. Other than that, we are fairly free to choose technical solutions as we want.</p>