KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.persistence.TransactionRequiredException;
25
26 import oracle.toplink.essentials.internal.ejb.cmp3.base.*;
27 import oracle.toplink.essentials.exceptions.TransactionException;
28 import oracle.toplink.essentials.internal.localization.ExceptionLocalization;
29 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
30
31 /**
32  * INTERNAL:
33  * Base EntityTransactionWrapper
34  * The EntityTransactionWrapper is used to make in transparent to an EntityManager
35  * what kind of transaction is being used. Transaction type can either be JTATransaction
36  * or EntityTransaciton and they are mutually exclusive. This is the implementation for
37  * EntityTransaction
38  *
39  * @see oracle.toplink.essentials.internal.ejb.cmp3.transaction.EntityTransactionWrapper
40  * @see oracle.toplink.essentials.internal.ejb.cmp3.transaction.jdk14.EntityTransactionWrapper
41  */

42 public class EntityTransactionWrapper extends TransactionWrapperImpl {
43
44     protected EntityTransactionImpl entityTransaction;
45
46     public EntityTransactionWrapper(EntityManagerImpl entityManager) {
47         super(entityManager);
48     }
49     
50
51     /**
52      * INTERNAL:
53      * This method will be used to check for a transaction and throws exception if none exists.
54      * If this methiod returns without exception then a transaction exists.
55      * This method must be called before accessing the localUOW.
56      */

57     public Object JavaDoc checkForTransaction(boolean validateExistence){
58         if (entityTransaction != null && entityTransaction.isActive()) {
59             return entityTransaction;
60         }
61         if (validateExistence){
62             throwCheckTransactionFailedException();
63         }
64         return null;
65     }
66
67     /**
68      * INTERNAL:
69      * THis method is used to get the active UnitOfWork. It is special in that it will
70      * return the required RepeatableWriteUnitOfWork required by the EntityManager. Once
71      * RepeatableWrite is merged into existing UnitOfWork this code can go away.
72      */

73     public RepeatableWriteUnitOfWork getTransactionalUnitOfWork(Object JavaDoc transaction){
74         if (transaction == null){
75             return null;
76         }
77         if (this.localUOW == null){
78             this.localUOW = new RepeatableWriteUnitOfWork(entityManager.getServerSession().acquireClientSession());
79             this.localUOW.setShouldCascadeCloneToJoinedRelationship(true);
80         }
81         return (RepeatableWriteUnitOfWork)this.localUOW;
82     }
83     
84     public EntityManagerImpl getEntityManager(){
85         return entityManager;
86     }
87
88     public void registerUnitOfWorkWithTxn(UnitOfWorkImpl uow){
89         throw new TransactionRequiredException(ExceptionLocalization.buildMessage("join_trans_called_on_entity_trans"));// no JTA transactions availab
90
}
91     
92     public boolean shouldClose() {
93         return (this.entityTransaction == null || ! this.entityTransaction.isActive());
94     }
95
96     public boolean shouldFlushBeforeQuery(UnitOfWorkImpl uow){
97         return true;
98     }
99     
100     protected void throwCheckTransactionFailedException() {
101         throw TransactionException.transactionNotActive();
102     }
103
104     /**
105      * These two method used for closing of EntityManager in case there is a transaction in progress:
106      * The first method is called by EntityManager.close method to mark the current transaction,
107      * the second one is called by EntityManager.verifyOpen method.
108      */

109     public void markLastTransaction() {
110         if(entityTransaction != null) {
111             entityTransaction.markLastTransaction();
112         }
113     }
114     public boolean hasLastTransactionCompleted() {
115         return entityTransaction != null && entityTransaction.hasLastTransactionCompleted();
116     }
117
118 }
119
Popular Tags