KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > ClientXAConnection


1 /*
2
3    Derby - Class org.apache.derby.client.ClientXAConnection
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21 package org.apache.derby.client;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import javax.sql.XAConnection JavaDoc;
26 import javax.transaction.xa.XAResource JavaDoc;
27
28 import org.apache.derby.client.am.SqlException;
29 import org.apache.derby.client.net.NetLogWriter;
30 import org.apache.derby.client.net.NetXAConnection;
31 import org.apache.derby.jdbc.ClientXADataSource;
32
33 public class ClientXAConnection extends ClientPooledConnection implements XAConnection JavaDoc {
34     private static int rmIdSeed_ = 95688932; // semi-random starting value for rmId
35

36     private ClientXADataSource derbyds_ = null;
37     private XAResource JavaDoc xares_ = null;
38     private org.apache.derby.client.net.NetXAResource netXares_ = null;
39     private boolean fFirstGetConnection_ = true;
40     private Connection JavaDoc logicalCon_; // logicalConnection_ is inherited from ClientPooledConnection
41
// This connection is used to access the indoubt table
42
private NetXAConnection controlCon_ = null;
43
44     public ClientXAConnection(ClientXADataSource ds,
45                               org.apache.derby.client.net.NetLogWriter logWtr,
46                               String JavaDoc userId,
47                               String JavaDoc password) throws SQLException JavaDoc {
48         super(ds, logWtr, userId, password, getUnigueRmId());
49         derbyds_ = ds;
50
51         // Have to instantiate a real connection here,
52
// otherwise if XA function is called before the connect happens,
53
// an error will be returned
54
// Note: conApp will be set after this call
55
logicalCon_ = super.getConnection();
56
57         netXares_ = new org.apache.derby.client.net.NetXAResource(this,
58                 rmId_, userId, password, netXAPhysicalConnection_);
59         xares_ = netXares_;
60     }
61
62     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
63         if (fFirstGetConnection_) {
64             // Since super.getConnection() has already been called once
65
// in the constructor, we don't need to call it again for the
66
// call of this method.
67
fFirstGetConnection_ = false;
68         } else {
69             // A new connection object is required
70
logicalCon_ = super.getConnection();
71             if (this.physicalConnection_ != null) { // have a physical connection, check if a NetXAResource
72
if (netXAPhysicalConnection_ != null) { // the XAResource is a NetXAResource, re-initialize it
73
netXares_.initForReuse();
74                 }
75             }
76         }
77         return logicalCon_;
78     }
79
80     private static synchronized int getUnigueRmId() {
81         rmIdSeed_ += 1;
82         return rmIdSeed_;
83     }
84
85     public int getRmId() {
86         return rmId_;
87     }
88
89     public XAResource JavaDoc getXAResource() throws SQLException JavaDoc {
90         if (logWriter_ != null) {
91             logWriter_.traceExit(this, "getXAResource", xares_);
92         }
93
94         return xares_;
95     }
96
97     public ClientXADataSource getDataSource() throws SqlException {
98         if (logWriter_ != null) {
99             logWriter_.traceExit(this, "getDataSource", derbyds_);
100         }
101
102         return derbyds_;
103     }
104
105     public NetXAConnection createControlConnection(NetLogWriter logWriter,
106                                                    String JavaDoc user,
107                                                    String JavaDoc password,
108                                                    org.apache.derby.jdbc.ClientDataSource dataSource,
109                                                    int rmId,
110                                                    boolean isXAConn) throws SQLException JavaDoc {
111         try
112         {
113             controlCon_ = new NetXAConnection(logWriter,
114                     user,
115                     password,
116                     dataSource,
117                     rmId,
118                     isXAConn,
119                     this);
120             controlCon_.getNetConnection().setTransactionIsolation(
121                     Connection.TRANSACTION_READ_UNCOMMITTED);
122
123             if (logWriter_ != null) {
124                 logWriter_.traceExit(this, "createControlConnection", controlCon_);
125             }
126
127             return controlCon_;
128         }
129         catch ( SqlException se )
130         {
131             throw se.getSQLException();
132         }
133     }
134
135
136     public synchronized void close() throws SQLException JavaDoc {
137         super.close();
138     }
139 }
140
141
Popular Tags