KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > DatabaseSession


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 package org.infoglue.cms.util.workflow;
24
25 import org.apache.log4j.Logger;
26 import org.exolab.castor.jdo.Database;
27 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
28
29 import com.opensymphony.workflow.WorkflowException;
30
31 /**
32  * A database session controls the lifecycle of a wrapped database object.
33  */

34 public class DatabaseSession
35 {
36     /**
37      * The class logger.
38      */

39     private final static Logger logger = Logger.getLogger(DatabaseSession.class.getName());
40
41     /**
42      * The wrapped database object.
43      */

44     private Database db;
45     
46     /**
47      * Indicates if the database should be rollback when released.
48      */

49     private boolean rollbackOnly;
50
51     /**
52      * Default constructor.
53      */

54     public DatabaseSession()
55     {
56         super();
57     }
58
59     /**
60      * Sets the rollback only flag to true.
61      */

62     public void setRollbackOnly()
63     {
64         rollbackOnly = true;
65     }
66     
67     /**
68      * Returns the wrapped database. The database will be opened and the transaction will be started
69      * when this function is first called.
70      *
71      * @return the wrapped database object.
72      * @throws WorkflowException if an error occurs during the initializing of the transaction/database.
73      */

74     public Database getDB() throws WorkflowException
75     {
76         if(db == null)
77         {
78             try
79             {
80                 logger.debug("Creating a new database");
81                 db = CastorDatabaseService.getDatabase();
82                 db.begin();
83             }
84             catch(Exception JavaDoc e)
85             {
86                 logger.error("Unable to create database", e);
87                 throw new WorkflowException(e);
88             }
89         }
90         return db;
91     }
92     
93     /**
94      * Releases the database. If the rollback only flag has been set, the transaction
95      * is rolled back. Otherwise the transaction is commited. Finally the database is closed.
96      *
97      * @throws WorkflowException if an error occurs while releasing the transaction/database.
98      */

99     public void releaseDB() throws WorkflowException
100     {
101         logger.debug("releaseDB : " + (rollbackOnly ? "rollback" : "commit"));
102         if(rollbackOnly)
103         {
104             rollback();
105         }
106         else
107         {
108             commit();
109         }
110     }
111     
112     /**
113      * Rolls back the transaction and closes the database.
114      *
115      * @throws WorkflowException if an error occurs during the rollback/close operation.
116      */

117     private void rollback() throws WorkflowException
118     {
119         logger.debug("rollback()");
120         try
121         {
122             doRollback();
123         }
124         finally
125         {
126             doClose();
127         }
128     }
129
130     /**
131      * Commits the transaction and closes the database.
132      *
133      * @throws WorkflowException if an error occurs during the commit/close operation.
134      */

135     private void commit() throws WorkflowException
136     {
137         logger.debug("commit()");
138         try
139         {
140             doCommit();
141         }
142         catch(Exception JavaDoc e)
143         {
144             doRollback();
145             throw new WorkflowException(e);
146         }
147         finally
148         {
149             doClose();
150         }
151     }
152     
153     /**
154      *
155      */

156     private void doCommit() throws WorkflowException
157     {
158         if(db != null && db.isActive())
159         {
160             try
161             {
162                 logger.debug("doCommit()");
163                 db.commit();
164             }
165             catch(Exception JavaDoc e)
166             {
167                 logger.error(e);
168                 throw new WorkflowException(e);
169             }
170         }
171     }
172
173     /**
174      *
175      */

176     private void doRollback() throws WorkflowException
177     {
178         if(db != null && db.isActive())
179         {
180             try
181             {
182                 logger.debug("doRollback()");
183                 db.rollback();
184             }
185             catch(Exception JavaDoc e)
186             {
187                 logger.error(e);
188                 throw new WorkflowException(e);
189             }
190         }
191     }
192
193     /**
194      *
195      */

196     private void doClose() throws WorkflowException
197     {
198         if(db != null && !db.isClosed())
199         {
200             try
201             {
202                 logger.debug("doClose()");
203                 db.close();
204             }
205             catch(Exception JavaDoc e)
206             {
207                 logger.error(e);
208                 throw new WorkflowException(e);
209             }
210         }
211     }
212 }
213
Popular Tags