KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
22
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.persistence.EntityManager;
26 import javax.persistence.spi.PersistenceUnitInfo;
27 import javax.transaction.Status JavaDoc;
28 import javax.transaction.TransactionSynchronizationRegistry JavaDoc;
29
30 import org.apache.cayenne.access.DataDomain;
31
32 /**
33  * An EntityManagerFactory that registers all EntityManagers that it creates with an
34  * active JTA Transaction so that they could flush the object state to the database during
35  * commit.
36  *
37  * @author Andrus Adamchik
38  */

39 public class JtaEntityManagerFactory extends ResourceLocalEntityManagerFactory {
40
41     static final String JavaDoc TX_SYNC_REGISTRY_KEY = "java:comp/TransactionSynchronizationRegistry";
42
43     protected TransactionSynchronizationRegistry JavaDoc transactionRegistry;
44
45     /**
46      * Non-public constructor used for unit testing.
47      */

48     JtaEntityManagerFactory(PersistenceUnitInfo unitInfo) {
49         super(unitInfo);
50     }
51
52     public JtaEntityManagerFactory(Provider provider, DataDomain domain,
53             PersistenceUnitInfo unitInfo) {
54         super(provider, domain, unitInfo);
55     }
56
57     /**
58      * Returns JTA 11 TransactionSynchronizationRegistry, looking it up via JNDI on first
59      * access, and caching it for the following invocations.
60      */

61     protected TransactionSynchronizationRegistry JavaDoc getTransactionRegistry() {
62         if (transactionRegistry == null) {
63             InitialContext JavaDoc jndiContext;
64             try {
65                 jndiContext = new InitialContext JavaDoc();
66             }
67             catch (NamingException JavaDoc e) {
68                 throw new JpaProviderException("Error creating JNDI context", e);
69             }
70
71             try {
72                 transactionRegistry = (TransactionSynchronizationRegistry JavaDoc) jndiContext
73                         .lookup(TX_SYNC_REGISTRY_KEY);
74             }
75             catch (NamingException JavaDoc e) {
76                 throw new JpaProviderException("Failed to look up "
77                         + TX_SYNC_REGISTRY_KEY, e);
78             }
79         }
80
81         return transactionRegistry;
82     }
83
84     /**
85      * Returns whether there is a JTA transaction in progress.
86      */

87     protected boolean isActiveTransaction() {
88         int txStatus = getTransactionRegistry().getTransactionStatus();
89         return txStatus == Status.STATUS_ACTIVE
90                 || txStatus == Status.STATUS_MARKED_ROLLBACK;
91     }
92
93     @Override JavaDoc
94     public EntityManager createEntityManager(Map JavaDoc map) {
95         checkClosed();
96         CayenneEntityManager em = new JtaEntityManager(createObjectContext(), this);
97         em = new TypeCheckingEntityManager(em);
98
99         if (isActiveTransaction()) {
100             em.joinTransaction();
101         }
102
103         return em;
104     }
105 }
106
Popular Tags