001: /*
002:
003: Derby - Class org.apache.derby.client.ClientXAConnection
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: package org.apache.derby.client;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025: import javax.sql.XAConnection;
026: import javax.transaction.xa.XAResource;
027:
028: import org.apache.derby.client.am.SqlException;
029: import org.apache.derby.client.net.NetLogWriter;
030: import org.apache.derby.client.net.NetXAConnection;
031: import org.apache.derby.jdbc.ClientXADataSource;
032:
033: public class ClientXAConnection extends ClientPooledConnection
034: implements XAConnection {
035: private static int rmIdSeed_ = 95688932; // semi-random starting value for rmId
036:
037: private ClientXADataSource derbyds_ = null;
038: private XAResource xares_ = null;
039: private org.apache.derby.client.net.NetXAResource netXares_ = null;
040: private boolean fFirstGetConnection_ = true;
041: private Connection logicalCon_; // logicalConnection_ is inherited from ClientPooledConnection
042: // This connection is used to access the indoubt table
043: private NetXAConnection controlCon_ = null;
044:
045: public ClientXAConnection(ClientXADataSource ds,
046: org.apache.derby.client.net.NetLogWriter logWtr,
047: String userId, String password) throws SQLException {
048: super (ds, logWtr, userId, password, getUnigueRmId());
049: derbyds_ = ds;
050:
051: // Have to instantiate a real connection here,
052: // otherwise if XA function is called before the connect happens,
053: // an error will be returned
054: // Note: conApp will be set after this call
055: logicalCon_ = super .getConnection();
056:
057: netXares_ = new org.apache.derby.client.net.NetXAResource(this ,
058: rmId_, userId, password, netXAPhysicalConnection_);
059: xares_ = netXares_;
060: }
061:
062: public Connection getConnection() throws SQLException {
063: if (fFirstGetConnection_) {
064: // Since super.getConnection() has already been called once
065: // in the constructor, we don't need to call it again for the
066: // call of this method.
067: fFirstGetConnection_ = false;
068: } else {
069: // A new connection object is required
070: logicalCon_ = super .getConnection();
071: if (this .physicalConnection_ != null) { // have a physical connection, check if a NetXAResource
072: if (netXAPhysicalConnection_ != null) { // the XAResource is a NetXAResource, re-initialize it
073: netXares_.initForReuse();
074: }
075: }
076: }
077: return logicalCon_;
078: }
079:
080: private static synchronized int getUnigueRmId() {
081: rmIdSeed_ += 1;
082: return rmIdSeed_;
083: }
084:
085: public int getRmId() {
086: return rmId_;
087: }
088:
089: public XAResource getXAResource() throws SQLException {
090: if (logWriter_ != null) {
091: logWriter_.traceExit(this , "getXAResource", xares_);
092: }
093:
094: return xares_;
095: }
096:
097: public ClientXADataSource getDataSource() throws SqlException {
098: if (logWriter_ != null) {
099: logWriter_.traceExit(this , "getDataSource", derbyds_);
100: }
101:
102: return derbyds_;
103: }
104:
105: public NetXAConnection createControlConnection(
106: NetLogWriter logWriter, String user, String password,
107: org.apache.derby.jdbc.ClientDataSource dataSource,
108: int rmId, boolean isXAConn) throws SQLException {
109: try {
110: controlCon_ = new NetXAConnection(logWriter, user,
111: password, dataSource, rmId, isXAConn, this );
112: controlCon_.getNetConnection().setTransactionIsolation(
113: Connection.TRANSACTION_READ_UNCOMMITTED);
114:
115: if (logWriter_ != null) {
116: logWriter_.traceExit(this , "createControlConnection",
117: controlCon_);
118: }
119:
120: return controlCon_;
121: } catch (SqlException se) {
122: throw se.getSQLException();
123: }
124: }
125:
126: public synchronized void close() throws SQLException {
127: super.close();
128: }
129: }
|