Obtain an sql connection using entityManager

1

I would like to know if it is possible to establish a DB connection type sql.connection using entityManager , that is, I need to call a procedure stored in the database, usually making a connection to the database in where previously the link, user and password is defined, obtaining it by means of a getConexion like the following form: Conn = DriverManager.getConnection(connection, user, password); and declaring a variable of type CallableStatement nombreVariable = conn.prepareCall ... that procedure can be executed in the BD, but because the user's password and address were received from persistence , and that application was migrated to WebLogic , everything is handled through datasource , and connections are managed through entityManager with < strong> EclipseLink , my question is how could transform from entity to connection , all in favor to modify most of the methods already made, something like:

@PersistenceContext(unitName = "rmsprd")
    public void setEm(EntityManager em) {
        this.em = em;

..........................

connection = this.getConexion();

CallableStatement statement = connection.prepareCall("call")
    }

It should be noted that using the above procedure I throw an exception

  

"Uncompilable source code - Erroneous sym type:"

Does anyone have any ideas or suggestions?

Regarding NULL return of the entity manager

This way I get my persistenceUnit in my entityManager

@PersistenceContext(unitName="rd")
private EntityManager em ;
@Override
public void setEm(EntityManager em) {
    this.em = em;
}

and here is my method to get the connection

 @Transactional
protected Connection getConnection() throws SQLException {
    logger.info("contenido de Em en GetConnection"+em);
    if(em.unwrap(Connection.class) != null) {
        logger.info("entre a el if CONEXION ");
        logger.info("entityManager2"+this.em);
        return (Connection)this.em.unwrap(Connection.class);
    } else {
        logger.info("entre a el else CONEXION ");
        logger.info("contenido de Em en GetConnection"+em);
        Map datosConexion = em.getProperties();
        String url = (String)datosConexion.get("javax.persistence.jdbc.url");
        String user = (String)datosConexion.get("javax.persistence.jdbc.user");
        String password = (String)datosConexion.get("javax.persistence.jdbc.password");
        return DriverManager.getConnection(url, user, password);
    }

}

Even though the method is of a transactional type, the exception still comes back to me, in fact the EM variable is already null, to which this could be due?

persistence.xml configuration

<persistence-unit name="rmsprd" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>rmsprd</jta-data-source>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
    </properties>
</persistence-unit>
    
asked by Jdeveloper 22.02.2017 в 20:17
source

1 answer

1

If you use EclipseLink, and according to the JPA version:

JPA 2.0

    entityManager.getTransaction().begin();
    java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class); // unwraps the Connection class.

    ...
    entityManager.getTransaction().commit();

JPA 1.0

entityManager.getTransaction().begin();
UnitOfWork unitOfWork = (UnitOfWork)((JpaEntityManager)entityManager.getDelegate()).getActiveSession();
unitOfWork.beginEarlyTransaction();
Accessor accessor = unitOfWork.getAccessor();
accessor.incrementCallCount(unitOfWork.getParent());
accessor.decrementCallCount();
java.sql.Connection connection = accessor.getConnection();
...
entityManager.getTransaction().commit();

Taken from a SO thread , which in turn references the EclipseLink wiki .

Adding to the comments of Luggi Mendoza, an example of the new way to call PLs, JPA 2.1 onwards:

StoredProcedureQuery query = this.em.createStoredProcedureQuery("nombreProcedimiento");
query.registerStoredProcedureParameter("paramEntrada1", Double.class, ParameterMode.IN);
query.registerStoredProcedureParameter("paramEntrada2", Double.class, ParameterMode.IN);
query.registerStoredProcedureParameter("paramSalida1", Double.class, ParameterMode.OUT);
query.setParameter("paramEntrada1", 1.23d);
query.setParameter("paramEntrada2", 4.56d);

query.execute();
Double sum = (Double) query.getOutputParameterValue("paramSalida1");
    
answered by 23.02.2017 в 12:19