KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > checkDataSource


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.checkDataSource
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.derbyTesting.functionTests.tests.jdbcapi;
23
24 import java.io.Serializable JavaDoc;
25 import java.sql.CallableStatement JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.DriverManager JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.sql.ConnectionEvent JavaDoc;
37 import javax.sql.ConnectionEventListener JavaDoc;
38 import javax.sql.ConnectionPoolDataSource JavaDoc;
39 import javax.sql.DataSource JavaDoc;
40 import javax.sql.PooledConnection JavaDoc;
41 import javax.sql.XAConnection JavaDoc;
42 import javax.sql.XADataSource JavaDoc;
43 import javax.transaction.xa.XAException JavaDoc;
44 import javax.transaction.xa.XAResource JavaDoc;
45 import javax.transaction.xa.Xid JavaDoc;
46
47 import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;
48 import org.apache.derby.jdbc.EmbeddedDataSource;
49 import org.apache.derby.jdbc.EmbeddedXADataSource;
50 import org.apache.derby.jdbc.ClientConnectionPoolDataSource;
51 import org.apache.derby.jdbc.ClientDataSource;
52 import org.apache.derby.jdbc.ClientXADataSource;
53
54 import org.apache.derby.tools.JDBCDisplayUtil;
55 import org.apache.derby.tools.ij;
56 import org.apache.derbyTesting.functionTests.util.SecurityCheck;
57 import org.apache.derbyTesting.functionTests.util.TestUtil;
58 import org.apache.oro.text.perl.Perl5Util;
59
60 /**
61  * Test the various embedded DataSource implementations of Derby.
62  *
63  * Performs SecurityCheck analysis on the JDBC objects returned.
64  * This is because this test returns to the client a number of
65  * different implementations of Connection, Statement etc.
66  *
67  * @see org.apache.derbyTesting.functionTests.util.SecurityCheck
68  *
69  */

