0

I'm trying to understand the purpose and the diference between JdbcDataSource and JdbcConnectionPool.

Based in code either JdbcDataSource or JdbcConnectionPool implement javax.sql.DataSource.

My mains question is: When should I use one or other?

I have created an JNDI entry in jetty like:

<New id="h2ds" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg><Ref refid="itracker" /></Arg>
  <Arg>jdbc/itracker_ds</Arg>
  <Arg>
    <New class="org.h2.jdbcx.JdbcDataSource">
      <Set name="url">db-url</Set>
      <Set name="password">user-password</Set>
      <Set name="user">user-password</Set>
    </New>
  </Arg>
</New>

Can I assume that org.h2.jdbcx.JdbcDataSource works like a pool or should I use some pool like DBCP or C3P0?

josivan
  • 1,963
  • 1
  • 15
  • 26

2 Answers2

2

You should never just assume a javax.sql.DataSource implementation is a connection pool, especially not if it does not contain any properties related to connection pool configuration.

As far as I can tell from its documentation, org.h2.jdbcx.JdbcDataSource is a simple javax.sql.DataSource (and javax.sql.ConnectionPoolDataSource) and does not provide connection pooling, while org.h2.jdbcx.JdbcConnectionPool is a javax.sql.DataSource and provides very basic connection pool.

However in most cases you are probably better off using DBCP, HikariCP or C3p0, or the built-in connection pool if you are using a full application server (which Jetty is not).

Note that implementations of javax.sql.ConnectionPoolDataSource are not a connection pool either, they are intended as a data source for supplying poolable connections to a DataSource providing connection pooling (eg from an application server).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

I have updated my configuration to use HikariCP. Take a look:

<New id="h2ds" class="org.eclipse.jetty.plus.jndi.Resource">
  <Arg><Ref refid="itracker" /></Arg>
  <Arg>jdbc/itracker_ds</Arg>
  <Arg>
    <New class="com.zaxxer.hikari.HikariDataSource">
      <Arg>
        <New class="com.zaxxer.hikari.HikariConfig">
          <Set name="maximumPoolSize">20</Set>
          <Set name="dataSourceClassName">org.h2.jdbcx.JdbcDataSource</Set>
          <Set name="password">db-password</Set>
          <Set name="username">db-user</Set>
          <Call name="addDataSourceProperty">
            <Arg>url</Arg>
            <Arg>db-url</Arg>
          </Call>
        </New>
      </Arg>
    </New>
  </Arg>
</New>

Now I'm really using a connection pool.

josivan
  • 1,963
  • 1
  • 15
  • 26