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>