70 public class checkDataSource
71 {
72     // Only test connection toString values for embedded.
73
// Client connection toString values are not correlated at this time and just
74
// use default toString
75
// These tests are exempted from other frameworks
76
private boolean testConnectionToString = TestUtil.isEmbeddedFramework();
77     
78     // Only embedded supports SimpleDataSource (JSR169).
79
// These tests are exempted from other frameworks
80
private boolean testSimpleDataSource = TestUtil.isEmbeddedFramework();
81
82     /**
83      * A hashtable of opened connections. This is used when checking to
84      * make sure connection strings are unique; we need to make sure all
85      * the connections are closed when we are done, so they are stored
86      * in this hashtable
87      */

88     protected static Hashtable JavaDoc conns = new Hashtable JavaDoc();
89     
90     /**
91      * This is a utility that knows how to do pattern matching. Used
92      * in checking the format of a connection string
93      */

94     protected static Perl5Util p5u = new Perl5Util();
95     
96     /** The expected format of a connection string. In English:
97      * "<classname>@<hashcode> (XID=<xid>), (SESSION = <sessionid>),
98      * (DATABASE=<dbname>), (DRDAID = <drdaid>)"
99      */

100     private static final String JavaDoc CONNSTRING_FORMAT = "\\S+@[0-9]+ " +
101         "\\(XID = .*\\), \\(SESSIONID = [0-9]+\\), " +
102         "\\(DATABASE = [A-Za-z]+\\), \\(DRDAID = .+\\)";
103     
104     /**
105      * Hang onto the SecurityCheck class while running the
106      * tests so that it is not garbage collected during the
107      * test and lose the information it has collected.
108      */

109     private final Object JavaDoc nogc = SecurityCheck.class;
110
111     
112     
113     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
114
115         try
116         {
117             new checkDataSource().runTest(args);
118             
119             // Print a report on System.out of the issues
120
// found with the security checks.
121
SecurityCheck.report();
122
123         }
124         catch ( Exception JavaDoc e )
125         {
126             e.printStackTrace();
127             throw e;
128         }
129         System.out.println("Completed checkDataSource");
130
131     }
132
133     public checkDataSource() {
134     }
135
136     protected void runTest(String JavaDoc[] args) throws Exception JavaDoc {
137
138         // Check the returned type of the JDBC Connections.
139
ij.getPropertyArg(args);
140         Connection JavaDoc dmc = ij.startJBMS();
141
142         dmc.createStatement().executeUpdate("create table y(i int)");
143
144         dmc.createStatement().executeUpdate(
145                 "create procedure checkConn2(in dsname varchar(20)) " +
146                 "parameter style java language java modifies SQL DATA " +
147                 "external name 'org.apache.derbyTesting.functionTests.tests.jdbcapi." +
148                 this.getNestedMethodName() +
149                 "'");
150         CallableStatement JavaDoc cs = dmc.prepareCall("call checkConn2(?)");
151         cs.setString(1,"Nested");
152         cs.execute();
153         
154
155         checkConnection("DriverManager ", dmc);
156         if (testConnectionToString)
157             checkJBMSToString();
158
159
160         Properties JavaDoc attrs = new Properties JavaDoc();
161         attrs.setProperty("databaseName", "wombat");
162         DataSource JavaDoc dscs = TestUtil.getDataSource(attrs);
163         
164         if (testConnectionToString)
165                 checkToString(dscs);
166
167         DataSource JavaDoc ds = dscs;
168
169         checkConnection("DataSource", ds.getConnection());
170          
171         DataSource JavaDoc dssimple = null;
172         if (testSimpleDataSource)
173         {
174             dssimple = TestUtil.getSimpleDataSource(attrs);
175             ds = dssimple;
176             checkConnection("SimpleDataSource", ds.getConnection());
177         }
178
179         ConnectionPoolDataSource JavaDoc dsp = TestUtil.getConnectionPoolDataSource(attrs);
180         
181         
182         if (testConnectionToString)
183             checkToString(dsp);
184     
185
186         PooledConnection JavaDoc pc = dsp.getPooledConnection();
187         SecurityCheck.inspect(pc, "javax.sql.PooledConnection");
188         pc.addConnectionEventListener(new EventCatcher(1));
189
190         checkConnection("ConnectionPoolDataSource", pc.getConnection());
191         checkConnection("ConnectionPoolDataSource", pc.getConnection());
192
193         // BUG 4471 - check outstanding updates are rolled back.
194
Connection JavaDoc c1 = pc.getConnection();
195
196         Statement JavaDoc s = c1.createStatement();
197
198         s.executeUpdate("create table t (i int)");
199
200         s.executeUpdate("insert into t values(1)");
201
202         c1.setAutoCommit(false);
203
204         // this update should be rolled back
205
s.executeUpdate("insert into t values(2)");
206         
207         c1 = pc.getConnection();
208
209         ResultSet JavaDoc rs = c1.createStatement().executeQuery("select count(*) from t");
210         rs.next();
211         int count = rs.getInt(1);
212
213         System.out.println(count == 1 ? "Changes rolled back OK in auto closed pooled connection" :
214                 ("FAIL changes committed in in auto closed pooled connection - " + count));
215
216         c1.close();
217
218         // check connection objects are closed once connection is closed
219
try {
220             rs.next();
221             System.out.println("FAIL - ResultSet is open for a closed connection obtained from PooledConnection");
222         } catch (SQLException JavaDoc sqle) {
223             System.out.println("expected SQL Exception: (" + sqle.getSQLState()
224                               + ") " + sqle.getMessage());
225         }
226
227         try {
228             s.executeUpdate("update t set i = 1");
229             System.out.println("FAIL - Statement is open for a closed connection obtained from PooledConnection");
230         } catch (SQLException JavaDoc sqle) {
231             System.out.println("expected SQL Exception: (" + sqle.getSQLState()
232                               + ") " + sqle.getMessage());
233         }
234
235         pc.close();
236         pc = null;
237
238         testPoolReset("ConnectionPoolDataSource", dsp.getPooledConnection());
239
240         XADataSource JavaDoc dsx = TestUtil.getXADataSource(attrs);
241         if (testConnectionToString)
242             checkToString(dsx);
243
244         XAConnection JavaDoc xac = dsx.getXAConnection();
245         SecurityCheck.inspect(xac, "javax.sql.XAConnection");
246         xac.addConnectionEventListener(new EventCatcher(3));
247
248         checkConnection("XADataSource", xac.getConnection());
249
250         // BUG 4471 - check outstanding updates are rolled back wi XAConnection.
251
c1 = xac.getConnection();
252
253         s = c1.createStatement();
254
255         s.executeUpdate("insert into t values(1)");
256
257         c1.setAutoCommit(false);
258
259         // this update should be rolled back
260
s.executeUpdate("insert into t values(2)");
261         
262         c1 = xac.getConnection();
263
264         rs = c1.createStatement().executeQuery("select count(*) from t");
265         rs.next();
266         count = rs.getInt(1);
267         rs.close();
268
269         System.out.println(count == 2 ? "Changes rolled back OK in auto closed local XAConnection" :
270                 ("FAIL changes committed in in auto closed pooled connection - " + count));
271
272         c1.close();
273         xac.close();
274         xac = null;
275
276         testPoolReset("XADataSource", dsx.getXAConnection());
277
278         try {
279             TestUtil.getConnection("","shutdown=true");
280         } catch (SQLException JavaDoc sqle) {
281             JDBCDisplayUtil.ShowSQLException(System.out, sqle);
282         }
283
284         dmc = ij.startJBMS();
285
286         cs = dmc.prepareCall("call checkConn2(?)");
287         SecurityCheck.inspect(cs, "java.sql.CallableStatement");
288         cs.setString(1,"Nested");
289         cs.execute();
290         
291
292         checkConnection("DriverManager ", dmc);
293
294         // reset ds back to the Regular DataSource
295
ds = dscs;
296         checkConnection("DataSource", ds.getConnection());
297         
298         // and back to EmbeddedSimpleDataSource
299
if(TestUtil.isEmbeddedFramework())
300         {
301             // JSR169 (SimpleDataSource) is only available on embedded.
302
ds = dssimple;
303             checkConnection("EmbeddedSimpleDataSource", dssimple.getConnection());
304         }
305         
306         pc = dsp.getPooledConnection();
307         pc.addConnectionEventListener(new EventCatcher(2));
308         checkConnection("ConnectionPoolDataSource", pc.getConnection());
309         checkConnection("ConnectionPoolDataSource", pc.getConnection());
310
311         // test "local" XAConnections
312
xac = dsx.getXAConnection();
313         xac.addConnectionEventListener(new EventCatcher(4));
314         checkConnection("XADataSource", xac.getConnection());
315         checkConnection("XADataSource", xac.getConnection());
316         xac.close();
317
318         // test "global" XAConnections
319
xac = dsx.getXAConnection();
320         xac.addConnectionEventListener(new EventCatcher(5));
321         XAResource JavaDoc xar = xac.getXAResource();
322         SecurityCheck.inspect(xar, "javax.transaction.xa.XAResource");
323         Xid JavaDoc xid = new cdsXid(1, (byte) 35, (byte) 47);
324         xar.start(xid, XAResource.TMNOFLAGS);
325         Connection JavaDoc xacc = xac.getConnection();
326         xacc.close();
327         checkConnection("Global XADataSource", xac.getConnection());
328         checkConnection("Global XADataSource", xac.getConnection());
329
330         xar.end(xid, XAResource.TMSUCCESS);
331
332         checkConnection("Switch to local XADataSource", xac.getConnection());
333         checkConnection("Switch to local XADataSource", xac.getConnection());
334
335         Connection JavaDoc backtoGlobal = xac.getConnection();
336
337         xar.start(xid, XAResource.TMJOIN);
338         checkConnection("Switch to global XADataSource", backtoGlobal);
339         checkConnection("Switch to global XADataSource", xac.getConnection());
340         xar.end(xid, XAResource.TMSUCCESS);
341         xar.commit(xid, true);
342
343         xac.close();
344
345         // now some explicit tests for how connection state behaves
346
// when switching between global transactions and local
347
// and setting connection state.
348
// some of this is already tested in simpleDataSource and checkDataSource
349
// but I want to make sure I cover all situations. (djd)
350
xac = dsx.getXAConnection();
351         xac.addConnectionEventListener(new EventCatcher(6));
352         xar = xac.getXAResource();
353         xid = new cdsXid(1, (byte) 93, (byte) 103);
354
355         // series 1 - Single connection object
356
Connection JavaDoc cs1 = xac.getConnection();
357         printState("initial local", cs1);
358         xar.start(xid, XAResource.TMNOFLAGS);
359         printState("initial X1", cs1);
360         cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
361         cs1.setReadOnly(true);
362         setHoldability(cs1, false);
363         printState("modified X1", cs1);
364         xar.end(xid, XAResource.TMSUCCESS);
365         // the underlying local transaction/connection must pick up the
366
// state of the Connection handle cs1
367
printState("modified local", cs1);
368         
369         cs1.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
370         cs1.setReadOnly(false);
371         setHoldability(cs1, false);
372
373         printState("reset local", cs1);
374
375         // now re-join the transaction, should pick up the read-only
376
// and isolation level from the transaction,
377
// holdability remains that of this handle.
378
xar.start(xid, XAResource.TMJOIN);
379         printState("re-join X1", cs1);
380         xar.end(xid, XAResource.TMSUCCESS);
381
382         // should be the same as the reset local
383
printState("back to local (same as reset)", cs1);
384         
385         // test suspend/resume
386
// now re-join the transaction, should pick up the read-only
387
// and isolation level from the transaction,
388
// holdability remains that of this handle.
389
xar.start(xid, XAResource.TMJOIN);
390         printState("re-join X1 second time", cs1);
391         
392         xar.end(xid, XAResource.TMSUSPEND);
393         printState("local after suspend", cs1);
394         
395         xar.start(xid, XAResource.TMRESUME);
396         printState("resume X1", cs1);
397         
398         xar.end(xid, XAResource.TMSUCCESS);
399         printState("back to local (second time)", cs1);
400         
401         cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
402         cs1.setReadOnly(true);
403         setHoldability(cs1, true);
404         cs1.close();
405         
406         cs1 = xac.getConnection();
407         printState("new handle - local ", cs1);
408         cs1.close();
409         
410         xar.start(xid, XAResource.TMJOIN);
411         cs1 = xac.getConnection();
412         printState("re-join with new handle X1", cs1);
413         cs1.close();
414         xar.end(xid, XAResource.TMSUCCESS);
415
416         // now get a connection (attached to a local)
417
// attach to the global and commit it.
418
// state should be that of the local after the commit.
419
cs1 = xac.getConnection();
420         cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
421         printState("pre-X1 commit - local", cs1);
422         xar.start(xid, XAResource.TMJOIN);
423         printState("pre-X1 commit - X1", cs1);
424         xar.end(xid, XAResource.TMSUCCESS);
425         printState("post-X1 end - local", cs1);
426         xar.commit(xid, true);
427         printState("post-X1 commit - local", cs1);
428         cs1.close();
429
430         //Derby-421 Setting isolation level with SQL was not getting handled correctly
431
System.out.println("Some more isolation testing using SQL and JDBC api");
432         cs1 = xac.getConnection();
433         s = cs1.createStatement();
434         printState("initial local", cs1);
435
436     System.out.println("Issue setTransactionIsolation in local transaction");
437         cs1.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
438         printState("setTransactionIsolation in local", cs1);
439
440         testSetIsolationWithStatement(s, xar, cs1);
441
442         // now check re-use of *Statement objects across local/global connections.
443
System.out.println("TESTING RE_USE OF STATEMENT OBJECTS");
444         cs1 = xac.getConnection();
445
446     
447         // ensure read locks stay around until end-of transaction
448
cs1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
449         cs1.setAutoCommit(false);
450
451         checkLocks(cs1);
452         
453         Statement JavaDoc sru1 = cs1.createStatement();
454         sru1.setCursorName("SN1");
455         sru1.executeUpdate("create table ru(i int)");
456         sru1.executeUpdate("insert into ru values 1,2,3");
457         Statement JavaDoc sruBatch = cs1.createStatement();
458         sruBatch.setCursorName("sruBatch");
459         Statement JavaDoc sruState = createFloatStatementForStateChecking(cs1);
460         PreparedStatement JavaDoc psruState = createFloatStatementForStateChecking(cs1, "select i from ru where i = ?");
461         CallableStatement JavaDoc csruState = createFloatCallForStateChecking(cs1, "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?,?)");
462         PreparedStatement JavaDoc psParams = cs1.prepareStatement("select * from ru where i > ?");
463         psParams.setCursorName("params");
464         psParams.setInt(1, 2);
465         resultSetQuery("Params-local-1", psParams.executeQuery());
466
467         sruBatch.addBatch("insert into ru values 4");
468         queryOnStatement("sru1-local-1", cs1, sru1);
469         cs1.commit(); // need to commit to switch to an global connection;
470
xid = new cdsXid(1, (byte) 103, (byte) 119);
471         xar.start(xid, XAResource.TMNOFLAGS); // simple case - underlying connection is re-used for global.
472
System.out.println("Expecting downgrade because global transaction sru1-global-2 is using a statement with holdability true");
473         queryOnStatement("sru1-global-2", cs1, sru1);
474         sruBatch.addBatch("insert into ru values 5");
475         Statement JavaDoc sru2 = cs1.createStatement();
476         sru2.setCursorName("OAK2");
477         queryOnStatement("sru2-global-3", cs1, sru2);
478         System.out.println("Expecting downgrade because global transaction sru1-global-4 is using a statement with holdability true");
479         queryOnStatement("sru1-global-4", cs1, sru1);
480         showStatementState("GLOBAL ", sruState);
481         showStatementState("PS GLOBAL ", psruState);
482         showStatementState("CS GLOBAL ", csruState);
483         resultSetQuery("Params-global-1", psParams.executeQuery());
484
485         xar.end(xid, XAResource.TMSUCCESS);
486         // now a new underlying connection is created
487
queryOnStatement("sru1-local-5", cs1, sru1);
488         queryOnStatement("sru2-local-6", cs1, sru2);
489         sruBatch.addBatch("insert into ru values 6,7");
490         Statement JavaDoc sru3 = cs1.createStatement();
491         sru3.setCursorName("SF3");
492         queryOnStatement("sru3-local-7", cs1, sru3);
493         // Two transactions should hold locks (global and the current XA);
494
showStatementState("LOCAL ", sruState);
495         showStatementState("PS LOCAL ", psruState);
496         showStatementState("CS LOCAL ", csruState);
497         resultSetQuery("Params-local-2", psParams.executeQuery());
498         checkLocks(cs1);
499         cs1.commit();
500
501         // attach the XA transaction to another connection and see what happens
502
XAConnection JavaDoc xac2 = dsx.getXAConnection();
503         xac2.addConnectionEventListener(new EventCatcher(5));
504         XAResource JavaDoc xar2 = xac2.getXAResource();
505
506         xar2.start(xid, XAResource.TMJOIN);
507         Connection JavaDoc cs2 = xac2.getConnection();
508
509         // these statements were generated by cs1 and thus are still
510
// in a local connection.
511
queryOnStatement("sru1-local-8", cs1, sru1);
512         queryOnStatement("sru2-local-9", cs1, sru2);
513         queryOnStatement("sru3-local-10", cs1, sru3);
514         sruBatch.addBatch("insert into ru values 8");
515         showStatementState("LOCAL 2 ", sruState);
516         showStatementState("PS LOCAL 2 ", psruState);
517         showStatementState("CS LOCAL 2", csruState);
518
519         checkLocks(cs1);
520
521         int[] updateCounts = sruBatch.executeBatch();
522         System.out.print("sruBatch update counts :");
523         for (int i = 0; i < updateCounts.length; i++) {
524             System.out.print(" " + updateCounts[i] + " ");
525         }
526         System.out.println(":");
527         queryOnStatement("sruBatch", cs1, sruBatch);
528
529
530         xar2.end(xid, XAResource.TMSUCCESS);
531
532         xac2.close();
533
534         // allow close on already closed XAConnection
535
xac2.close();
536         xac2.addConnectionEventListener(null);
537         xac2.removeConnectionEventListener(null);
538
539         // test methods against a closed XAConnection and its resource
540
try {
541             xac2.getXAResource();
542         } catch (SQLException JavaDoc sqle) {
543             System.out.println("XAConnection.getXAResource : " + sqle.getMessage());
544         }
545         try {
546             xac2.getConnection();
547         } catch (SQLException JavaDoc sqle) {
548             System.out.println("XAConnection.getConnection : " + sqle.getMessage());
549         }
550         try {
551             xar2.start(xid, XAResource.TMJOIN);
552         } catch (XAException JavaDoc xae) {
553             showXAException("XAResource.start", xae);
554         }
555
556         try {
557             xar2.end(xid, XAResource.TMJOIN);
558         } catch (XAException JavaDoc xae) {
559             showXAException("XAResource.end", xae);
560         }
561         try {
562             xar2.commit(xid, true);
563         } catch (XAException JavaDoc xae) {
564             showXAException("XAResource.commit", xae);
565         }
566         try {
567             xar2.prepare(xid);
568         } catch (XAException JavaDoc xae) {
569             showXAException("XAResource.prepare", xae);
570         }
571         try {
572             xar2.recover(0);
573         } catch (XAException JavaDoc xae) {
574             showXAException("XAResource.recover", xae);
575         }
576         try {
577             xar2.prepare(xid);
578         } catch (XAException JavaDoc xae) {
579             showXAException("XAResource.prepare", xae);
580         }
581         try {
582             xar2.isSameRM(xar2);
583         } catch (XAException JavaDoc xae) {
584             showXAException("XAResource.isSameRM", xae);
585         }
586
587         // Patricio (on the forum) one was having an issue with set schema not working in an XA connection.
588
dmc = ij.startJBMS();
589         dmc.createStatement().executeUpdate("create schema SCHEMA_Patricio");
590         dmc.createStatement().executeUpdate("create table SCHEMA_Patricio.Patricio (id VARCHAR(255), value INTEGER)");
591         dmc.commit();
592
593         dmc.close();
594
595         XAConnection JavaDoc xac3 = dsx.getXAConnection();
596         Connection JavaDoc conn3 = xac3.getConnection();
597         Statement JavaDoc st3 = conn3.createStatement();
598         st3.execute("SET SCHEMA SCHEMA_Patricio");
599         st3.close();
600
601         PreparedStatement JavaDoc ps3 = conn3.prepareStatement("INSERT INTO Patricio VALUES (? , ?)");
602         ps3.setString(1, "Patricio");
603         ps3.setInt(2, 3);
604         ps3.executeUpdate();
605
606         System.out.println("Patricio update count " + ps3.getUpdateCount());
607         ps3.close();
608         conn3.close();
609         xac3.close();
610
611         // test that an xastart in auto commit mode commits the existing work.(beetle 5178)
612
XAConnection JavaDoc xac4 = dsx.getXAConnection();
613         Xid JavaDoc xid4a = new cdsXid(4, (byte) 23, (byte) 76);
614         Connection JavaDoc conn4 = xac4.getConnection();
615         System.out.println("conn4 autcommit " + conn4.getAutoCommit());
616
617         Statement JavaDoc s4 = conn4.createStatement();
618         s4.executeUpdate("create table autocommitxastart(i int)");
619         s4.executeUpdate("insert into autocommitxastart values 1,2,3,4,5");
620
621         ResultSet JavaDoc rs4 = s4.executeQuery("select i from autocommitxastart");
622         rs4.next(); System.out.println("acxs " + rs4.getInt(1));
623         rs4.next(); System.out.println("acxs " + rs4.getInt(1));
624
625         xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
626         xac4.getXAResource().end(xid4a, XAResource.TMSUCCESS);
627
628         try {
629             rs4.next(); System.out.println("acxs " + rs.getInt(1));
630         } catch (SQLException JavaDoc sqle) {
631             System.out.println("autocommitxastart expected " + sqle.getMessage());
632         }
633
634         conn4.setAutoCommit(false);
635
636         rs4 = s4.executeQuery("select i from autocommitxastart");
637         rs4.next(); System.out.println("acxs " + rs4.getInt(1));
638         rs4.next(); System.out.println("acxs " + rs4.getInt(1));
639         
640          // Get a new xid to begin another transaction.
641
// This should give XAER_OUTSIDE exception because
642
// the resource manager is busy in the local transaction
643
xid4a = new cdsXid(4, (byte) 93, (byte) 103);
644         try {
645             xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
646         } catch (XAException JavaDoc xae) {
647             showXAException("autocommitxastart expected ", xae);
648             System.out.println("Expected XA error code: " + xae.errorCode);
649         }
650         rs4.next(); System.out.println("acxs " + rs4.getInt(1));
651         rs4.close();
652
653         conn4.rollback();
654         conn4.close();
655         xac4.close();
656         
657         
658         // test jira-derby 95 - a NullPointerException was returned when passing
659
// an incorrect database name (a url in this case) - should now give error XCY00
660
Connection JavaDoc dmc95 = ij.startJBMS();
661         String JavaDoc sqls;
662         try {
663             testJira95ds( dmc95, "jdbc:derby:mydb" );
664         } catch (SQLException JavaDoc sqle) {
665             sqls = sqle.getSQLState();
666             if (sqls.equals("XCY00"))
667                 System.out.println("; ok - expected exception: " + sqls);
668             else
669                 System.out.println("; wrong, unexpected exception: " + sqls + " - " + sqle.toString());
670         } catch (Exception JavaDoc e) {
671                 System.out.println("; wrong, unexpected exception: " + e.toString());
672         }
673             
674         try {
675             testJira95xads( dmc95, "jdbc:derby:wombat" );
676         } catch (SQLException JavaDoc sqle) {
677             sqls = sqle.getSQLState();
678             if (sqls.equals("XCY00"))
679                 System.out.println("; ok - expected exception: " + sqls + "\n");
680             else
681                 System.out.println("; wrong - unexpected exception: " + sqls + " - " + sqle.toString());
682         } catch (Exception JavaDoc e) {
683                 System.out.println("; wrong, unexpected exception: " + e.toString());
684         }
685         
686         if (TestUtil.isDerbyNetClientFramework())
687             testClientDSConnectionAttributes();
688         
689         // skip testDSRequestAuthentication for client because of this issue:
690
// DERBY-1131 : Deprecate Derby DataSource property attributesAsPassword
691
// First part of this test is covered by testClientDSConnectionAttributes()
692
if (TestUtil.isDerbyNetClientFramework())
693             return;
694         testDSRequestAuthentication();
695     }
696
697     /**
698      * @param s
699      * @param xar
700      * @param conn
701      * @throws SQLException
702      * @throws XAException
703      */

