Thursday 27 June 2013

Getting JDBC Connection from EntityManager ( EclipseLink )

when things gets too complex, your hibernate/eclipselink jpa implementations won't help. You may need to access the underlying JDBC connection. Logic to extract a JDBC connection from JTA environment and non-JTA environment is not the same.
If you are using JTA datasource managed by the container, then you can get the connection directly from the JavaEE container Datasource. Just inject or lookup the datasource from JNDI. As long as you are with in a JTA trasactional context, the JDBC connection will be the same connection used by JPA.
If you are using non-JTA datasource, then you can still access the datasource from the javaEE container if you want a new connection. If you want the same connection used by JPA, then you can get it from JPAEntityManager interface.


Getting JDBC connection from JTA Datasource:

  First take a look at persistence.xml
  

  Now inject the Datasource bean in your java file
  

Getting JDBC connection from NON-JTA Datasource:

  JPA 2.0

  JPA 1.0

No comments:

Post a Comment