001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: DBUtil.java,v 1.2 2006-06-15 13:44:07 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.sessionEnhydra.persistent;
025:
026: import java.sql.SQLException;
027:
028: import com.lutris.appserver.server.Enhydra;
029: import com.lutris.appserver.server.session.SessionException;
030: import com.lutris.appserver.server.sessionEnhydra.StandardSession;
031: import com.lutris.appserver.server.sessionEnhydra.StandardSessionManager;
032: import com.lutris.appserver.server.sql.DBQuery;
033: import com.lutris.appserver.server.sql.DBTransaction;
034: import com.lutris.appserver.server.sql.DatabaseManagerException;
035: import com.lutris.appserver.server.sql.Query;
036:
037: /**
038: * Utilitity class for performint database operations.
039: *
040: * @see StandardSession
041: * @see StandardSessionManager
042: * @version $Revision: 1.2 $
043: * @author Kyle Clark
044: */
045: class DBUtil {
046:
047: /**
048: * Saves a session to the database.
049: *
050: * @param session
051: * the session to save.
052: * @param dbName the database that should be accessed. May be
053: * null in which case the default database is used.
054: * @return the number of rows that were affected
055: * by this update.
056: * @exception SessionException
057: * if an error occurs.
058: */
059: static int dbUpdate(PersistentSession session, String dbName)
060: throws SessionException {
061: PersistentSessionDO sessionDO = new PersistentSessionDO(session);
062: try {
063: DBTransaction t = createTransaction(dbName);
064: try {
065: t.update(sessionDO);
066: t.commit();
067: } catch (Exception e) {
068: t.rollback();
069: throw e;
070: } finally {
071: t.release();
072: }
073: } catch (Exception e) {
074: throw new SessionException(e);
075: }
076: return sessionDO.getTransactionRowCount();
077: }
078:
079: /**
080: * Deletes a session from the database.
081: *
082: * @param sessionKey
083: * the key that identifies the session that will be deleted.
084: * @param dbName the database that should be accessed. May be
085: * null in which case the default database is used.
086: * @exception SessionException
087: * if an error occurs.
088: */
089: static void dbDelete(String sessionKey, String dbName)
090: throws SessionException {
091: try {
092: DBTransaction t = createTransaction(dbName);
093: SessionDeleteDO s = new SessionDeleteDO(sessionKey);
094: try {
095: t.delete(s);
096: t.commit();
097: } catch (Exception e) {
098: t.rollback();
099: throw e;
100: } finally {
101: t.release();
102: }
103: } catch (Exception e) {
104: throw new SessionException(e);
105: }
106: }
107:
108: /**
109: * Inserts a session into the database.
110: *
111: * @param session
112: * the session that will be inserted.
113: * @param dbName the database that should be accessed. May be
114: * null in which case the default database is used.
115: * @exception SessionException
116: * if an error occurs.
117: */
118: static void dbInsert(PersistentSession session, String dbName)
119: throws SessionException {
120: try {
121: PersistentSessionDO sessionDO = new PersistentSessionDO(
122: session);
123: DBTransaction t = createTransaction(dbName);
124: try {
125: t.insert(sessionDO);
126: t.commit();
127: } catch (Exception e) {
128: t.rollback();
129: throw e;
130: } finally {
131: t.release();
132: }
133: } catch (Exception 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 dbQuery(Query query, String dbName)
149: throws SessionException {
150: try {
151: DBQuery q = createQuery(dbName);
152: try {
153: q.query(query);
154: return q.next();
155: } catch (Exception e) {
156: throw e;
157: } finally {
158: q.release();
159: }
160: } catch (Exception e) {
161: throw new SessionException(e);
162: }
163: }
164:
165: /**
166: * Creates a transaction for the specified DB name.
167: *
168: * @param dbName the database name. May be null in
169: * which case the default database is used.
170: * @exception DatabaseManagerException if an error occurs.
171: * @exception SQLException if an error occurs.
172: */
173: static DBTransaction createTransaction(String dbName)
174: throws DatabaseManagerException, SQLException {
175: DBTransaction t;
176: if (dbName != null) {
177: t = Enhydra.getDatabaseManager().createTransaction(dbName);
178: } else {
179: t = Enhydra.getDatabaseManager().createTransaction();
180: }
181: return t;
182: }
183:
184: /**
185: * Creates a query for the specified DB name.
186: *
187: * @param dbName the database name. May be null in
188: * which case the default database is used.
189: * @exception DatabaseManagerException if an error occurs.
190: * @exception SQLException if an error occurs.
191: */
192: static DBQuery createQuery(String dbName)
193: throws DatabaseManagerException, SQLException {
194: DBQuery q;
195: if (dbName != null) {
196: q = Enhydra.getDatabaseManager().createQuery(dbName);
197: } else {
198: q = Enhydra.getDatabaseManager().createQuery();
199: }
200: return q;
201: }
202:
203: }
|