views:

523

answers:

2
+2  Q: 

JPA and PostgreSQL

I'm on a project that uses the EclipseLink implementation of JPA to talk to a PostgreSQL database. I have a task for which PostgreSQL NOTIFY/LISTEN seems like a perfect fit. Unfortunately, I'm a JPA newb, and am struggling to figure out how to make it work. So I guess I really have two questions; answering either one will make me happy.

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

OR

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

Thank you very much for your help.


Edit: Two working solutions! I love this site. If anybody has anything to say about hidden gotchas/benefits that would make either Pascal's or Balus's solution better than the other before I hand out the checkmark, I'd like to hear it.

+3  A: 

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

The way differs per JPA API version. Here's an extract 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();
BalusC
Works nicely, thanks! (And I think I came across this during earlier Google flailing; not sure how I managed to not implement it.) Are there any inobvious gotchas/benefits that would make this better than Pascal's solution?
BlairHippo
Which JPA API version are you using? If 2.0, then I definitely would go for the `EntityManager#unwrap()`. If 1.0, then I personally find Pascal's solution clearer/nicer, but I am not sure if that it will always return a working connection as you're outside the Transaction/UnitOfWork then. Otherwise it would be described as is in EclipseLink's own Wiki.
BalusC
I'm actually using 1.0, but if there's any suspicion that Pascal's might not always work, this is the one I have to go with. Thank you very much for your help.
BlairHippo
+2  A: 

You should be able to get it from org.eclipse.persistence.internal.jpa.EntityManagerImpl that is returned by EntityManager.getDelegate():

java.sql.Connection conn = ((EntityManagerImpl)(em.getDelegate())).getServerSession().getAccessor().getConnection();
Pascal Thivent
Works nicely, thanks! Are there any inobvious gotchas/benefits that would make this better than Balus's solution?
BlairHippo
For JPA 1.0, this solution is less verbose and it has worked for me. But it is not documented. If you are using JPA 2.0, go for the EclipseLink wiki solution given by BalusC.
Pascal Thivent