001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: */
022: package org.enhydra.jdbc.instantdb;
023:
024: import org.enhydra.jdbc.standard.StandardXAConnection;
025: import org.enhydra.jdbc.standard.StandardXADataSource;
026: import org.enhydra.jdbc.standard.StandardXAStatefulConnection;
027: import org.enhydra.instantdb.jdbc.ConnectionExtensions;
028:
029: import javax.transaction.xa.XAException;
030: import javax.transaction.xa.Xid;
031: import javax.transaction.xa.XAResource;
032: import java.sql.SQLException;
033:
034: /**
035: * Provides and InstantDB specific instance of StandardXAConnection. Almost all of
036: * the required functionality is provided curtesy of the generic super class
037: * which looks after most of the transaction state management. InstantDB's
038: * own Transaction object is informed that it is part of a global transaction
039: * and looks after the detail thereafter.
040: */
041: public final class IdbXAConnection extends StandardXAConnection {
042:
043: /**
044: * Creates the first free connection.
045: */
046: public IdbXAConnection(StandardXADataSource dataSource,
047: String user, String password) throws SQLException {
048: super (dataSource, user, password); // creates the first Connection object
049: }
050:
051: /**
052: * Associates this XAConnection with a global transaction. This
053: * is the only method which can associate the current connection
054: * with a global transaction. It acts only on the current
055: * connection which must have been previously established using
056: * getConnection.
057: */
058: public void start(Xid xid, int flags) throws XAException {
059: doStart(xid, flags); // do state checks and set state
060: curCon.commitOnPrepare = false; // we will do a REAL prepare
061: ConnectionExtensions conExt = (ConnectionExtensions) curCon.con; // get the InstantDB connection
062: conExt.startGlobalTransaction(xid); // associate the transaction with the global TX
063: curCon = null; // no longer owned by this object
064: con = null; // ditto
065: }
066:
067: // We don't override "end" as all it does is change the state of a connection.
068:
069: /**
070: * Prepares to perform a commit.
071: */
072: public int prepare(Xid xid) throws XAException {
073: StandardXAStatefulConnection stateCon = checkPreparedState(xid);// do generic state checking etc.
074: ConnectionExtensions con = (ConnectionExtensions) stateCon.con; // get the InstantDB connection
075: int status = con.prepare(); // prepare to commit
076: if (status == XA_RDONLY) { // if transaction didn't update the database
077: xaDataSource.freeConnection(xid, false); // free the connection
078: } // if
079: return status;
080: }
081:
082: // We don't override commit or rollback. Connection.commit and
083: // Connection.rollback already know that they're part of a global
084: // transaction and will behave accordingly.
085:
086: /**
087: * Checks to see if two XAResource objects correspond to the
088: * same Resource Manager. This can go one better than its
089: * super class as it can actually check the InstantDB database
090: * objects.
091: */
092: public boolean isSameRM(XAResource xares) throws XAException {
093: if (super .isSameRM(xares)) { // if super class can figure it out
094: return true; // then accept its conclusion
095: } // if
096: if (xares instanceof IdbXAConnection) { // if it's one of our wrappers
097: IdbXAConnection xac = (IdbXAConnection) xares; // cast to something more convenient
098: IdbXADataSource cmpds = (IdbXADataSource) xac.dataSource;// get the data source to compare
099: IdbXADataSource ds = (IdbXADataSource) dataSource;// get our own data source
100: if (ds.databaseId.equals(cmpds.databaseId)) { // if using the same database
101: return true; // they're the same resource
102: } // if
103: } // if
104: return false;
105: }
106:
107: }
|