2

I've a web application that uses EJB 3. Persistence is managed by container so the entity manager is injected.

@PersistenceContext(name="Ejbs", type=PersistenceContextType.TRANSACTION)
private EntityManager em;

I need in one of my EJB methods to get the jdbc connection. I've read that it is possible with the next code:

java.sql.Connection conn = em.unwrap(java.sql.Connection.class);

But I'm always getting null.

Why? How could I obtain the underlying connection?

UPDATE:

After changing the datasource definition it started to work. I remove a line of my datasource definition and it started to work. The line I've removed was

<driver-class>oracle.jdbc.OracleDriver</driver-class>

My datasource definion is:

<datasource jta="true" jndi-name="java:/jboss/datasources/Unificado" pool-name="Unificado" enabled="true" use-ccm="true">
      <connection-url>jdbc:oracle:thin:@10.12.54.186:1522:prd</connection-url>                    
      <driver>ojdbc6</driver>
      <security>
         <user-name>unificado</user-name>
         <password>*******</password>
      </security>
      <validation>                        
         <validate-on-match>false</validate-on-match>
         <background-validation>false</background-validation>
      </validation>
      <statement>
         <share-prepared-statements>false</share-prepared-statements>
      </statement>
</datasource>
Eduardo
  • 1,169
  • 5
  • 21
  • 56
  • Why don't you just inject the DataSource that the EntityManager is getting its connection from? Or executing native queries with the EntityManager? – JB Nizet Jan 26 '15 at 17:38
  • Thanks for your reply. I don't execute native queries because i want to execute a stored procedure with blob parameters. – Eduardo Jan 26 '15 at 17:44
  • Perhaps the docs for your JPA implementation have some note for that. The one I use (DataNucleus JPA) provides me access to their internal wrapper for the underlying connection. – Neil Stockton Jan 26 '15 at 18:09
  • I don't think it is a matter of the JPA implementation. I've deployed the application on a JBoss 7.1. When I test the application in a jboss installed on Windows it works properly, but when I test the application in a a jboss installed on Linux, it doesn't work. – Eduardo Jan 27 '15 at 07:53
  • Are you using hibernate for your implementation? – kmansoor Jan 27 '15 at 22:40
  • No, I'm using EclipseLink – Eduardo Jan 28 '15 at 07:51
  • I'm not sure what I've done but it works now. I've just changed how the datasource was defined in standalone.xml. I'm going to update my question with the datasource definition. – Eduardo Jan 28 '15 at 07:53

3 Answers3

4

valid only for Spring framework

inject:

@PersistenceContext
private EntityManager entityManager;

and get connection using:

    EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) entityManager.getEntityManagerFactory();
    Connection connection = info.getDataSource().getConnection();
Davide Consonni
  • 2,094
  • 26
  • 27
1

You can get connection using bellow way:

Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> doSomeStuffWith(connection));

How you get EntityManager?

@PersistenceContext
private EntityManager entityManager;

Here is the imports:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Session;

Now, if you have problem on connection pooling then check this tread

Hope this will help you.

Thanks :)

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
0

I've used

    //... some insert and update on EntityManager
    SessionImplementor sessionImp = (SessionImplementor) em.getDelegate();
    Connection con = sessionImp.connection();

to get JDBC connection as suggested in other answers but in my case I had also to flush the entity manager

    em.flush();

in order to "see" in my selects all the entities previously persisted both using em and repositories.

Roberto Petrilli
  • 711
  • 1
  • 8
  • 22