Hibernate EntityManager does not allow changing database connection information after EntityManagerFactory creation

Description

I'm trying to migrate my applications from pure Hibernate to JPA.

In my environment, I have a shared library which creates a SessionFactory at startup. Several Web applications use this library and its shared SessionFactory. Also, each Web application uses a particular datasource with a different user/password, although all the applications access the same database. Upon a request, a Session is obtained from a JDBC connection, which is obtained from the application server's managed pool, referenced by the Web application specific JNDI bound datasource.

When I try to migrate this cenary to JPA, in the same way, I need to create an EntityManagerFactory at startup and let each Web application request create a new EntityManager from the same factory but with different database connection information. But I had the following problems:

  • When the EntityManagerFactory is created with a default connection information and the EntityManager is created through EntityManagerFactory.createEntityManager(map), where map contains a JNDI datasource bound to the "javax.persistence.nonJtaDataSource" property, the same EntityManagerFactory connection information is used, and the datasource connection information is completely ignored.

  • When the EntityManagerFactory is created without any initial connection information, the following error occurs when an EntityManager is created with datasource information and the underlying JDBC connection needs to accessed:
    java.lang.UnsupportedOperationException: The user must supply a JDBC connection
    at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
    at org.hibernate.jdbc.BorrowedConnectionProxy.invoke(BorrowedConnectionProxy.java:50)
    at $Proxy116.prepareStatement(Unknown Source)

I studyied Hibernate's source code and I'm afraid Hibernate does not allow changing connection information (user, password, datasource, etc.) after EntityManagerFactory creation.

Environment

hibernate-entity-manager-3.2.1.ga, hibernate-3.2.4.sp1, Oracle database

Assignee

Unassigned

Reporter

Monique Louise de Barros Monteiro

Labels

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

Yes, likely

Pull Request

None

backportDecision

None

backportReEvaluate

None

Affects versions

Priority

Major
Configure