16

using EclipseLink as JPA 2.0 provider, I can obtain a JDBC connection by simply calling

Connection con = entityManager.unwrap(Connection.class);

But I'm unsure what I'm responsible for. Do I have to close the connection after submitting my queries? Or are I'm not allowed to close the connection, because EclipseLink also uses this connection internally. Or does it not care, because EclipseLink observes my behaviour and closes the connection automatically if I don't do it?

3
  • Why do you need a connection? JPA uses EntityManager in most of the situations.
    – perissf
    Commented Jul 30, 2012 at 5:25
  • 1
    To call stored procedures. Calling them via EclipseLink is not supported by my DBMS. Commented Jul 30, 2012 at 5:57
  • What DBMS are you on that EclipseLink cannot handle the stored procedures for you?
    – Chris
    Commented Jul 30, 2012 at 15:17

2 Answers 2

10

If you are in the context of a JPA transaction the connection will be managed by the provider (EclipseLink). If you are outside of a transaction you are responsible for managing the connection yourself.

See the following link for additional information:

http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_Connection_from_an_EntityManager

3
  • I have a problem. Transaction always commits changes, even if I will do a rollback on the entity manager. I'm using Informix db and ifxjdbc driver. Transaction is application managed. And second problem is that eclipselink returns null on unwrap(java.sql.Connection). Using eclipselink 2.5.1 with old SGES2.1.1.
    – dmatej
    Commented Nov 14, 2013 at 22:31
  • Resolved: The EclipseLink wiki was wrong but it's unwrap method is correct. I have fixed Eclipselink's wiki today.
    – dmatej
    Commented Nov 20, 2013 at 11:29
  • Not sure if this is relevant, but as of Jul2019, using eclipselink 2.5.0 and querydsl 4.2.1, the proposed unwrap(Connection.class) approach returns null in my own project. Solved it by creating the needed JPASQLQuery and passing the existing entityManager as parameter (see querydsl.com/static/querydsl/4.2.1/apidocs/com/querydsl/jpa/sql/…)
    – Repoker
    Commented Jul 10, 2019 at 8:03
1

But I'm unsure what I'm responsible for. Do I have to close the connection after submitting my queries? Or are I'm not allowed to close the connection, because EclipseLink also uses this connection internally.

A good and valid question. It seems that the documentation is lacking the semantics of the unwrap() calls.

Regarding EclipseLink, according from what I got from the source:

EclipseLink gives you a reference to the currently active connection which it uses for the currently active client session transaction. If no transaction is active, a new will be created, associated with the session and returned from the unwrap() method.

As a result, IMHO, a commit/rollback of such a obtained Connection may lead to undefined behavior and/or exceptions. Same is true for executing DML which changed records have been previously cached by eclipselink internal caches or for which managed entities exist.
So when using this API, especially if the underlying transaction is dirty, be careful.

If you can refer to internal eclipselink classes, you can access eclipselink internal connection pool to get a Connection exclusively (have a look at org.eclipse.persistence.sessions.server.ServerSession.getConnectionPool(String) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.