704     private void testSetIsolationWithStatement(Statement JavaDoc s, XAResource JavaDoc xar, Connection JavaDoc conn) throws SQLException JavaDoc, XAException JavaDoc {
705         Xid JavaDoc xid;
706         System.out.println("Issue SQL to change isolation in local transaction");
707             s.executeUpdate("set current isolation = RR");
708             printState("SQL to change isolation in local", conn);
709
710             xid = new cdsXid(1, (byte) 35, (byte) 47);
711             xar.start(xid, XAResource.TMNOFLAGS);
712             printState("1st global(new)", conn);
713             xar.end(xid, XAResource.TMSUCCESS);
714
715             printState("local", conn);
716         System.out.println("Issue SQL to change isolation in local transaction");
717             s.executeUpdate("set current isolation = RS");
718             printState("SQL to change isolation in local", conn);
719             
720             // DERBY-1325 - Isolation level of local connection does not get reset after ending
721
// a global transaction that was joined/resumed if the isolation level was changed
722
// using SQL
723
xar.start(xid, XAResource.TMJOIN);
724             printState("1st global(existing)", conn);
725             xar.end(xid, XAResource.TMSUCCESS);
726             printState("local", conn);
727             // DERBY-1325 end test
728

729             Xid JavaDoc xid2 = new cdsXid(1, (byte) 93, (byte) 103);
730             xar.start(xid2, XAResource.TMNOFLAGS);
731             printState("2nd global(new)", conn);
732             xar.end(xid2, XAResource.TMSUCCESS);
733
734             xar.start(xid, XAResource.TMJOIN);
735             printState("1st global(existing)", conn);
736             xar.end(xid, XAResource.TMSUCCESS);
737
738             printState("local", conn);
739
740             xar.start(xid, XAResource.TMJOIN);
741             printState("1st global(existing)", conn);
742         System.out.println("Issue SQL to change isolation in 1st global transaction");
743             s.executeUpdate("set current isolation = UR");
744             printState("change isolation of existing 1st global transaction", conn);
745             xar.end(xid, XAResource.TMSUCCESS);
746
747             printState("local", conn);
748
749             xar.start(xid2, XAResource.TMJOIN);
750             printState("2nd global(existing)", conn);
751             xar.end(xid2, XAResource.TMSUCCESS);
752             xar.rollback(xid2);
753             printState("(After 2nd global rollback) local", conn);
754
755             xar.rollback(xid);
756             printState("(After 1st global rollback) local", conn);
757     }
758
759     protected void showXAException(String JavaDoc tag, XAException JavaDoc xae) {
760
761         System.out.println(tag + " : XAException - " + xae.getMessage());
762     }
763
764     /**
765         Create a statement with modified State.
766     */

