You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@opentempo.com> on 2011/09/15 22:00:04 UTC
JDBC operation in JPA transaction
Hello,
consider the following code in an entity "save" method:
em.getTransaction().begin();
try {
// get a hold of JDBC connection so we can join transaction
OpenJPAEntityManager emo = (OpenJPAEntityManager) em;
Connection connection = (Connection) emo.getConnection();
// ... do JDBC insert
connection.executeUpdate(...);
em.merge(this);
em.getTransaction().commit();
} catch (...) {
}
The idea is to start a transaction on the EM, perform an operation in JDBC,
then commit the EM transaction. When the commit fails the JDBC operation is
successful. I expected the rollback of the transaction to rollback the
operation on the JDBC connection, too. Am I doing this right? How can I
perform a JDBC operation in a JPA transaction and have it rolled back upon
failure of commit?
Thanks.
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com
Re: JDBC operation in JPA transaction
Posted by Daryl Stultz <da...@opentempo.com>.
On Fri, Sep 16, 2011 at 5:34 AM, Miłosz Tylenda <mt...@o2.pl> wrote:
> Try closing the connection:
>
> connection.executeUpdate(...);
> connection.close();
>
> Without closing, I suspect the entity manager and your JDBC update operate
> on different connections.
>
> It seems like closing the connection before the em commit would guarantee
different connections.
> The openjpa.ConnectionRetainMode property may also have some impact here.
>
I tried "transaction" and "always" with no change in behavior. I can work
around it in this case but would like to know how to do this effectively. I
do have a case where I want the EM to read uncommitted data manipulated by
JDBC.
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
http://www.opentempo.com
mailto:daryl.stultz@opentempo.com
Re: JDBC operation in JPA transaction
Posted by Miłosz Tylenda <mt...@o2.pl>.
Hi Daryl,
Try closing the connection:
connection.executeUpdate(...);
connection.close();
Without closing, I suspect the entity manager and your JDBC update operate on different connections.
The openjpa.ConnectionRetainMode property may also have some impact here.
Greetings,
Milosz
> Hello,
>
> consider the following code in an entity "save" method:
>
> em.getTransaction().begin();
> try {
> // get a hold of JDBC connection so we can join transaction
> OpenJPAEntityManager emo = (OpenJPAEntityManager) em;
> Connection connection = (Connection) emo.getConnection();
> // ... do JDBC insert
> connection.executeUpdate(...);
> em.merge(this);
> em.getTransaction().commit();
> } catch (...) {
> }
>
> The idea is to start a transaction on the EM, perform an operation in JDBC,
> then commit the EM transaction. When the commit fails the JDBC operation is
> successful. I expected the rollback of the transaction to rollback the
> operation on the JDBC connection, too. Am I doing this right? How can I
> perform a JDBC operation in a JPA transaction and have it rolled back upon
> failure of commit?
>
> Thanks.
>
>