01: /**
02: * EasyBeans
03: * Copyright (C) 2006 Bull S.A.S.
04: * Contact: easybeans@ow2.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * --------------------------------------------------------------------------
22: * $Id: TxEntityManagerLifeCycle.java 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.persistence;
25:
26: import javax.persistence.EntityManager;
27: import javax.transaction.Synchronization;
28: import javax.transaction.Transaction;
29:
30: /**
31: * This class manages the lifecycle of an entity manager with transaction scoped
32: * persistence context. It means that it should be closed when the transaction
33: * is committed or rollbacked.
34: * @author Florent Benoit
35: */
36: public class TxEntityManagerLifeCycle implements Synchronization {
37:
38: /**
39: * Entity manager that is referenced.
40: */
41: private EntityManager entityManager = null;
42:
43: /**
44: * Handler that manages tx entity manager (to release the tx).
45: */
46: private TxEntityManagerHandler txEntityManagerHandler = null;
47:
48: /**
49: * Tx's association to release.
50: */
51: private Transaction tx = null;
52:
53: /**
54: * @param entityManager Entity manager that is managed (lifecycle).
55: * @param tx the transaction that needs to be released in the handler.
56: * @param txEntityManagerHandler handler on which release association with TX
57: */
58: public TxEntityManagerLifeCycle(final EntityManager entityManager,
59: final Transaction tx,
60: final TxEntityManagerHandler txEntityManagerHandler) {
61: this .entityManager = entityManager;
62: this .tx = tx;
63: this .txEntityManagerHandler = txEntityManagerHandler;
64:
65: // Needs to join the transaction.
66: entityManager.joinTransaction();
67: }
68:
69: /**
70: * The beforeCompletion method is called by the transaction manager prior to
71: * the start of the two-phase transaction commit process. This call is
72: * executed with the transaction context of the transaction that is being
73: * committed.
74: */
75: public void beforeCompletion() {
76: }
77:
78: /**
79: * This method is called by the transaction manager after the transaction is
80: * committed or rolled back.
81: * @param status The status of the transaction completion.
82: */
83: public void afterCompletion(final int status) {
84: // release tx
85: txEntityManagerHandler.release(tx);
86: txEntityManagerHandler = null;
87:
88: // Close entityManager
89: entityManager.close();
90:
91: }
92:
93: }
|