2

I use eclipselink as an implementation of JPA and I would like to know if it is possible to check if my EntityManagerFactory is "alive"

for example, if the connection parameters are invalid, when I create the EMF I don't have any exception until I begin a transaction

Thanks

Julien
  • 401
  • 1
  • 4
  • 9

3 Answers3

2

With the entityManagerFactory you can only get a EntityManager and send a check query to the DB, but in this case I think the best solution is to configure your connection pool with a validator.

In this way each connection created or retrieved from the pool is validated via JDBC API or with a simple query. For example, in JBoss inside the data-source configuration you can add the following lines:

<!-- sql to call when connection is created    -->
<new-connection-sql>some arbitrary sql</new-connection-sql>

<!-- sql to call on an existing pooled connection when it is obtained from pool     -->
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>

or (for Postgres, but there are example for each DBMS)

<validation>
  <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"></valid-connection-checker>
  <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"></exception-sorter>
</validation>
Teg
  • 1,302
  • 13
  • 32
2

EclipseLink lazy deploys by default to improve server startup time.

You can configure deploy to occur on server start using,

"eclipselink.deploy-on-startup"="true"

In your persistence.xml.

James
  • 17,965
  • 11
  • 91
  • 146
0

for the moment I use this piece of code:

try {
  emf = Persistence.createEntityManagerFactory(pu, properties);
} catch (Exception e) {
  throw new DaoException("cannot open entity manager factory", e);
}

EntityManager em = null;
try {
  em = emf.createEntityManager();
} catch (Exception e) {
  throw new DaoException("cannot open entity manager", e);
} finally {
  if (null!=em && em.isOpen()) {
    em.close();
  }
}

I will try your suggestions thanks

Julien
  • 401
  • 1
  • 4
  • 9