KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > transaction > base > JTATransactionWrapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.transaction.base;
23
24 import oracle.toplink.essentials.transaction.AbstractTransactionController;
25 import oracle.toplink.essentials.exceptions.TransactionException;
26 import oracle.toplink.essentials.internal.ejb.cmp3.base.*;
27 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
28
29 /**
30  * INTERNAL:
31  * Base JTATransactionWrapper
32  * The JTATransactionWrapper is used to make in transparent to an EntityManager
33  * what kind of transaction is being used. Transaction type can either be JTATransaction
34  * or EntityTransaciton and they are mutually exclusive. This is the implementation for JTA
35  * Transaction
36  *
37  * @see oracle.toplink.essentials.internal.ejb.cmp3.transaction.JTATransactionWrapper
38  * @see oracle.toplink.essentials.internal.ejb.cmp3.transaction.jdk14.JTATransactionWrapper
39  */

40 public class JTATransactionWrapper extends TransactionWrapperImpl {
41
42     //This is a quick reference for the external Transaction Controller
43
protected AbstractTransactionController txnController;
44     
45     protected Object JavaDoc lastTransaction;
46     
47     public JTATransactionWrapper(EntityManagerImpl entityManager) {
48         super(entityManager);
49         this.txnController = (AbstractTransactionController)entityManager.getServerSession().getExternalTransactionController();
50     }
51
52     /**
53      * INTERNAL:
54      * This method will be used to check for a transaction and throws exception if none exists.
55      * If this methiod returns without exception then a transaction exists.
56      * This method must be called before accessing the localUOW.
57      */

58     public Object JavaDoc checkForTransaction(boolean validateExistence){
59         Object JavaDoc transaction = this.txnController.getTransaction();
60         if (validateExistence && (transaction == null)){
61             throwCheckTransactionFailedException();
62         }
63         return transaction;
64     }
65
66     /**
67      * INTERNAL:
68      * Internal clear the underlying data structures that this transaction owns.
69      */

70     public void clear(){
71         if (txnKey != null && this.entityManager.shouldPropagatePersistenceContext()){
72             this.txnController.getUnitsOfWork().remove(txnKey);
73         }
74         localUOW.release();
75         localUOW = null;
76     }
77     
78     /**
79      * INTERNAL:
80      * THis method is used to get the active UnitOfWork. It is special in that it will
81      * return the required RepeatableWriteUnitOfWork required by the EntityManager. Once
82      * RepeatableWrite is merged into existing UnitOfWork this code can go away.
83      */

84     public RepeatableWriteUnitOfWork getTransactionalUnitOfWork(Object JavaDoc transaction){
85         if (transaction == null){
86             return null;
87         }
88         if (this.entityManager.shouldPropagatePersistenceContext()){
89             Object JavaDoc newTxnKey = this.txnController.getTransactionKey(transaction);
90             if (this.txnKey == newTxnKey){
91                 return (RepeatableWriteUnitOfWork)this.localUOW;
92             }
93             this.txnKey = newTxnKey;
94             this.localUOW = (RepeatableWriteUnitOfWork)this.txnController.lookupActiveUnitOfWork(transaction);
95             if (this.localUOW == null){
96                 this.localUOW = new RepeatableWriteUnitOfWork(entityManager.getServerSession().acquireClientSession());
97                 this.localUOW.registerWithTransactionIfRequired();
98                 this.localUOW.setShouldCascadeCloneToJoinedRelationship(true);
99                 this.txnController.getUnitsOfWork().put(newTxnKey, this.localUOW);
100             }
101         }else if (this.localUOW == null){
102             this.localUOW = new RepeatableWriteUnitOfWork(entityManager.getServerSession().acquireClientSession());
103             this.localUOW.registerWithTransactionIfRequired();
104             this.localUOW.setShouldCascadeCloneToJoinedRelationship(true);
105         }
106         return (RepeatableWriteUnitOfWork)this.localUOW;
107     }
108     
109     protected void throwUserTransactionException() {
110         throw TransactionException.entityTransactionWithJTANotAllowed();
111     }
112
113     protected void throwCheckTransactionFailedException() {
114         throw TransactionException.externalTransactionNotActive();
115     }
116
117     public void registerUnitOfWorkWithTxn(UnitOfWorkImpl uow){
118         uow.registerWithTransactionIfRequired();
119     }
120     
121     public boolean shouldClose() {
122         if (!txnController.noTransactionOrRolledBackOrCommited()) {
123             return false;
124         }
125         return true;
126     }
127
128     /**
129      * We should only flush the entity manager before the query if the query is
130      * joined to a transaction
131      */

132     public boolean shouldFlushBeforeQuery(UnitOfWorkImpl uow){
133         return uow.isSynchronized();
134     }
135     
136     /**
137      * These two method used for closing of EntityManager in case there is a transaction in progress:
138      * The first method is called by EntityManager.close method to mark the current transaction,
139      * the second one is called by EntityManager.verifyOpen method.
140      */

141     public void markLastTransaction() {
142         if(lastTransaction == null) {
143             lastTransaction = txnController.getTransaction();
144         }
145     }
146     public boolean hasLastTransactionCompleted() {
147         if(lastTransaction != null) {
148             Object JavaDoc transaction = txnController.getTransaction();
149             if(transaction != null) {
150                 return !lastTransaction.equals(transaction);
151             } else {
152                 return true;
153             }
154         } else {
155             return false;
156         }
157     }
158 }
159
Popular Tags