| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>2. The EntityTransaction Interface</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><base href="display"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA 2.1 User's Guide"><link rel="up" href="jpa_overview_trans.html" title="Chapter 9. Transaction"><link rel="prev" href="jpa_overview_trans.html" title="Chapter 9. Transaction"><link rel="next" href="jpa_overview_query.html" title="Chapter 10. 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. |
| The EntityTransaction Interface |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_trans.html">Prev</a> </td><th width="60%" align="center">Chapter 9. |
| Transaction |
| </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_query.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jpa_overview_trans_local"></a>2. |
| The EntityTransaction Interface |
| </h2></div></div></div><a class="indexterm" name="d0e5934"></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="d0e5955"></a> |
| <a class="indexterm" name="d0e5961"></a> |
| <a class="indexterm" name="d0e5967"></a> |
| <a class="indexterm" name="d0e5973"></a> |
| <a class="indexterm" name="d0e5979"></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="d0e6012"></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 9.1. |
| 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> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_trans.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_query.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 9. |
| Transaction |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 10. |
| JPA Query |
| </td></tr></table></div></body></html> |