Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Getting a JDBC Connection from an EntityManager returns null in EclipseLink 2.0.2(Attempting to obtain a JDBC Connection from an EntityManager using JPA 1.0 returns null)
Getting a JDBC Connection from an EntityManager returns null in EclipseLink 2.0.2 [message #649824] Thu, 20 January 2011 16:35 Go to next message
Doug Gschwind is currently offline Doug GschwindFriend
Messages: 18
Registered: July 2009
Junior Member
Hello,

We are using JPA 1.0 with the EclipseLink 2.0.2 distribution and finding that :

UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)((JpaEntityManager)em.getDelegate()).getActi veSession();
unitOfWork.beginEarlyTransaction();
return unitOfWork.getAccessor().getConnection();

as described here ( http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getti ng_a_JDBC_Connection_from_an_EntityManager) is unexpectedly returning null.

Prior to the above code being executed, we obtain an EntityManager reference, obtain a Transaction reference against it, begin the transaction modify an instance of a JPA mapped class, then attempt to obtain the JDBC Connection from the EntityManager which is returning null.

The transaction that was started otherwise commits fine.

Has anyone experienced this problem and has found a suitable workaround?

Thank you,

Doug Gschwind
Re: Getting a JDBC Connection from an EntityManager returns null in EclipseLink 2.0.2 [message #650361 is a reply to message #649824] Mon, 24 January 2011 15:57 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I think the issue is that your are using an external transaction controller, i.e. a JTA DataSource.

I updated the code in the example to handle this,

entityManager.getTransaction().begin();
UnitOfWork unitOfWork = (UnitOfWork)((JpaEntityManager)entityManager.getDelegate()).getActiveSession();
unitOfWork.beginEarlyTransaction();
Accessor accessor = unitOfWork.getAccessor();
accessor.incrementCallCount(unitOfWork.getParent());
accessor.decrementCallCount();
java.sql.Connection connection = accessor.getConnection();
...
entityManager.getTransaction().commit();


You could also acquire the connection directly from the JTA DataSource.


James : Wiki : Book : Blog : Twitter
Re: Getting a JDBC Connection from an EntityManager returns null in EclipseLink 2.0.2 [message #650407 is a reply to message #649824] Mon, 24 January 2011 18:07 Go to previous messageGo to next message
Doug Gschwind is currently offline Doug GschwindFriend
Messages: 18
Registered: July 2009
Junior Member
Thanks for your response James, that code change appears to be working for us.

A follow on question though. Is that technique that you describe also appropriate when the datasource is a non-JTA datasource or when simply JTA is not available?

Thank you,

Doug
Re: Getting a JDBC Connection from an EntityManager returns null in EclipseLink 2.0.2 [message #650410 is a reply to message #649824] Mon, 24 January 2011 18:13 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

The code will work with either.


James : Wiki : Book : Blog : Twitter
Previous Topic:Querying ManyToMany relationship
Next Topic:eclipselink.ddl-generation - best practice
Goto Forum:
  


Current Time: Thu Apr 25 08:13:21 GMT 2024

Powered by FUDForum. Page generated in 0.02668 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top