KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > JtaEntityManager


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.jpa;
20
21 import javax.persistence.EntityTransaction;
22 import javax.persistence.TransactionRequiredException;
23 import javax.transaction.Status JavaDoc;
24 import javax.transaction.Synchronization JavaDoc;
25 import javax.transaction.TransactionSynchronizationRegistry JavaDoc;
26
27 import org.apache.cayenne.ObjectContext;
28
29 /**
30  * An EntityManager that can participate in JTA transactions.
31  *
32  * @author Andrus Adamchik
33  */

34 public class JtaEntityManager extends ResourceLocalEntityManager {
35
36     protected Object JavaDoc currentTxKey;
37
38     public JtaEntityManager(ObjectContext context, JtaEntityManagerFactory factory) {
39         super(context, factory);
40     }
41
42     private JtaEntityManagerFactory getJtaFactory() {
43         return (JtaEntityManagerFactory) getFactory();
44     }
45
46     /**
47      * @throws IllegalStateException, as this entity manager is of JTA kind.
48      */

49     public EntityTransaction getTransaction() {
50         throw new IllegalStateException JavaDoc(
51                 "'getTransaction' is called on a JTA EntityManager");
52     }
53
54     /**
55      * Indicates to the EntityManager that a JTA transaction is active. This method should
56      * be called on a JTA application managed EntityManager that was created outside the
57      * scope of the active transaction to associate it with the current JTA transaction.
58      *
59      * @throws TransactionRequiredException if there is no transaction.
60      */

61     @Override JavaDoc
62     public void joinTransaction() {
63         if (currentTxKey == null) {
64             TransactionSynchronizationRegistry JavaDoc registry = getJtaFactory()
65                     .getTransactionRegistry();
66             registry.registerInterposedSynchronization(new TransactionBinding());
67             currentTxKey = registry.getTransactionKey();
68         }
69     }
70
71     /**
72      * @throws TransactionRequiredException if there is no transaction.
73      */

74     @Override JavaDoc
75     public void persist(Object JavaDoc entity) {
76         checkTransaction();
77         super.persist(entity);
78     }
79
80     /**
81      * @throws TransactionRequiredException if there is no transaction.
82      */

83     @Override JavaDoc
84     public <T> T merge(T entity) {
85         checkTransaction();
86         return super.merge(entity);
87     }
88
89     /**
90      * @throws TransactionRequiredException if there is no transaction.
91      */

92     @Override JavaDoc
93     public void remove(Object JavaDoc entity) {
94         checkTransaction();
95         super.remove(entity);
96     }
97
98     /**
99      * @throws TransactionRequiredException if there is no transaction.
100      */

101     @Override JavaDoc
102     public void refresh(Object JavaDoc entity) {
103         checkTransaction();
104         super.refresh(entity);
105     }
106
107     /**
108      * @throws TransactionRequiredException if there is no transaction.
109      */

110     public void flush() {
111         checkTransaction();
112         super.flush();
113     };
114
115     /**
116      * @throws TransactionRequiredException if there is no transaction in progress.
117      */

118     protected void checkTransaction() throws TransactionRequiredException {
119         if (!getJtaFactory().isActiveTransaction()) {
120             throw new TransactionRequiredException();
121         }
122     }
123
124     class TransactionBinding implements Synchronization JavaDoc {
125
126         public void afterCompletion(int status) {
127             if (status != Status.STATUS_COMMITTED) {
128                 clear();
129             }
130
131             currentTxKey = null;
132         }
133
134         public void beforeCompletion() {
135             flush();
136         }
137     }
138 }
139
Popular Tags