blob: f12a51ab627390413e12213e30d7a30cc8452abd [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.&nbsp; The EntityTransaction Interface</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.2 User's Guide"><link rel="up" href="jpa_overview_trans.html" title="Chapter&nbsp;9.&nbsp; Transaction"><link rel="prev" href="jpa_overview_trans.html" title="Chapter&nbsp;9.&nbsp; Transaction"><link rel="next" href="jpa_overview_query.html" title="Chapter&nbsp;10.&nbsp; JPA Query"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.&nbsp;
The EntityTransaction Interface
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_trans.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;9.&nbsp;
Transaction
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_query.html">Next</a></td></tr></table><hr></div><div class="section" title="2.&nbsp; The EntityTransaction Interface"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="jpa_overview_trans_local">2.&nbsp;
The EntityTransaction Interface
</h2></div></div></div>
<a class="indexterm" name="d5e2893"></a>
<div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="129"><tr><td><img src="img/jpa-transaction.png"></td></tr></table></div>
<p>
JPA integrates with your container's <span class="emphasis"><em>managed</em></span> transactions,
allowing you to use the container's declarative transaction demarcation and its
Java Transaction API (JTA) implementation for transaction management. Outside of
a container, though, you must demarcate transactions manually through JPA. The
<code class="classname">EntityTransaction</code> interface controls unmanaged
transactions in JPA.
</p>
<pre class="programlisting">
public void begin();
public void commit();
public void rollback();
</pre>
<p>
<a class="indexterm" name="d5e2904"></a>
<a class="indexterm" name="d5e2907"></a>
<a class="indexterm" name="d5e2910"></a>
<a class="indexterm" name="d5e2913"></a>
<a class="indexterm" name="d5e2916"></a>
The <code class="methodname">begin</code>, <code class="methodname">commit</code>, and
<code class="methodname">rollback</code> methods demarcate transaction boundaries. The
methods should be self-explanatory: <code class="methodname">begin</code> starts a
transaction, <code class="methodname">commit</code> attempts to commit the
transaction's changes to the datastore, and <code class="methodname">rollback</code>
aborts the transaction, in which case the datastore is "rolled back" to its
previous state. JPA implementations will automatically roll back transactions if
any exception is thrown during the commit process.
</p>
<p>
Unless you are using an extended persistence context, committing or rolling back
also ends the persistence context. All managed entities will be detached from the
<code class="classname">EntityManager</code>.
</p>
<pre class="programlisting">
public boolean isActive();
</pre>
<p>
<a class="indexterm" name="d5e2929"></a>
Finally, the <code class="methodname">isActive</code> method returns <code class="literal">true
</code> if the transaction is in progress (<code class="methodname">begin</code>
has been called more recently than <code class="methodname">commit</code> or
<code class="methodname">rollback</code>), and <code class="literal">false</code> otherwise.
</p>
<div class="example"><a name="jpa_overview_trans_group"></a><p class="title"><b>Example&nbsp;9.1.&nbsp;
Grouping Operations with Transactions
</b></p><div class="example-contents">
<pre class="programlisting">
public void transferFunds(EntityManager em, User from, User to, double amnt) {
// note: it would be better practice to move the transaction demarcation
// code out of this method, but for the purposes of example...
Transaction trans = em.getTransaction();
trans.begin();
try
{
from.decrementAccount(amnt);
to.incrementAccount(amnt);
trans.commit();
}
catch (RuntimeException re)
{
if (trans.isActive())
trans.rollback(); // or could attempt to fix error and retry
throw re;
}
}
</pre>
</div></div><br class="example-break">
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_trans.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_trans.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_query.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;9.&nbsp;
Transaction
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;Chapter&nbsp;10.&nbsp;
JPA Query
</td></tr></table></div></body></html>