KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > engine > transaction > toplink > ToplinkDaoTransaction


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.dao.engine.transaction.toplink;
17
18 import oracle.toplink.exceptions.TopLinkException;
19 import oracle.toplink.sessions.Session;
20 import oracle.toplink.sessions.UnitOfWork;
21 import oracle.toplink.threetier.Server;
22
23 import com.ibatis.dao.client.DaoException;
24 import com.ibatis.dao.client.DaoTransaction;
25
26 /**
27  * The <code>ToplinkDaoTransaction</code> class represents an abstract
28  * DaoTransaction implemented using Toplink. Toplink provides transaction
29  * management methods to commit and rollback transactions.
30  *
31  * @author Wayne Gentile
32  * @version $Revision: 152595 $ $Date: 2004-12-28 09:46:14 -0700 (Tue, 28 Dec 2004) $
33  */

34 public class ToplinkDaoTransaction implements DaoTransaction {
35
36     //~ Instance fields
37
// --------------------------------------------------------
38

39     private Server server;
40
41     private Session session;
42
43     private UnitOfWork unitOfWork;
44
45     private boolean commmitted = false;
46
47     //~ Constructors
48
// -----------------------------------------------------------
49

50     /**
51      * Constructor for the <code>ToplinkDaoTransaction</code> class that
52      * accepts a Toplink Server object for creating client sessions.
53      *
54      * @param uow
55      * The Toplink UnitOfWork associated with the transaction
56      * @param server
57      * The Toplink server used to acquire client sessions
58      *
59      * @throws DaoException
60      * If a DaoException is thrown
61      */

62     public ToplinkDaoTransaction(UnitOfWork uow, Server server)
63             throws DaoException {
64
65         // Check arguments
66
if (server == null) {
67
68             throw new DaoException("Toplink Server not available");
69
70         }
71
72         // Set the server settings
73
this.unitOfWork = uow;
74         this.server = server;
75
76     }
77
78     //~ Methods
79
// ----------------------------------------------------------------
80

81     /**
82      * Gets the Toplink Session associated with this transaction.
83      *
84      * @return The Toplink Session
85      *
86      * @throws DaoException
87      * If a data access exception occurs
88      */

89     public Session getSession() throws DaoException {
90
91         // Get a session
92
try {
93
94             if (session == null) {
95
96                 session = server.acquireClientSession();
97
98             }
99
100             return session;
101
102         } catch (TopLinkException e) {
103
104             throw new DaoException("Error aquiring Session", e);
105
106         }
107
108     }
109
110     /**
111      * Gets the active unit of work.
112      *
113      * @return The unitOfWork instance
114      *
115      * @throws DaoException
116      * If a data access exception occurs
117      */

118     public UnitOfWork getUnitOfWork() throws DaoException {
119
120         try {
121
122             if (unitOfWork == null) {
123
124                 unitOfWork = getSession().acquireUnitOfWork();
125
126             }
127
128             return unitOfWork;
129
130         } catch (TopLinkException e) {
131
132             throw new DaoException("Error acquiring UnitOfWork.", e);
133
134         }
135
136     }
137
138     /**
139      * Commits all pending changes to persistent storage.
140      *
141      * @throws DaoException
142      * If a data access exception occurs
143      */

144     public void commit() throws DaoException {
145
146         // Make sure the transaction has not been committed
147
if (commmitted) {
148
149             throw new DaoException("Transaction already committed");
150
151         }
152
153         // Now try to commit the transaction
154
try {
155
156             // Check for UnitOfWork
157
if (unitOfWork != null) {
158
159                 // UnitOfWork was not lazily loaded
160
unitOfWork.commit();
161                 unitOfWork.release();
162                 session.release();
163
164             }
165
166         } catch (TopLinkException e) {
167
168             throw new DaoException("Error committing transaction", e);
169
170         }
171         commmitted = true;
172
173     }
174
175     /**
176      * Rollback all pending changes to persistent storage and revert changes
177      * back to the original state.
178      *
179      * @throws DaoException
180      * If a data access exception occurs
181      */

182     public void rollback() throws DaoException {
183
184         // Commit the transaction if it has not been committed
185
if (!commmitted) {
186
187             try {
188
189                 // Make sure the UOW is still active before rollback
190
if ((unitOfWork != null) && unitOfWork.isActive()) {
191
192                     unitOfWork.revertAndResume();
193                     unitOfWork.release();
194                     session.release();
195
196                 }
197
198             } catch (TopLinkException e) {
199
200                 throw new DaoException("Error rolling back transaction", e);
201
202             }
203
204         }
205
206     }
207
208     /**
209      * Clean up outstanding units of work and sessions.
210      *
211      * @throws Throwable
212      * If any exception occurs
213      */

214     protected void finalize() throws Throwable JavaDoc {
215
216         super.finalize();
217
218         // Commit outstanding transactions
219
if (unitOfWork.isActive()) {
220
221             commit();
222
223         }
224
225         // Make sure everything is cleaned up
226
if ((session != null) && (session.isConnected())) {
227
228             session.release();
229
230         }
231
232     }
233
234 }
Popular Tags