KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > TxEntityManagerLifeCycle


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * 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 326 2006-04-14 08:20:59Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence;
27
28 import javax.persistence.EntityManager;
29 import javax.transaction.Synchronization JavaDoc;
30 import javax.transaction.Transaction JavaDoc;
31
32 /**
33  * This class manages the lifecycle of an entity manager with transaction scoped
34  * persistence context. It means that it should be closed when the transaction
35  * is committed or rollbacked.
36  * @author Florent Benoit
37  */

38 public class TxEntityManagerLifeCycle implements Synchronization JavaDoc {
39
40     /**
41      * Entity manager that is referenced.
42      */

43     private EntityManager entityManager = null;
44
45     /**
46      * Handler that manages tx entity manager (to release the tx).
47      */

48     private TxEntityManagerHandler txEntityManagerHandler = null;
49
50     /**
51      * Tx's association to release.
52      */

53     private Transaction JavaDoc tx = null;
54
55     /**
56      * @param entityManager Entity manager that is managed (lifecycle).
57      * @param tx the transaction that needs to be released in the handler.
58      * @param txEntityManagerHandler handler on which release association with TX
59      */

60     public TxEntityManagerLifeCycle(final EntityManager entityManager, final Transaction JavaDoc tx,
61             final TxEntityManagerHandler txEntityManagerHandler) {
62         this.entityManager = entityManager;
63         this.tx = tx;
64         this.txEntityManagerHandler = txEntityManagerHandler;
65
66         // Needs to join the transaction.
67
entityManager.joinTransaction();
68     }
69
70     /**
71      * The beforeCompletion method is called by the transaction manager prior to
72      * the start of the two-phase transaction commit process. This call is
73      * executed with the transaction context of the transaction that is being
74      * committed.
75      */

76     public void beforeCompletion() {
77     }
78
79     /**
80      * This method is called by the transaction manager after the transaction is
81      * committed or rolled back.
82      * @param status The status of the transaction completion.
83      */

84     public void afterCompletion(final int status) {
85         // release tx
86
txEntityManagerHandler.release(tx);
87         txEntityManagerHandler = null;
88
89         // Close entityManager
90
entityManager.close();
91
92     }
93
94 }
95
Popular Tags