0

I am migrating my application from hibernate to eclipseLink. In Hibernate, I am getting connection in this way.

import org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl;
public class HibernateXADataSourceConnectionProvider extends DatasourceConnectionProviderImpl {

    @Override
    public Connection getConnection() throws SQLException {
        if (getDataSource() != null && getDataSource() instanceof XADataSource) {
            XAConnection xaConnection = ThreadLocalXAConnection.get();
            if (xaConnection == null) {
                xaConnection = ((XADataSource) getDataSource()).getXAConnection();
            }
            return xaConnection.getConnection();
        }
        return super.getConnection();
    }
}

How can I do this in eclipse link?

Atul D
  • 1
  • Does this answer your question? [EclipseLink JPA 2.1 User supplied connection](https://stackoverflow.com/questions/24452376/eclipselink-jpa-2-1-user-supplied-connection) – Ashish Patil Jul 21 '22 at 15:24

1 Answers1

0

You can use the: "javax.persistence.jtaDataSource"/"jakarta.persistence.jtaDataSource" or "javax.persistence.nonJtaDataSource"/"jakarta.persistence.nonJtaDataSource" persistence properties.

These normally are used to pass in a string for lookup in the container context, but EclipseLink accepts passing in any javax.sql.DataSource instance directly.

For example:

javax.sql.DataSource yourDatasourceinstance = getDataSource();
HashMap<String, Object> map = new HashMap<>();
map.put("javax.persistence.nonJtaDataSource", yourDatasourceinstance);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, map);

The HibernateXADataSourceConnectionProvider class though looks like a wrapper somehow configured to go over the datasource provided to Hibernate; you'd have to tell how you configure that datasource and get it into Hibernate if you need more from an answer to convert that specifically to EclipseLInk. I'm not sure why if you are using an XA datasource in a JTA environment you would really need such an implementation class though.

Chris
  • 20,138
  • 2
  • 29
  • 43