KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > instantdb > IdbXAConnection


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22 package org.enhydra.jdbc.instantdb;
23
24 import org.enhydra.jdbc.standard.StandardXAConnection;
25 import org.enhydra.jdbc.standard.StandardXADataSource;
26 import org.enhydra.jdbc.standard.StandardXAStatefulConnection;
27 import org.enhydra.instantdb.jdbc.ConnectionExtensions;
28
29 import javax.transaction.xa.XAException JavaDoc;
30 import javax.transaction.xa.Xid JavaDoc;
31 import javax.transaction.xa.XAResource JavaDoc;
32 import java.sql.SQLException JavaDoc;
33
34
35 /**
36  * Provides and InstantDB specific instance of StandardXAConnection. Almost all of
37  * the required functionality is provided curtesy of the generic super class
38  * which looks after most of the transaction state management. InstantDB's
39  * own Transaction object is informed that it is part of a global transaction
40  * and looks after the detail thereafter.
41  */

42 public final class IdbXAConnection extends StandardXAConnection {
43
44     /**
45      * Creates the first free connection.
46      */

47     public IdbXAConnection (StandardXADataSource dataSource, String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
48         super (dataSource, user, password); // creates the first Connection object
49
}
50
51     /**
52      * Associates this XAConnection with a global transaction. This
53      * is the only method which can associate the current connection
54      * with a global transaction. It acts only on the current
55      * connection which must have been previously established using
56      * getConnection.
57      */

58     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
59         doStart (xid, flags); // do state checks and set state
60
curCon.commitOnPrepare = false; // we will do a REAL prepare
61
ConnectionExtensions conExt
62                 = (ConnectionExtensions)curCon.con; // get the InstantDB connection
63
conExt.startGlobalTransaction (xid); // associate the transaction with the global TX
64
curCon = null; // no longer owned by this object
65
con = null; // ditto
66
}
67
68     // We don't override "end" as all it does is change the state of a connection.
69

70     /**
71      * Prepares to perform a commit.
72      */

73     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
74         StandardXAStatefulConnection stateCon = checkPreparedState (xid);// do generic state checking etc.
75
ConnectionExtensions con
76                 = (ConnectionExtensions)stateCon.con; // get the InstantDB connection
77
int status = con.prepare(); // prepare to commit
78
if (status == XA_RDONLY) { // if transaction didn't update the database
79
xaDataSource.freeConnection (xid, false); // free the connection
80
} // if
81
return status;
82     }
83
84     // We don't override commit or rollback. Connection.commit and
85
// Connection.rollback already know that they're part of a global
86
// transaction and will behave accordingly.
87

88     /**
89      * Checks to see if two XAResource objects correspond to the
90      * same Resource Manager. This can go one better than its
91      * super class as it can actually check the InstantDB database
92      * objects.
93      */

94     public boolean isSameRM(XAResource JavaDoc xares) throws XAException JavaDoc {
95         if (super.isSameRM(xares)) { // if super class can figure it out
96
return true; // then accept its conclusion
97
} // if
98
if (xares instanceof IdbXAConnection) { // if it's one of our wrappers
99
IdbXAConnection xac = (IdbXAConnection)xares; // cast to something more convenient
100
IdbXADataSource cmpds = (IdbXADataSource)xac.dataSource;// get the data source to compare
101
IdbXADataSource ds = (IdbXADataSource)dataSource;// get our own data source
102
if (ds.databaseId.equals (cmpds.databaseId)) { // if using the same database
103
return true; // they're the same resource
104
} // if
105
} // if
106
return false;
107     }
108
109 }
110
Popular Tags