KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
22
23 import javax.persistence.EntityManager;
24 import javax.persistence.EntityTransaction;
25 import javax.persistence.PersistenceException;
26
27 import org.apache.cayenne.CayenneException;
28 import org.apache.cayenne.access.Transaction;
29
30 /**
31  * A JPA wrapper around a Cayenne Transaction. For more info see <a
32  * HREF="http://cayenne.apache.org/doc/understanding-transactions.html">this page</a>.
33  */

34 public class JpaTransaction implements EntityTransaction {
35
36     protected EntityManager entityManager;
37     protected Transaction transaction;
38     protected boolean rollbackOnly;
39
40     public JpaTransaction(Transaction transaction, EntityManager entityManager) {
41         this.entityManager = entityManager;
42         this.transaction = transaction;
43     }
44
45     /**
46      * Start a resource transaction.
47      *
48      * @throws IllegalStateException if isActive() is true.
49      */

50     public void begin() {
51         if (isActive()) {
52             throw new IllegalStateException JavaDoc("transaction active");
53         }
54
55         transaction.begin();
56     }
57
58     /**
59      * Commit the current transaction, writing any unflushed changes to the database.
60      *
61      * @throws IllegalStateException if isActive() is false.
62      * @throws PersistenceException if the commit fails.
63      */

64     public void commit() {
65         if (!isActive()) {
66             throw new IllegalStateException JavaDoc("transaction not active");
67         }
68
69         try {
70             entityManager.flush();
71             transaction.commit();
72         }
73         catch (SQLException JavaDoc e) {
74             throw new PersistenceException(e.getMessage(), e);
75         }
76         catch (CayenneException e) {
77             throw new PersistenceException(e.getMessage(), e);
78         }
79     }
80
81     /**
82      * Roll back the current transaction.
83      *
84      * @throws IllegalStateException if isActive() is false.
85      * @throws PersistenceException if an unexpected error condition is encountered.
86      */

87     public void rollback() {
88         if (!isActive()) {
89             throw new IllegalStateException JavaDoc("transaction not active");
90         }
91
92         try {
93             transaction.rollback();
94         }
95         catch (SQLException JavaDoc e) {
96             throw new PersistenceException(e.getMessage(), e);
97         }
98         catch (CayenneException e) {
99             throw new PersistenceException(e.getMessage(), e);
100         }
101     }
102
103     /**
104      * Indicate whether a transaction is in progress.
105      *
106      * @throws PersistenceException if an unexpected error condition is encountered.
107      */

108     public boolean isActive() {
109         return (transaction.getStatus() == Transaction.STATUS_ACTIVE);
110     }
111
112     public boolean getRollbackOnly() {
113         return rollbackOnly;
114     }
115
116     public void setRollbackOnly() {
117         rollbackOnly = true;
118     }
119 }
120
Popular Tags