KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > EmbedXAConnection


1 /*
2
3    Derby - Class org.apache.derby.jdbc.EmbedXAConnection
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
22 package org.apache.derby.jdbc;
23
24 import org.apache.derby.impl.jdbc.Util;
25 import org.apache.derby.iapi.jdbc.EngineConnection;
26 import org.apache.derby.iapi.jdbc.ResourceAdapter;
27
28 import org.apache.derby.iapi.reference.SQLState;
29 import org.apache.derby.iapi.reference.JDBC30Translation;
30
31 import java.sql.Connection JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.CallableStatement JavaDoc;
36 import javax.transaction.xa.XAResource JavaDoc;
37
38 /** -- jdbc 2.0. extension -- */
39 import javax.sql.XAConnection JavaDoc;
40
41 /**
42  */

43 class EmbedXAConnection extends EmbedPooledConnection
44         implements XAConnection JavaDoc
45
46 {
47
48         private EmbedXAResource xaRes;
49
50     EmbedXAConnection(EmbeddedDataSource ds, ResourceAdapter ra, String JavaDoc u, String JavaDoc p, boolean requestPassword) throws SQLException JavaDoc
51     {
52         super(ds, u, p, requestPassword);
53                 xaRes = new EmbedXAResource (this, ra);
54     }
55
56     /*
57     ** XAConnection methods
58     */

59
60     public final synchronized XAResource JavaDoc getXAResource() throws SQLException JavaDoc {
61         checkActive();
62         return xaRes;
63     }
64
65     /*
66     ** BrokeredConnectionControl api
67     */

68     /**
69         Allow control over setting auto commit mode.
70     */

71     public void checkAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
72         if (autoCommit && (xaRes.getCurrentXid () != null))
73             throw Util.generateCsSQLException(SQLState.CANNOT_AUTOCOMMIT_XA);
74
75         super.checkAutoCommit(autoCommit);
76     }
77     /**
78         Are held cursors allowed. If the connection is attached to
79         a global transaction then downgrade the result set holdabilty
80         to CLOSE_CURSORS_AT_COMMIT if downgrade is true, otherwise
81         throw an exception.
82         If the connection is in a local transaction then the
83         passed in holdabilty is returned.
84     */

85     public int checkHoldCursors(int holdability, boolean downgrade)
86         throws SQLException JavaDoc
87     {
88         if (holdability == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) {
89             if (xaRes.getCurrentXid () != null) {
90                 if (!downgrade)
91                     throw Util.generateCsSQLException(SQLState.CANNOT_HOLD_CURSOR_XA);
92                 
93                 holdability = JDBC30Translation.CLOSE_CURSORS_AT_COMMIT;
94             }
95         }
96
97         return super.checkHoldCursors(holdability, downgrade);
98     }
99
100     /**
101         Allow control over creating a Savepoint (JDBC 3.0)
102     */

103     public void checkSavepoint() throws SQLException JavaDoc {
104
105         if (xaRes.getCurrentXid () != null)
106             throw Util.generateCsSQLException(SQLState.CANNOT_ROLLBACK_XA);
107
108         super.checkSavepoint();
109     }
110
111     /**
112         Allow control over calling rollback.
113     */

114     public void checkRollback() throws SQLException JavaDoc {
115
116         if (xaRes.getCurrentXid () != null)
117             throw Util.generateCsSQLException(SQLState.CANNOT_ROLLBACK_XA);
118
119         super.checkRollback();
120     }
121     /**
122         Allow control over calling commit.
123     */

124     public void checkCommit() throws SQLException JavaDoc {
125
126         if (xaRes.getCurrentXid () != null)
127             throw Util.generateCsSQLException(SQLState.CANNOT_COMMIT_XA);
128
129         super.checkCommit();
130     }
131
132     public Connection getConnection() throws SQLException JavaDoc
133     {
134         Connection handle;
135
136         // Is this just a local transaction?
137
if (xaRes.getCurrentXid () == null) {
138             handle = super.getConnection();
139         } else {
140
141             if (currentConnectionHandle != null) {
142                 // this can only happen if someone called start(Xid),
143
// getConnection, getConnection (and we are now the 2nd
144
// getConnection call).
145
// Cannot yank a global connection away like, I don't think...
146
throw Util.generateCsSQLException(
147                                SQLState.CANNOT_CLOSE_ACTIVE_XA_CONNECTION);
148             }
149
150             handle = getNewCurrentConnectionHandle();
151         }
152
153         currentConnectionHandle.syncState();
154
155         return handle;
156     }
157
158     /**
159         Wrap and control a Statement
160     */

161     public Statement JavaDoc wrapStatement(Statement JavaDoc s) throws SQLException JavaDoc {
162         XAStatementControl sc = new XAStatementControl(this, s);
163         return sc.applicationStatement;
164     }
165     /**
166         Wrap and control a PreparedStatement
167     */

168     public PreparedStatement JavaDoc wrapStatement(PreparedStatement JavaDoc ps, String JavaDoc sql, Object JavaDoc generatedKeys) throws SQLException JavaDoc {
169                 ps = super.wrapStatement(ps,sql,generatedKeys);
170         XAStatementControl sc = new XAStatementControl(this, ps, sql, generatedKeys);
171         return (PreparedStatement JavaDoc) sc.applicationStatement;
172     }
173     /**
174         Wrap and control a PreparedStatement
175     */

176     public CallableStatement JavaDoc wrapStatement(CallableStatement JavaDoc cs, String JavaDoc sql) throws SQLException JavaDoc {
177                 cs = super.wrapStatement(cs,sql);
178         XAStatementControl sc = new XAStatementControl(this, cs, sql);
179         return (CallableStatement JavaDoc) sc.applicationStatement;
180     }
181
182     /**
183         Override getRealConnection to create a a local connection
184         when we are not associated with an XA transaction.
185
186         This can occur if the application has a Connection object (conn)
187         and the following sequence occurs.
188
189         conn = xac.getConnection();
190         xac.start(xid, ...)
191         
192         // do work with conn
193
194         xac.end(xid, ...);
195
196         // do local work with conn
197         // need to create new connection here.
198     */

199     public EngineConnection getRealConnection() throws SQLException JavaDoc
200     {
201         EngineConnection rc = super.getRealConnection();
202         if (rc != null)
203             return rc;
204
205         openRealConnection();
206
207         // a new Connection, set its state according to the application's Connection handle
208
currentConnectionHandle.setState(true);
209
210         return realConnection;
211     }
212 }
213
Popular Tags