1 | /* |
2 | |
3 | Derby - Class org.apache.derby.client.ClientXAConnection |
4 | |
5 | Copyright (c) 2003, 2005 The Apache Software Foundation or its licensors, where applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | package org.apache.derby.client; |
21 | |
22 | import java.sql.Connection; |
23 | import java.sql.SQLException; |
24 | import javax.sql.XAConnection; |
25 | import javax.transaction.xa.XAResource; |
26 | |
27 | import org.apache.derby.client.am.SqlException; |
28 | import org.apache.derby.client.net.NetLogWriter; |
29 | import org.apache.derby.client.net.NetXAConnection; |
30 | import org.apache.derby.jdbc.ClientXADataSource; |
31 | |
32 | public class ClientXAConnection extends ClientPooledConnection implements XAConnection { |
33 | private static int rmIdSeed_ = 95688932; // semi-random starting value for rmId |
34 | |
35 | private ClientXADataSource derbyds_ = null; |
36 | private XAResource xares_ = null; |
37 | private org.apache.derby.client.net.NetXAResource netXares_ = null; |
38 | private boolean fFirstGetConnection_ = true; |
39 | private Connection logicalCon_; // logicalConnection_ is inherited from ClientPooledConnection |
40 | // This connection is used to access the indoubt table |
41 | private NetXAConnection controlCon_ = null; |
42 | |
43 | public ClientXAConnection(ClientXADataSource ds, |
44 | org.apache.derby.client.net.NetLogWriter logWtr, |
45 | String userId, |
46 | String password) throws SQLException { |
47 | super(ds, logWtr, userId, password, getUnigueRmId()); |
48 | derbyds_ = ds; |
49 | |
50 | // Have to instantiate a real connection here, |
51 | // otherwise if XA function is called before the connect happens, |
52 | // an error will be returned |
53 | // Note: conApp will be set after this call |
54 | logicalCon_ = super.getConnection(); |
55 | |
56 | netXares_ = new org.apache.derby.client.net.NetXAResource(this, |
57 | rmId_, userId, password, netXAPhysicalConnection_); |
58 | xares_ = netXares_; |
59 | } |
60 | |
61 | public Connection getConnection() throws SQLException { |
62 | if (fFirstGetConnection_) { |
63 | // Since super.getConnection() has already been called once |
64 | // in the constructor, we don't need to call it again for the |
65 | // call of this method. |
66 | fFirstGetConnection_ = false; |
67 | } else { |
68 | // A new connection object is required |
69 | logicalCon_ = super.getConnection(); |
70 | if (this.physicalConnection_ != null) { // have a physical connection, check if a NetXAResource |
71 | if (netXAPhysicalConnection_ != null) { // the XAResource is a NetXAResource, re-initialize it |
72 | netXares_.initForReuse(); |
73 | } |
74 | } |
75 | } |
76 | return logicalCon_; |
77 | } |
78 | |
79 | private static synchronized int getUnigueRmId() { |
80 | rmIdSeed_ += 1; |
81 | return rmIdSeed_; |
82 | } |
83 | |
84 | public int getRmId() { |
85 | return rmId_; |
86 | } |
87 | |
88 | public XAResource getXAResource() throws SQLException { |
89 | if (logWriter_ != null) { |
90 | logWriter_.traceExit(this, "getXAResource", xares_); |
91 | } |
92 | |
93 | return xares_; |
94 | } |
95 | |
96 | public ClientXADataSource getDataSource() throws SqlException { |
97 | if (logWriter_ != null) { |
98 | logWriter_.traceExit(this, "getDataSource", derbyds_); |
99 | } |
100 | |
101 | return derbyds_; |
102 | } |
103 | |
104 | public NetXAConnection createControlConnection(NetLogWriter logWriter, |
105 | String user, |
106 | String password, |
107 | org.apache.derby.jdbc.ClientDataSource dataSource, |
108 | int rmId, |
109 | boolean isXAConn) throws SQLException { |
110 | try |
111 | { |
112 | controlCon_ = new NetXAConnection(logWriter, |
113 | user, |
114 | password, |
115 | dataSource, |
116 | rmId, |
117 | isXAConn, |
118 | this); |
119 | controlCon_.getNetConnection().setTransactionIsolation( |
120 | Connection.TRANSACTION_READ_UNCOMMITTED); |
121 | |
122 | if (logWriter_ != null) { |
123 | logWriter_.traceExit(this, "createControlConnection", controlCon_); |
124 | } |
125 | |
126 | return controlCon_; |
127 | } |
128 | catch ( SqlException se ) |
129 | { |
130 | throw se.getSQLException(); |
131 | } |
132 | } |
133 | |
134 | |
135 | public synchronized void close() throws SQLException { |
136 | super.close(); |
137 | } |
138 | } |
139 | |