001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: DbTransactionUser.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database;
009:
010: import com.uwyn.rife.database.exceptions.RollbackException;
011: import com.uwyn.rife.tools.ExceptionUtils;
012: import com.uwyn.rife.tools.InnerClassException;
013: import java.util.logging.Logger;
014:
015: /**
016: * By extending this class it's possible to provide the logic that should be
017: * executed by the {@link DbQueryManager#inTransaction(DbTransactionUser) inTransaction}
018: * method in the {@link DbQueryManager} class.
019: * <p>This class has both a default constructor and one that can take a data
020: * object. This can be handy when using it as an extending anonymous inner
021: * class when you need to use variables inside the inner class that are
022: * cumbersome to change to <code>final</code> in the enclosing class.
023: *
024: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
025: * @version $Revision: 3634 $
026: * @see DbQueryManager#inTransaction(DbTransactionUser)
027: * @since 1.0
028: */
029: public abstract class DbTransactionUser<ResultType, DataType>
030: implements Cloneable {
031: protected DataType mData = null;
032:
033: public DbTransactionUser() {
034: }
035:
036: public DbTransactionUser(DataType data) {
037: mData = data;
038: }
039:
040: public DataType getData() {
041: return mData;
042: }
043:
044: /**
045: * Should be overridden if the transaction has to be executed in another
046: * isolation level.
047: *
048: * @return <code>-1</code> when the active isolation level should be
049: * preserved; or
050: * <p>a level constant from {@link java.sql.Connection Connection} if the
051: * isolation needs to be changed.
052: * @since 1.0
053: */
054: public int getTransactionIsolation() {
055: return -1;
056: }
057:
058: /**
059: * Should be used to roll back ongoing transactions, otherwise enclosing
060: * transaction users might not be interrupted and subsequent modification
061: * can still happen outside the transaction.
062: *
063: * @exception RollbackException indicates that a rollback should happen
064: * and all further transaction logic interrupted.
065: * @since 1.0
066: */
067: public void rollback() throws RollbackException {
068: throw new RollbackException();
069: }
070:
071: /**
072: * Calling this method makes it possible to throw a checked exception from
073: * within this class.
074: * <p>To catch it you should surround the {@link
075: * DbQueryManager#inTransaction(DbTransactionUser) inTransaction} with a
076: * <code>try-catch</code> block that catching
077: * <code>InnerClassException</code>. The original exception is then
078: * available through <code>getCause()</code> and can for example be
079: * rethrown.
080: *
081: * @exception InnerClassException when a checked exception needs to be
082: * thrown from within this class and caught outside the caller.
083: * @since 1.0
084: */
085: public void throwException(Exception exception)
086: throws InnerClassException {
087: throw new InnerClassException(exception);
088: }
089:
090: /**
091: * Should be implemented by all extending classes.
092: *
093: * @since 1.0
094: */
095: public abstract ResultType useTransaction()
096: throws InnerClassException;
097:
098: /**
099: * Simply clones the instance with the default clone method since this
100: * class contains no member variables.
101: *
102: * @since 1.0
103: */
104: public Object clone() {
105: try {
106: return super .clone();
107: } catch (CloneNotSupportedException e) {
108: // this should never happen
109: Logger.getLogger("com.uwyn.rife.database").severe(
110: ExceptionUtils.getExceptionStackTrace(e));
111: return null;
112: }
113: }
114: }
|