KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionEnhydra > persistent > DBUtil


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: DBUtil.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra.persistent;
26
27 import java.sql.SQLException JavaDoc;
28
29 import com.lutris.appserver.server.Enhydra;
30 import com.lutris.appserver.server.session.SessionException;
31 import com.lutris.appserver.server.sessionEnhydra.StandardSession;
32 import com.lutris.appserver.server.sessionEnhydra.StandardSessionManager;
33 import com.lutris.appserver.server.sql.DBQuery;
34 import com.lutris.appserver.server.sql.DBTransaction;
35 import com.lutris.appserver.server.sql.DatabaseManagerException;
36 import com.lutris.appserver.server.sql.Query;
37
38 /**
39  * Utilitity class for performint database operations.
40  *
41  * @see StandardSession
42  * @see StandardSessionManager
43  * @version $Revision: 1.2 $
44  * @author Kyle Clark
45  */

46 class DBUtil {
47
48     /**
49      * Saves a session to the database.
50      *
51      * @param session
52      * the session to save.
53      * @param dbName the database that should be accessed. May be
54      * null in which case the default database is used.
55      * @return the number of rows that were affected
56      * by this update.
57      * @exception SessionException
58      * if an error occurs.
59      */

60     static int dbUpdate(PersistentSession session, String JavaDoc dbName)
61         throws SessionException {
62         PersistentSessionDO sessionDO = new PersistentSessionDO(session);
63         try {
64         DBTransaction t = createTransaction(dbName);
65             try {
66                 t.update(sessionDO);
67                 t.commit();
68             } catch (Exception JavaDoc e) {
69                 t.rollback();
70         throw e;
71             } finally {
72                 t.release();
73             }
74         } catch (Exception JavaDoc e) {
75             throw new SessionException(e);
76         }
77         return sessionDO.getTransactionRowCount();
78     }
79
80     /**
81      * Deletes a session from the database.
82      *
83      * @param sessionKey
84      * the key that identifies the session that will be deleted.
85      * @param dbName the database that should be accessed. May be
86      * null in which case the default database is used.
87      * @exception SessionException
88      * if an error occurs.
89      */

90     static void dbDelete(String JavaDoc sessionKey, String JavaDoc dbName)
91         throws SessionException {
92         try {
93         DBTransaction t = createTransaction(dbName);
94             SessionDeleteDO s = new SessionDeleteDO(sessionKey);
95             try {
96                 t.delete(s);
97                 t.commit();
98             } catch (Exception JavaDoc e) {
99                 t.rollback();
100         throw e;
101             } finally {
102                 t.release();
103             }
104         } catch (Exception JavaDoc e) {
105             throw new SessionException(e);
106         }
107     }
108
109     /**
110      * Inserts a session into the database.
111      *
112      * @param session
113      * the session that will be inserted.
114      * @param dbName the database that should be accessed. May be
115      * null in which case the default database is used.
116      * @exception SessionException
117      * if an error occurs.
118      */

119     static void dbInsert(PersistentSession session, String JavaDoc dbName)
120         throws SessionException {
121         try {
122             PersistentSessionDO sessionDO = new PersistentSessionDO(session);
123         DBTransaction t = createTransaction(dbName);
124             try {
125                 t.insert(sessionDO);
126                 t.commit();
127             } catch (Exception JavaDoc e) {
128                 t.rollback();
129         throw e;
130             } finally {
131                 t.release();
132             }
133         } catch (Exception JavaDoc e) {
134             throw new SessionException(e);
135         }
136     }
137
138     /**
139      * Executes a query of the database.
140      *
141      * @param query
142      * the object containg the query statement.
143      * @param dbName the database that should be queried. May be
144      * null in which case the default database is used.
145      * @exception SessionException
146      * if an erro occurs.
147      */

148     static Object JavaDoc dbQuery(Query query, String JavaDoc dbName) throws SessionException {
149         try {
150             DBQuery q = createQuery(dbName);
151             try {
152                 q.query(query);
153                 return q.next();
154             } catch (Exception JavaDoc e) {
155                 throw e;
156             } finally {
157                 q.release();
158             }
159         } catch (Exception JavaDoc e) {
160             throw new SessionException(e);
161         }
162     }
163
164     /**
165      * Creates a transaction for the specified DB name.
166      *
167      * @param dbName the database name. May be null in
168      * which case the default database is used.
169      * @exception DatabaseManagerException if an error occurs.
170      * @exception SQLException if an error occurs.
171      */

172     static DBTransaction createTransaction(String JavaDoc dbName)
173         throws DatabaseManagerException, SQLException JavaDoc {
174         DBTransaction t;
175         if (dbName != null) {
176             t = Enhydra.getDatabaseManager().createTransaction(dbName);
177         } else {
178             t = Enhydra.getDatabaseManager().createTransaction();
179         }
180         return t;
181     }
182
183     /**
184      * Creates a query for the specified DB name.
185      *
186      * @param dbName the database name. May be null in
187      * which case the default database is used.
188      * @exception DatabaseManagerException if an error occurs.
189      * @exception SQLException if an error occurs.
190      */

191     static DBQuery createQuery(String JavaDoc dbName)
192         throws DatabaseManagerException, SQLException JavaDoc {
193         DBQuery q;
194         if (dbName != null) {
195             q = Enhydra.getDatabaseManager().createQuery(dbName);
196         } else {
197             q = Enhydra.getDatabaseManager().createQuery();
198         }
199         return q;
200     }
201
202 }
203
204
205
206
207
Popular Tags