767     protected Statement JavaDoc createFloatStatementForStateChecking(Connection JavaDoc conn) throws SQLException JavaDoc {
768         Statement JavaDoc s = internalCreateFloatStatementForStateChecking(conn);
769         s.setCursorName("StokeNewington");
770         s.setFetchDirection(ResultSet.FETCH_REVERSE);
771         s.setFetchSize(444);
772         s.setMaxFieldSize(713);
773         s.setMaxRows(19);
774
775         showStatementState("Create ", s);
776         return s;
777     }
778
779     protected Statement JavaDoc internalCreateFloatStatementForStateChecking(Connection JavaDoc conn) throws SQLException JavaDoc {
780         return conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
781     }
782
783     protected void showStatementState(String JavaDoc when, Statement JavaDoc s) throws SQLException JavaDoc {
784         System.out.println("Statement State @ " + when);
785         System.out.println(" getResultSetType() " + rsType(s.getResultSetType()));
786         System.out.println(" getResultSetConcurrency() " + rsConcurrency(s.getResultSetConcurrency()));
787         System.out.println(" getFetchDirection() " + rsFetchDirection(s.getFetchDirection()));
788         System.out.println(" getFetchSize() " + s.getFetchSize());
789         System.out.println(" getMaxFieldSize() " + s.getMaxFieldSize());
790         System.out.println(" getMaxRows() " + s.getMaxRows());
791     }
792     protected PreparedStatement JavaDoc createFloatStatementForStateChecking(Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc {
793         PreparedStatement JavaDoc s = internalCreateFloatStatementForStateChecking(conn, sql);
794         s.setCursorName("StokeNewington");
795         s.setFetchDirection(ResultSet.FETCH_REVERSE);
796         s.setFetchSize(888);
797         s.setMaxFieldSize(317);
798         s.setMaxRows(91);
799
800         showStatementState("PS Create ", s);
801         return s;
802     }
803     protected CallableStatement JavaDoc createFloatCallForStateChecking(Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc {
804         CallableStatement JavaDoc s = internalCreateFloatCallForStateChecking(conn, sql);
805         s.setCursorName("StokeNewington");
806         s.setFetchDirection(ResultSet.FETCH_REVERSE);
807         s.setFetchSize(999);
808         s.setMaxFieldSize(137);
809         s.setMaxRows(85);
810
811         showStatementState("CS Create ", s);
812         return s;
813     }
814     protected PreparedStatement JavaDoc internalCreateFloatStatementForStateChecking(Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc {
815         return conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
816     }
817     protected CallableStatement JavaDoc internalCreateFloatCallForStateChecking(Connection JavaDoc conn, String JavaDoc sql) throws SQLException JavaDoc {
818         return conn.prepareCall(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
819     }
820     
821     /**
822      * Return the Java class and method for the procedure
823      * for the nested connection test.
824      * checkDataSource 30 will override.
825      */

826     protected String JavaDoc getNestedMethodName()
827     {
828         return "checkDataSource.checkNesConn";
829     }
830
831     static String JavaDoc rsType(int type) {
832         switch (type) {
833         case ResultSet.TYPE_FORWARD_ONLY:
834             return "FORWARD_ONLY";
835         case ResultSet.TYPE_SCROLL_SENSITIVE:
836             return "SCROLL_SENSITIVE";
837         case ResultSet.TYPE_SCROLL_INSENSITIVE:
838             return "SCROLL_INSENSITIVE";
839         default:
840             return "?? TYPE UNKNOWN ??";
841
842         }
843     }
844
845     static String JavaDoc rsConcurrency(int type) {
846         switch (type) {
847         case ResultSet.CONCUR_READ_ONLY:
848             return "READ_ONLY";
849         case ResultSet.CONCUR_UPDATABLE:
850             return "UPDATEABLE";
851         default:
852             return "?? CONCURRENCY UNKNOWN ??";
853
854         }
855     }
856     static String JavaDoc rsFetchDirection(int type) {
857         switch (type) {
858         case ResultSet.FETCH_FORWARD:
859             return "FORWARD";
860         case ResultSet.FETCH_REVERSE:
861             return "REVERSE";
862         case ResultSet.FETCH_UNKNOWN:
863             return "UNKNOWN";
864         default:
865             return "?? FETCH DIRECTION REALLY UNKNOWN ??";
866
867         }
868     }
869     private static void checkLocks(Connection JavaDoc conn) throws SQLException JavaDoc {
870         Statement JavaDoc s = conn.createStatement();
871         ResultSet JavaDoc rs = s.executeQuery("SELECT XID, sum(cast (LOCKCOUNT AS INT)) FROM SYSCS_DIAG.LOCK_TABLE AS L GROUP BY XID");
872         System.out.println("LOCK TABLE");
873
874         // Don't output actual XID's as they tend for every catalog change
875
// to the system.
876
int xact_index = 0;
877         while (rs.next()) {
878             // System.out.println(" xid " + rs.getString(1) + " lock count " + rs.getInt(2));
879
System.out.println(" xid row " + xact_index + " lock count " + rs.getInt(2));
880             xact_index++;
881         }
882         s.close();
883         System.out.println("END LOCK TABLE");
884     }
885
886     private static void queryOnStatement(String JavaDoc tag, Connection JavaDoc conn, Statement JavaDoc s) throws SQLException JavaDoc {
887
888         try {
889             if (s.getConnection() != conn)
890                 System.out.println(tag + ": mismatched Statement connection");
891             resultSetQuery(tag, s.executeQuery("select * from ru"));
892         } catch (SQLException JavaDoc sqle) {
893             System.out.println(tag + ": " + sqle.toString());
894         }
895     }
896
897     private static void resultSetQuery(String JavaDoc tag, ResultSet JavaDoc rs) throws SQLException JavaDoc {
898         String JavaDoc cursorName = rs.getCursorName();
899         System.out.print(tag + ": ru(" + cursorName + ") contents");
900         SecurityCheck.inspect(rs, "java.sql.ResultSet");
901         while (rs.next()) {
902             System.out.print(" {" + rs.getInt(1) + "}");
903         }
904         System.out.println("");
905         rs.close();
906     }
907
908     private void printState(String JavaDoc header, Connection JavaDoc conn) throws SQLException JavaDoc {
909         System.out.println(header);
910         getHoldability(conn);
911         System.out.println(" isolation level " + translateIso(conn.getTransactionIsolation()));
912         System.out.println(" auto commit " + conn.getAutoCommit());
913         System.out.println(" read only " + conn.isReadOnly());
914     }
915
916     protected void setHoldability(Connection JavaDoc conn, boolean hold) throws SQLException JavaDoc {
917     }
918
919     protected void getHoldability(Connection JavaDoc conn) throws SQLException JavaDoc {
920     }
921
922     //calling checkConnection - for use in a procedure to get a nested connection.
923
public static void checkNesConn (String JavaDoc dsName) throws SQLException JavaDoc {
924         Connection JavaDoc conn = DriverManager.getConnection("jdbc:default:connection");
925         new checkDataSource().checkConnection(dsName, conn);
926     }
927
928     public void checkConnection(String JavaDoc dsName, Connection JavaDoc conn) throws SQLException JavaDoc {
929
930         System.out.println("Running connection checks on " + dsName);
931
932         SecurityCheck.inspect(conn, "java.sql.Connection");
933         SecurityCheck.inspect(conn.getMetaData(), "java.sql.DatabaseMetaData");
934
935         //System.out.println(" url " + conn.getMetaData().getURL());
936
System.out.println(" isolation level " + conn.getTransactionIsolation());
937         System.out.println(" auto commit " + conn.getAutoCommit());
938         System.out.println(" read only " + conn.isReadOnly());
939
940         // when 4729 is fixed, remove the startsWith() clause
941
if (dsName.endsWith("DataSource") && !dsName.startsWith("Global"))
942             System.out.println(" has warnings " + (conn.getWarnings() != null));
943
944         Statement JavaDoc s1 = conn.createStatement();
945         checkStatement(dsName, conn, s1);
946         checkStatement(dsName, conn, conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
947
948         Connection JavaDoc c1 = conn.getMetaData().getConnection();
949         if (c1 != conn)
950             System.out.println("FAIL incorrect connection object returned for DatabaseMetaData.getConnection()");
951
952         // Derby-33 - setTypeMap on connection
953
try {
954             conn.setTypeMap(java.util.Collections.EMPTY_MAP);
955             System.out.println("setTypeMap(EMPTY_MAP) - ok");
956         } catch (SQLException JavaDoc sqle) {
957             System.out.println("setTypeMap(EMPTY_MAP) - FAIL " + sqle.getSQLState() + " - " + sqle.getMessage());
958         }
959         try {
960             conn.setTypeMap(null);
961             System.out.println("setTypeMap(null) - FAIL - should throw exception");
962         } catch (SQLException JavaDoc sqle) {
963             System.out.println("setTypeMap(null) - ok " + sqle.getSQLState() + " - " + sqle.getMessage());
964         }
965         try {
966             // a populated map, not implemented
967
java.util.Map JavaDoc map = new java.util.HashMap JavaDoc();
968             map.put("name", "class");
969
970             conn.setTypeMap(map);
971             System.out.println("setTypeMap(map) - FAIL - should throw exception");
972         } catch (SQLException JavaDoc sqle) {
973             System.out.println("setTypeMap(map) - ok " + sqle.getSQLState() + " - " + sqle.getMessage());
974         }
975
976         checkConnectionPreClose(dsName, conn);
977         conn.close();
978
979         System.out.println("method calls on a closed connection");
980
981         try {
982             conn.close();
983             System.out.println(dsName + " <closedconn>.close() no error");
984         } catch (SQLException JavaDoc sqle) {
985             System.out.println(dsName + " <closedconn>.close() " + sqle.getSQLState() + " - " + sqle.getMessage());
986         }
987         try {
988             conn.createStatement();
989             System.out.println(dsName + " <closedconn>.createStatement() no error");
990         } catch (SQLException JavaDoc sqle) {
991             System.out.println(dsName + " <closedconn>.createStatement() " + sqle.getSQLState() + " - " + sqle.getMessage());
992         }
993         try {
994             s1.execute("values 1");
995             System.out.println(dsName + " <closedstmt>.execute() no error");
996         } catch (SQLException JavaDoc sqle) {
997             System.out.println(dsName + " <closedstmt>.execute() " + sqle.getSQLState() + " - " + sqle.getMessage());
998         }
999     }
1000        
1001    /**
1002     * Make sure this connection's string is unique (DERBY-243)
1003     */

1004    protected static void checkToString(Connection JavaDoc conn) throws Exception JavaDoc
1005    {
1006        checkStringFormat(conn);
1007        String JavaDoc str = conn.toString();
1008
1009        if ( conns.containsKey(str))
1010        {
1011            throw new Exception JavaDoc("ERROR: Connection toString() is not unique: "
1012              + str);
1013        }
1014        conns.put(str, conn);
1015    }
1016    
1017    /**
1018     * Check the format of a pooled connection
1019     **/

1020    protected static void checkStringFormat(PooledConnection JavaDoc pc) throws Exception JavaDoc
1021    {
1022        String JavaDoc prefix = checkStringPrefix(pc);
1023        String JavaDoc connstr = pc.toString();
1024        String JavaDoc format = "/" + prefix +
1025            " \\(ID = [0-9]+\\), Physical Connection = " +
1026            "<none>|" + CONNSTRING_FORMAT + "/";
1027        
1028        if ( ! p5u.match(format, connstr) )
1029        {
1030            throw new Exception JavaDoc( "Connection.toString() (" + connstr + ") " +
1031              "does not match expected format (" + format + ")");
1032        }
1033    }
1034        
1035     /**
1036     * Check the format of the connection string. This is the default test
1037     * to run if this is not a BrokeredConnection class
1038     */

1039    protected static void checkStringFormat(Connection JavaDoc conn) throws Exception JavaDoc
1040    {
1041        String JavaDoc prefix = checkStringPrefix(conn);
1042        
1043        String JavaDoc str = conn.toString();
1044       
1045        // See if the connection string matches the format pattern
1046
if ( ! p5u.match("/" + CONNSTRING_FORMAT + "/", str) )
1047        {
1048            throw new Exception JavaDoc( "Connection.toString() (" + str + ") " +
1049                "does not match expected format (" + CONNSTRING_FORMAT + ")");
1050        }
1051    }
1052    
1053    /**
1054     * Make sure the connection string starts with the right prefix, which
1055     * is the classname@hashcode.
1056     *
1057     * @return the expected prefix string, this is used in further string
1058     * format checking
1059     */

1060    protected static String JavaDoc checkStringPrefix(Object JavaDoc conn) throws Exception JavaDoc
1061    {
1062        String JavaDoc connstr = conn.toString();
1063        String JavaDoc prefix = conn.getClass().getName() + "@" + conn.hashCode();
1064        if ( ! connstr.startsWith(prefix) )
1065        {
1066            throw new Exception JavaDoc("Connection class and hash code for " +
1067                "connection string (" + connstr + ") does not match expected " +
1068                "(" + prefix + ")");
1069        }
1070        
1071        return prefix;
1072    }
1073    
1074    /**
1075     * Clear out and close connections in the connections
1076     * hashtable.
1077     */

1078    protected static void clearConnections() throws SQLException JavaDoc
1079    {
1080        java.util.Iterator JavaDoc it = conns.values().iterator();
1081        while ( it.hasNext() )
1082        {
1083            Connection JavaDoc conn = (Connection JavaDoc)it.next();
1084            conn.close();
1085        }
1086        conns.clear();
1087    }
1088    
1089    /**
1090     * Get connections using ij.startJBMS() and make sure
1091     * they're unique
1092     */

1093    protected static void checkJBMSToString() throws Exception JavaDoc
1094    {
1095        clearConnections();
1096        // Open ten connections rather than just two to
1097
// try and catch any odd uniqueness bugs. Still
1098
// no guarantee but is better than just two.
1099
int numConnections = 10;
1100        for ( int i = 0 ; i < numConnections ; i++ )
1101        {
1102            Connection JavaDoc conn = ij.startJBMS();
1103            checkToString(conn);
1104        }
1105        
1106        // Now close the connections
1107
clearConnections();
1108    }
1109    
1110    /**
1111     * Check uniqueness of connection strings coming from a
1112     * DataSouce
1113     */

1114    protected static void checkToString(DataSource JavaDoc ds) throws Exception JavaDoc
1115    {
1116        clearConnections();
1117        
1118        int numConnections = 10;
1119        for ( int i = 0 ; i < numConnections ; i++ )
1120        {
1121            Connection JavaDoc conn = ds.getConnection();
1122            checkToString(conn);
1123        }
1124        
1125        clearConnections();
1126    }
1127    
1128    /**
1129     * Check uniqueness of strings with a pooled data source.
1130     * We want to check the PooledConnection as well as the
1131     * underlying physical connection.
1132     */

1133    protected static void checkToString(ConnectionPoolDataSource JavaDoc pds)
1134        throws Exception JavaDoc
1135    {
1136        int numConnections = 10;
1137        
1138        // First get a bunch of pooled connections
1139
// and make sure they're all unique
1140
Hashtable JavaDoc pooledConns = new Hashtable JavaDoc();
1141        for ( int i = 0 ; i < numConnections ; i++ )
1142        {
1143            PooledConnection JavaDoc pc = pds.getPooledConnection();
1144            checkStringFormat(pc);
1145            String JavaDoc str = pc.toString();
1146            if ( pooledConns.get(str) != null )
1147            {
1148                throw new Exception JavaDoc("Pooled connection toString " +
1149                  "value " + str + " is not unique");
1150            }
1151            pooledConns.put(str, pc);
1152        }
1153
1154        // Now check that connections from each of these
1155
// pooled connections have different string values
1156
Iterator JavaDoc it = pooledConns.values().iterator();
1157        clearConnections();
1158        while ( it.hasNext() )
1159        {
1160            PooledConnection JavaDoc pc = (PooledConnection JavaDoc)it.next();
1161            Connection JavaDoc conn = pc.getConnection();
1162            checkToString(conn);
1163        }
1164        clearConnections();
1165        
1166        // Now clear out the pooled connections
1167
it = pooledConns.values().iterator();
1168        while ( it.hasNext() )
1169        {
1170            PooledConnection JavaDoc pc = (PooledConnection JavaDoc)it.next();
1171            pc.close();
1172        }
1173        pooledConns.clear();
1174    }
1175    
1176    /**
1177     * Check uniqueness of strings for an XA data source
1178     */

1179    protected static void checkToString(XADataSource JavaDoc xds) throws Exception JavaDoc
1180    {
1181        int numConnections = 10;
1182        
1183        // First get a bunch of pooled connections
1184
// and make sure they're all unique
1185
Hashtable JavaDoc xaConns = new Hashtable JavaDoc();
1186        for ( int i = 0 ; i < numConnections ; i++ )
1187        {
1188            XAConnection JavaDoc xc = xds.getXAConnection();
1189            checkStringFormat(xc);
1190            String JavaDoc str = xc.toString();
1191            if ( xaConns.get(str) != null )
1192            {
1193                throw new Exception JavaDoc("XA connection toString " +
1194                  "value " + str + " is not unique");
1195            }
1196            xaConns.put(str, xc);
1197        }
1198
1199        // Now check that connections from each of these
1200
// pooled connections have different string values
1201
Iterator JavaDoc it = xaConns.values().iterator();
1202        clearConnections();
1203        while ( it.hasNext() )
1204        {
1205            XAConnection JavaDoc xc = (XAConnection JavaDoc)it.next();
1206            Connection JavaDoc conn = xc.getConnection();
1207            checkToString(conn);
1208        }
1209        clearConnections();
1210        
1211        // Now clear out the pooled connections
1212
it = xaConns.values().iterator();
1213        while ( it.hasNext() )
1214        {
1215            XAConnection JavaDoc xc = (XAConnection JavaDoc)it.next();
1216            xc.close();
1217        }
1218        xaConns.clear();
1219    }
1220
1221    protected void checkConnectionPreClose(String JavaDoc dsName, Connection JavaDoc conn) throws SQLException JavaDoc {
1222        if (dsName.endsWith("DataSource")) {
1223
1224            // see if setting the state is carried over to any future connection from the
1225
// data source object.
1226
try {
1227                conn.setReadOnly(true);
1228            } catch (SQLException JavaDoc sqle) {
1229                // cannot set read-only in an active transaction, & sometimes
1230
// connections are active at this point.
1231
}
1232        }
1233    }
1234
1235    protected void checkStatement(String JavaDoc dsName, Connection JavaDoc conn, Statement JavaDoc s) throws SQLException JavaDoc {
1236
1237        SecurityCheck.inspect(s, "java.sql.Statement");
1238
1239        Connection JavaDoc c1 = s.getConnection();
1240        if (c1 != conn)
1241            System.out.println("FAIL incorrect connection object returned for Statement.getConnection()");
1242
1243        s.addBatch("insert into y values 1");
1244        s.addBatch("insert into y values 2,3");
1245        int[] states = s.executeBatch();
1246        if (states[0] != 1)
1247            System.out.println("FAIL invalid update count for first batch statement");
1248        if (states[1] != 2)
1249            System.out.println("FAIL invalid update count for second batch statement");
1250
1251        ResultSet JavaDoc rs = s.executeQuery("VALUES 1");
1252        if (rs.getStatement() != s)
1253            System.out.println(dsName + " FAIL incorrect Statement object returned for ResultSet.getStatement");
1254        rs.close();
1255        s.close();
1256    }
1257
1258    private static void testDSRequestAuthentication() throws SQLException JavaDoc {
1259
1260        EmbeddedDataSource ds = new EmbeddedDataSource();
1261
1262        System.out.println("DataSource - EMPTY");
1263        dsConnectionRequests(ds);
1264
1265        System.out.println("DataSource - connectionAttributes=databaseName=wombat");
1266        ds.setConnectionAttributes("databaseName=wombat");
1267        dsConnectionRequests(ds);
1268        ds.setConnectionAttributes(null);
1269
1270        System.out.println("DataSource - attributesAsPassword=true");
1271        ds.setAttributesAsPassword(true);
1272        dsConnectionRequests(ds);
1273        ds.setAttributesAsPassword(false);
1274
1275        System.out.println("DataSource - attributesAsPassword=true, connectionAttributes=databaseName=kangaroo");
1276        ds.setAttributesAsPassword(true);
1277        ds.setConnectionAttributes("databaseName=kangaroo");
1278        dsConnectionRequests(ds);
1279        ds.setAttributesAsPassword(false);
1280        ds.setConnectionAttributes(null);
1281
1282        System.out.println("Enable Authentication");
1283        ds.setDatabaseName("wombat");
1284        Connection JavaDoc cadmin = ds.getConnection();
1285        CallableStatement JavaDoc cs = cadmin.prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
1286        cs.setString(1, "derby.user.fred");
1287        cs.setString(2, "wilma");
1288        cs.execute();
1289
1290        cs.setString(1, "derby.authentication.provider");
1291        cs.setString(2, "BUILTIN");
1292        cs.execute();
1293
1294        cs.setString(1, "derby.connection.requireAuthentication");
1295        cs.setString(2, "true");
1296        cs.execute();
1297
1298        cs.close();
1299
1300        cadmin.close();
1301
1302        ds.setShutdownDatabase("shutdown");
1303        try {
1304            ds.getConnection();
1305        } catch (SQLException JavaDoc sqle) {
1306            System.out.println(sqle.getSQLState() + ":" + sqle.getMessage() );
1307        }
1308
1309        ds.setDatabaseName(null);
1310        ds.setShutdownDatabase(null);
1311
1312        System.out.println("AUTHENTICATION NOW ENABLED");
1313
1314        System.out.println("DataSource - attributesAsPassword=true");
1315        ds.setAttributesAsPassword(true);
1316        dsConnectionRequests(ds);
1317        ds.setAttributesAsPassword(false);
1318
1319        // ensure the DS property password is not treated as a set of attributes.
1320
System.out.println("DataSource - attributesAsPassword=true, user=fred, password=databaseName=wombat;password=wilma");
1321        ds.setAttributesAsPassword(true);
1322        ds.setUser("fred");
1323        ds.setPassword("databaseName=wombat;password=wilma");
1324        dsConnectionRequests(ds);
1325        ds.setAttributesAsPassword(false);
1326        ds.setUser(null);
1327        ds.setPassword(null);
1328        ds = null;
1329
1330        // now with ConnectionPoolDataSource
1331
EmbeddedConnectionPoolDataSource cpds = new EmbeddedConnectionPoolDataSource();
1332        System.out.println("ConnectionPoolDataSource - EMPTY");
1333        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1334
1335        System.out.println("ConnectionPoolDataSource - connectionAttributes=databaseName=wombat");
1336        cpds.setConnectionAttributes("databaseName=wombat");
1337        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1338        cpds.setConnectionAttributes(null);
1339
1340        System.out.println("ConnectionPoolDataSource - attributesAsPassword=true");
1341        cpds.setAttributesAsPassword(true);
1342        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1343        cpds.setAttributesAsPassword(false);
1344        
1345        // ensure the DS property password is not treated as a set of attributes.
1346
System.out.println("ConnectionPoolDataSource - attributesAsPassword=true, user=fred, password=databaseName=wombat;password=wilma");
1347        cpds.setAttributesAsPassword(true);
1348        cpds.setUser("fred");
1349        cpds.setPassword("databaseName=wombat;password=wilma");
1350        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1351        cpds.setAttributesAsPassword(false);
1352        cpds.setUser(null);
1353        cpds.setPassword(null);
1354        cpds = null;
1355
1356        // now with XADataSource
1357
EmbeddedXADataSource xads = new EmbeddedXADataSource();
1358        System.out.println("XADataSource - EMPTY");
1359        dsConnectionRequests((XADataSource JavaDoc) xads);
1360
1361        System.out.println("XADataSource - databaseName=wombat");
1362        xads.setDatabaseName("wombat");
1363        dsConnectionRequests((XADataSource JavaDoc) xads);
1364        xads.setDatabaseName(null);
1365
1366        System.out.println("XADataSource - connectionAttributes=databaseName=wombat");
1367        xads.setConnectionAttributes("databaseName=wombat");
1368        dsConnectionRequests((XADataSource JavaDoc) xads);
1369        xads.setConnectionAttributes(null);
1370
1371        System.out.println("XADataSource - attributesAsPassword=true");
1372        xads.setAttributesAsPassword(true);
1373        dsConnectionRequests((XADataSource JavaDoc) xads);
1374        xads.setAttributesAsPassword(false);
1375
1376        System.out.println("XADataSource - databaseName=wombat, attributesAsPassword=true");
1377        xads.setDatabaseName("wombat");
1378        xads.setAttributesAsPassword(true);
1379        dsConnectionRequests((XADataSource JavaDoc) xads);
1380        xads.setAttributesAsPassword(false);
1381        xads.setDatabaseName(null);
1382    }
1383
1384    /**
1385     * Check that database name set using setConnectionAttributes is not used
1386     * by ClientDataSource. This method tests DERBY-1130.
1387     *
1388     * @throws SQLException
1389     */

1390    private static void testClientDSConnectionAttributes() throws SQLException JavaDoc {
1391
1392        ClientDataSource ds = new ClientDataSource();
1393
1394        System.out.println("DataSource - EMPTY");
1395        dsConnectionRequests(ds);
1396
1397        System.out.println("DataSource - connectionAttributes=databaseName=wombat");
1398        ds.setConnectionAttributes("databaseName=wombat");
1399        dsConnectionRequests(ds);
1400        ds.setConnectionAttributes(null);
1401        
1402        // Test that database name specified in connection attributes is not used
1403
System.out.println("DataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo");
1404        ds.setConnectionAttributes("databaseName=kangaroo");
1405        ds.setDatabaseName("wombat");
1406        dsConnectionRequests(ds);
1407        ds.setConnectionAttributes(null);
1408        ds.setDatabaseName(null);
1409        
1410        // now with ConnectionPoolDataSource
1411
ClientConnectionPoolDataSource cpds = new ClientConnectionPoolDataSource();
1412        System.out.println("ConnectionPoolDataSource - EMPTY");
1413        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1414
1415        System.out.println("ConnectionPoolDataSource - connectionAttributes=databaseName=wombat");
1416        cpds.setConnectionAttributes("databaseName=wombat");
1417        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1418        cpds.setConnectionAttributes(null);
1419        
1420        // Test that database name specified in connection attributes is not used
1421
System.out.println("ConnectionPoolDataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo");
1422        cpds.setConnectionAttributes("databaseName=kangaroo");
1423        cpds.setDatabaseName("wombat");
1424        dsConnectionRequests((ConnectionPoolDataSource JavaDoc)cpds);
1425        cpds.setConnectionAttributes(null);
1426        cpds.setDatabaseName(null);
1427        
1428        // now with XADataSource
1429
ClientXADataSource xads = new ClientXADataSource();
1430        System.out.println("XADataSource - EMPTY");
1431        dsConnectionRequests((XADataSource JavaDoc) xads);
1432
1433        System.out.println("XADataSource - connectionAttributes=databaseName=wombat");
1434        xads.setConnectionAttributes("databaseName=wombat");
1435        dsConnectionRequests((XADataSource JavaDoc) xads);
1436        xads.setConnectionAttributes(null);
1437        
1438        // Test that database name specified in connection attributes is not used
1439
System.out.println("XADataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo");
1440        xads.setConnectionAttributes("databaseName=kangaroo");
1441        xads.setDatabaseName("wombat");
1442        dsConnectionRequests((XADataSource JavaDoc) xads);
1443        xads.setConnectionAttributes(null);
1444        xads.setDatabaseName(null);
1445    }
1446    
1447    private static void dsConnectionRequests(DataSource JavaDoc ds) {
1448        
1449        SecurityCheck.inspect(ds, "javax.sql.DataSource");
1450        
1451        try {
1452            Connection JavaDoc c1 = ds.getConnection();
1453            System.out.println(" getConnection() - OK");
1454            c1.close();
1455        } catch (SQLException JavaDoc sqle) {
1456            System.out.println(" getConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1457        }
1458
1459        try {
1460            Connection JavaDoc c1 = ds.getConnection(null, null);
1461            System.out.println(" getConnection(null, null) - OK");
1462            c1.close();
1463        } catch (SQLException JavaDoc sqle) {
1464            System.out.println(" getConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1465        }
1466        try {
1467            Connection JavaDoc c1 = ds.getConnection("fred", null);
1468            System.out.println(" getConnection(fred, null) - OK");
1469            c1.close();
1470        } catch (SQLException JavaDoc sqle) {
1471            System.out.println(" getConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1472        }
1473        try {
1474            Connection JavaDoc c1 = ds.getConnection("fred", "wilma");
1475            System.out.println(" getConnection(fred, wilma) - OK");
1476            c1.close();
1477        } catch (SQLException JavaDoc sqle) {
1478            System.out.println(" getConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1479        }
1480        try {
1481            Connection JavaDoc c1 = ds.getConnection(null, "wilma");
1482            System.out.println(" getConnection(null, wilma) - OK");
1483            c1.close();
1484        } catch (SQLException JavaDoc sqle) {
1485            System.out.println(" getConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1486        }
1487        try {
1488            Connection JavaDoc c1 = ds.getConnection(null, "databaseName=wombat");
1489            System.out.println(" getConnection(null, databaseName=wombat) - OK");
1490            c1.close();
1491        } catch (SQLException JavaDoc sqle) {
1492            System.out.println(" getConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1493        }
1494        try {
1495            Connection JavaDoc c1 = ds.getConnection("fred", "databaseName=wombat");
1496            System.out.println(" getConnection(fred, databaseName=wombat) - OK");
1497            c1.close();
1498        } catch (SQLException JavaDoc sqle) {
1499            System.out.println(" getConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1500        }
1501        try {
1502            Connection JavaDoc c1 = ds.getConnection("fred", "databaseName=wombat;password=wilma");
1503            System.out.println(" getConnection(fred, databaseName=wombat;password=wilma) - OK");
1504            c1.close();
1505        } catch (SQLException JavaDoc sqle) {
1506            System.out.println(" getConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1507        }
1508        try {
1509            Connection JavaDoc c1 = ds.getConnection("fred", "databaseName=wombat;password=betty");
1510            System.out.println(" getConnection(fred, databaseName=wombat;password=betty) - OK");
1511            c1.close();
1512        } catch (SQLException JavaDoc sqle) {
1513            System.out.println(" getConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1514        }
1515    }
1516    private static void dsConnectionRequests(ConnectionPoolDataSource JavaDoc ds) {
1517        try {
1518            PooledConnection JavaDoc pc = ds.getPooledConnection();
1519            System.out.println(" getPooledConnection() - OK");
1520            Connection JavaDoc c1 = pc.getConnection();
1521            c1.close();
1522            pc.close();
1523        } catch (SQLException JavaDoc sqle) {
1524            System.out.println(" getPooledConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1525        }
1526
1527        try {
1528            PooledConnection JavaDoc pc = ds.getPooledConnection(null, null);
1529            System.out.println(" getPooledConnection(null, null) - OK");
1530            Connection JavaDoc c1 = pc.getConnection();
1531            c1.close();
1532        } catch (SQLException JavaDoc sqle) {
1533            System.out.println(" getPooledConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1534        }
1535        try {
1536            PooledConnection JavaDoc pc = ds.getPooledConnection("fred", null);
1537            System.out.println(" getPooledConnection(fred, null) - OK");
1538            Connection JavaDoc c1 = pc.getConnection();
1539            c1.close();
1540            pc.close();
1541        } catch (SQLException JavaDoc sqle) {
1542            System.out.println(" getPooledConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1543        }
1544        try {
1545            PooledConnection JavaDoc pc = ds.getPooledConnection("fred", "wilma");
1546            System.out.println(" getPooledConnection(fred, wilma) - OK");
1547            Connection JavaDoc c1 = pc.getConnection();
1548            c1.close();
1549            pc.close();
1550        } catch (SQLException JavaDoc sqle) {
1551            System.out.println(" getPooledConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1552        }
1553        try {
1554            PooledConnection JavaDoc pc = ds.getPooledConnection(null, "wilma");
1555            System.out.println(" getPooledConnection(null, wilma) - OK");
1556            Connection JavaDoc c1 = pc.getConnection();
1557            c1.close();
1558            pc.close();
1559        } catch (SQLException JavaDoc sqle) {
1560            System.out.println(" getPooledConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1561        }
1562        try {
1563            PooledConnection JavaDoc pc = ds.getPooledConnection(null, "databaseName=wombat");
1564            System.out.println(" getPooledConnection(null, databaseName=wombat) - OK");
1565            Connection JavaDoc c1 = pc.getConnection();
1566            c1.close();
1567            pc.close();
1568        } catch (SQLException JavaDoc sqle) {
1569            System.out.println(" getPooledConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1570        }
1571        try {
1572            PooledConnection JavaDoc pc = ds.getPooledConnection("fred", "databaseName=wombat");
1573            System.out.println(" getPooledConnection(fred, databaseName=wombat) - OK");
1574            Connection JavaDoc c1 = pc.getConnection();
1575            c1.close();
1576            pc.close();
1577        } catch (SQLException JavaDoc sqle) {
1578            System.out.println(" getPooledConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1579        }
1580        try {
1581            PooledConnection JavaDoc pc = ds.getPooledConnection("fred", "databaseName=wombat;password=wilma");
1582            System.out.println(" getPooledConnection(fred, databaseName=wombat;password=wilma) - OK");
1583            Connection JavaDoc c1 = pc.getConnection();
1584            c1.close();
1585            pc.close();
1586        } catch (SQLException JavaDoc sqle) {
1587            System.out.println(" getPooledConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1588        }
1589        try {
1590            PooledConnection JavaDoc pc = ds.getPooledConnection("fred", "databaseName=wombat;password=betty");
1591            System.out.println(" getPooledConnection(fred, databaseName=wombat;password=betty) - OK");
1592            Connection JavaDoc c1 = pc.getConnection();
1593            c1.close();
1594            pc.close();
1595        } catch (SQLException JavaDoc sqle) {
1596            System.out.println(" getPooledConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1597        }
1598    }
1599    private static void dsConnectionRequests(XADataSource JavaDoc ds) {
1600        try {
1601            XAConnection JavaDoc xc = ds.getXAConnection();
1602            System.out.println(" getXAConnection() - OK");
1603            Connection JavaDoc c1 = xc.getConnection();
1604            c1.close();
1605            xc.close();
1606        } catch (SQLException JavaDoc sqle) {
1607            System.out.println(" getXAConnection() - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1608        }
1609
1610        try {
1611            XAConnection JavaDoc xc = ds.getXAConnection(null, null);
1612            System.out.println(" getXAConnection(null, null) - OK");
1613            Connection JavaDoc c1 = xc.getConnection();
1614            c1.close();
1615        } catch (SQLException JavaDoc sqle) {
1616            System.out.println(" getXAConnection(null, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1617        }
1618        try {
1619            XAConnection JavaDoc xc = ds.getXAConnection("fred", null);
1620            System.out.println(" getXAConnection(fred, null) - OK");
1621            Connection JavaDoc c1 = xc.getConnection();
1622            c1.close();
1623            xc.close();
1624        } catch (SQLException JavaDoc sqle) {
1625            System.out.println(" getXAConnection(fred, null) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1626        }
1627        try {
1628            XAConnection JavaDoc xc = ds.getXAConnection("fred", "wilma");
1629            System.out.println(" getXAConnection(fred, wilma) - OK");
1630            Connection JavaDoc c1 = xc.getConnection();
1631            c1.close();
1632            xc.close();
1633        } catch (SQLException JavaDoc sqle) {
1634            System.out.println(" getXAConnection(fred, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1635        }
1636        try {
1637            XAConnection JavaDoc xc = ds.getXAConnection(null, "wilma");
1638            System.out.println(" getXAConnection(null, wilma) - OK");
1639            Connection JavaDoc c1 = xc.getConnection();
1640            c1.close();
1641            xc.close();
1642        } catch (SQLException JavaDoc sqle) {
1643            System.out.println(" getXAConnection(null, wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1644        }
1645        try {
1646            XAConnection JavaDoc xc = ds.getXAConnection(null, "databaseName=wombat");
1647            System.out.println(" getXAConnection(null, databaseName=wombat) - OK");
1648            Connection JavaDoc c1 = xc.getConnection();
1649            c1.close();
1650            xc.close();
1651        } catch (SQLException JavaDoc sqle) {
1652            System.out.println(" getXAConnection(null, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1653        }
1654        try {
1655            XAConnection JavaDoc xc = ds.getXAConnection("fred", "databaseName=wombat");
1656            System.out.println(" getXAConnection(fred, databaseName=wombat) - OK");
1657            Connection JavaDoc c1 = xc.getConnection();
1658            c1.close();
1659            xc.close();
1660        } catch (SQLException JavaDoc sqle) {
1661            System.out.println(" getXAConnection(fred, databaseName=wombat) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1662        }
1663        try {
1664            XAConnection JavaDoc xc = ds.getXAConnection("fred", "databaseName=wombat;password=wilma");
1665            System.out.println(" getXAConnection(fred, databaseName=wombat;password=wilma) - OK");
1666            Connection JavaDoc c1 = xc.getConnection();
1667            c1.close();
1668            xc.close();
1669        } catch (SQLException JavaDoc sqle) {
1670            System.out.println(" getXAConnection(fred, databaseName=wombat;password=wilma) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1671        }
1672        try {
1673            XAConnection JavaDoc xc = ds.getXAConnection("fred", "databaseName=wombat;password=betty");
1674            System.out.println(" getXAConnection(fred, databaseName=wombat;password=betty) - OK");
1675            Connection JavaDoc c1 = xc.getConnection();
1676            c1.close();
1677            xc.close();
1678        } catch (SQLException JavaDoc sqle) {
1679            System.out.println(" getXAConnection(fred, databaseName=wombat;password=betty) - " + sqle.getSQLState() + ":" + sqle.getMessage() );
1680        }
1681    }
1682
1683    protected Xid JavaDoc getXid(int xid, byte b1, byte b2) {
1684        return new cdsXid(xid, b1, b2);
1685    }
1686
1687    public static String JavaDoc translateIso(int iso)
1688    {
1689        switch(iso)
1690        {
1691        case Connection.TRANSACTION_READ_COMMITTED: return "READ_COMMITTED";
1692        case Connection.TRANSACTION_SERIALIZABLE: return "SERIALIZABLE";
1693        case Connection.TRANSACTION_REPEATABLE_READ: return "REPEATABLE_READ";
1694        case Connection.TRANSACTION_READ_UNCOMMITTED: return "READ_UNCOMMITTED";
1695        }
1696        return "unknown";
1697    }
1698
1699    /**
1700        When a connection is being pooled, the underlying JDBC embedded
1701        connection object is re-used. As each application gets a new
1702        Connection object, that is really a wrapper around the old connection
1703        it should reset any connection spoecific state on the embedded connection
1704        object.
1705    */

1706    private static void testPoolReset(String JavaDoc type, PooledConnection JavaDoc pc) throws SQLException JavaDoc
1707    {
1708        System.out.println("Start testPoolReset " + type);
1709        testPoolResetWork("C", pc.getConnection());
1710        testPoolResetWork("", pc.getConnection());
1711        testPoolResetWork("D", pc.getConnection());
1712
1713        pc.close();
1714        System.out.println("End testPoolReset " + type);
1715    }
1716
1717    private static void testPoolResetWork(String JavaDoc tableAction, Connection JavaDoc conn) throws SQLException JavaDoc
1718    {
1719        Statement JavaDoc s = conn.createStatement();
1720        if (tableAction.equals("C"))
1721        {
1722            s.execute("CREATE TABLE testPoolResetWork (id int generated always as identity, name varchar(25))");
1723        }
1724
1725        ResultSet JavaDoc rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()");
1726        rs.next();
1727        String JavaDoc val = rs.getString(1);
1728        if (!rs.wasNull() || (val != null))
1729            System.out.println("FAIL - initial call to IDENTITY_VAL_LOCAL is not NULL!" + val);
1730        rs.close();
1731
1732        s.executeUpdate("INSERT INTO testPoolResetWork(name) values ('derby-222')");
1733
1734        rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()");
1735        rs.next();
1736        val = rs.getString(1);
1737        System.out.println("IDENTITY_VAL_LOCAL=" + val);
1738        rs.close();
1739
1740        if (tableAction.equals("D"))
1741        {
1742            s.execute("DROP TABLE testPoolResetWork");
1743        }
1744
1745
1746        s.close();
1747        conn.close();
1748
1749    }
1750
1751    public void testJira95ds(Connection JavaDoc conn, String JavaDoc dbName) throws SQLException JavaDoc
1752    {
1753        System.out.print("\ntesting jira 95 for DataSource");
1754        EmbeddedDataSource ds = new EmbeddedDataSource();
1755        ds.setDatabaseName( dbName);
1756        Connection JavaDoc conn1 = ds.getConnection();
1757        conn1.close();
1758    }
1759    
1760    public void testJira95xads(Connection JavaDoc conn, String JavaDoc dbName) throws SQLException JavaDoc
1761    {
1762        System.out.print("testing jira 95 for XADataSource");
1763        EmbeddedXADataSource dxs = new EmbeddedXADataSource();
1764        dxs.setDatabaseName(dbName);
1765        Connection JavaDoc conn2 = dxs.getXAConnection().getConnection();
1766        conn2.close();
1767    }
1768
1769
1770}
1771class cdsXid implements Xid JavaDoc, Serializable JavaDoc
1772{
1773  private static final long serialVersionUID = 64467338100036L;
1774
1775    private final int format_id;
1776    private byte[] global_id;
1777    private byte[] branch_id;
1778
1779
1780    cdsXid(int xid, byte b1, byte b2)
1781    {
1782        format_id = xid;
1783        global_id = new byte[Xid.MAXGTRIDSIZE];
1784        branch_id = new byte[Xid.MAXBQUALSIZE];
1785
1786        for (int i = 0; i < global_id.length; i++) {
1787            global_id[i] = b1;
1788        }
1789
1790        for (int i = 0; i < branch_id.length; i++) {
1791            branch_id[i] = b2;
1792        }
1793    }
1794
1795    /**
1796     * Obtain the format id part of the Xid.
1797     * <p>
1798     *
1799     * @return Format identifier. O means the OSI CCR format.
1800     **/

1801    public int getFormatId()
1802    {
1803        return(format_id);
1804    }
1805
1806    /**
1807     * Obtain the global transaction identifier part of XID as an array of
1808     * bytes.
1809     * <p>
1810     *
1811     * @return A byte array containing the global transaction identifier.
1812     **/

1813    public byte[] getGlobalTransactionId()
1814    {
1815        return(global_id);
1816    }
1817
1818    /**
1819     * Obtain the transaction branch qualifier part of the Xid in a byte array.
1820     * <p>
1821     *
1822     * @return A byte array containing the branch qualifier of the transaction.
1823     **/

1824    public byte[] getBranchQualifier()
1825    {
1826        return(branch_id);
1827    }
1828
1829
1830}
1831
1832class EventCatcher implements ConnectionEventListener JavaDoc
1833{
1834    private final int catcher;
1835
1836    EventCatcher(int which) {
1837        catcher=which;
1838    }
1839
1840    // ConnectionEventListener methods
1841
public void connectionClosed(ConnectionEvent JavaDoc event)
1842    {
1843        System.out.print("EVENT("+catcher+"):connectionClosed");
1844        SQLException JavaDoc sqle = event.getSQLException();
1845        if (sqle != null)
1846            System.out.print(" SQLSTATE=" + sqle.getSQLState());
1847        System.out.println("");
1848    }
1849
1850    public void connectionErrorOccurred(ConnectionEvent JavaDoc event)
1851    {
1852        System.out.print("EVENT("+catcher+"):connectionErrorOccurred");
1853        SQLException JavaDoc sqle = event.getSQLException();
1854        if (sqle != null)
1855            System.out.print(" SQLSTATE=" + sqle.getSQLState());
1856        System.out.println("");
1857
1858    }
1859
1860}
1861
Popular Tags