0

I'm trying to get the url that the Connection uses to connect the database as following :

getEntityManager().unwrap(SessionImplementor.class).connection().getMetaData().getURL();

But I get this error :

java.lang.IllegalStateException: No transactional EntityManager available

How can I solve this ?

Edit:

I don't understand why this question was considered as duplicated to a Hibernate solution, I'm using JPA, so I'm trying to get the url using entityManager and not sessionFactory.

Community
  • 1
  • 1
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • Look at this question: https://stackoverflow.com/questions/4351443/how-to-know-underlying-database-name-from-hibernate-provider – R. Daumann Mar 12 '18 at 15:31
  • Possible duplicate of [How to know underlying database name from hibernate provider](https://stackoverflow.com/questions/4351443/how-to-know-underlying-database-name-from-hibernate-provider) – R. Daumann Mar 12 '18 at 15:34
  • @R.Daumann how is that duplicated ? as you can see I'm using `entityManager` and not `sessionFactory` so I'm trying to get the url using JPA and not Hibernate, that's why I used `unwrap` to allow access to the provider-specific API !!! – Renaud is Not Bill Gates Mar 12 '18 at 15:37

2 Answers2

1

The solution was to add the @Transactional annotation.

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
0

The SessionImplementor Interface is form Hibernate, so which JPA Implementation are you using then?

R. Daumann
  • 86
  • 1
  • 6
  • I'm using Hibernate, but I don't have the access to the sessionFactory, unless I do `getEntityManager().unwrap(SessionImplementor.class)` which throws the error: `No transactional EntityManager available` – Renaud is Not Bill Gates Mar 12 '18 at 15:47
  • Could you try to get the connection like this: `em.getTransaction().begin(); java.sql.Connection conn = em.unwrap(java.sql.Connection.class); // ... em.getTransaction().commit();` – R. Daumann Mar 12 '18 at 15:54