openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daryl Stultz <>
Subject Sharing JDBC connection
Date Mon, 18 Oct 2010 18:41:45 GMT
Hello all,

A while back I asked if there was a way to share a connection between my
JDBC code and JPA code. It was suggested that I open an transaction on the
EM and then access the connection on the transaction. I'm sure this will
work, but I need to do the opposite.

I have a code block that gets a JDBC connection, loops over some things and
performs actions on one transaction per loop. In each loop some JPA action
may take place on an EM. The problem is that the JDBC connection is in a
transaction, and the queries run from the EM don't see the newly inserted

I happen to be using a DataSource to supply OpenJPA with connections. The
JDBC connection is available via a ThreadLocal variable (as is one and only
one EM). I did a quick test where my DataSource simply provides a wrapper of
the JDBC connection. I override the close() method to do nothing so the
outer JDBC code is not killed. It works. The EM queries are performed on the
same JDBC connection. Naturally, I do not expect OpenJPA to "join" the JDBC
connection, nor do I expect to need to open a transaction on the EM in the
loop. (Interestingly, though, it does not crash when I do so.)

So given my simple test seems to work, I'm wondering if anyone can tell me
if I'll run into some major trouble later. Am I playing with fire?
Ultimately this is "transition" code from JDBC to JPA, but it may be around
for a while.


Daryl Stultz
6 Degrees Software and Consulting, Inc.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message