JPA and PostgreSQL

I am participating in a project that uses the JPA implementation for EclipseLink to talk to a PostgreSQL database. I have a task for which PostgreSQL NOTIFY / LISTEN seems ideally suited. Unfortunately, I'm new to JPA, and I'm struggling to figure out how to make it work. Therefore, I think I have two questions; an answer to me will make me happy.

1) What is the best way for me to get a direct JDBC connection to the database? (Which, I sincerely hope, will be of type org.postgresql.PGConnection .)

OR

2) What is the best way to emulate / access org.postgresql.PGConnection.getNotifications() via EclipseLink JPA?

Thank you very much for your help.


Edit: Two working solutions! I like this site. If anyone can say anything about hidden errors / benefits that will make the Pascal or Balus solution better than the other, before I post a checkmark, I would like to hear it.
+4
source share
2 answers

Getting a JDBC connection from EntityManager in the EclipseLink is answered in the EclipseLink wiki .

The method is different version of the JPA API. Here is an excerpt from the wiki:

JPA 2.0

 entityManager.getTransaction().begin(); java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class); ... entityManager.getTransaction().commit(); 

JPA 1.0

 entityManager.getTransaction().begin(); UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)((JpaEntityManager)entityManager.getDelegate()).getActiveSession(); unitOfWork.beginEarlyTransaction(); java.sql.Connection connection = unitOfWork.getAccessor().getConnection(); ... entityManager.getTransaction().commit(); 
+7
source

You can get it from org.eclipse.persistence.internal.jpa.EntityManagerImpl , which is returned by EntityManager.getDelegate() :

 java.sql.Connection conn = ((EntityManagerImpl)(em.getDelegate())).getServerSession().getAccessor().getConnection(); 
+2
source

All Articles