Source Code Cross Referenced for checkDataSource.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » functionTests » tests » jdbcapi » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » db derby 10.2 » org.apache.derbyTesting.functionTests.tests.jdbcapi 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:
0003:           Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.checkDataSource
0004:
0005:           Licensed to the Apache Software Foundation (ASF) under one or more
0006:           contributor license agreements.  See the NOTICE file distributed with
0007:           this work for additional information regarding copyright ownership.
0008:           The ASF licenses this file to You under the Apache License, Version 2.0
0009:           (the "License"); you may not use this file except in compliance with
0010:           the License.  You may obtain a copy of the License at
0011:
0012:              http://www.apache.org/licenses/LICENSE-2.0
0013:
0014:           Unless required by applicable law or agreed to in writing, software
0015:           distributed under the License is distributed on an "AS IS" BASIS,
0016:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017:           See the License for the specific language governing permissions and
0018:           limitations under the License.
0019:
0020:         */
0021:
0022:        package org.apache.derbyTesting.functionTests.tests.jdbcapi;
0023:
0024:        import java.io.Serializable;
0025:        import java.sql.CallableStatement;
0026:        import java.sql.Connection;
0027:        import java.sql.DriverManager;
0028:        import java.sql.PreparedStatement;
0029:        import java.sql.ResultSet;
0030:        import java.sql.SQLException;
0031:        import java.sql.Statement;
0032:        import java.util.Hashtable;
0033:        import java.util.Iterator;
0034:        import java.util.Properties;
0035:
0036:        import javax.sql.ConnectionEvent;
0037:        import javax.sql.ConnectionEventListener;
0038:        import javax.sql.ConnectionPoolDataSource;
0039:        import javax.sql.DataSource;
0040:        import javax.sql.PooledConnection;
0041:        import javax.sql.XAConnection;
0042:        import javax.sql.XADataSource;
0043:        import javax.transaction.xa.XAException;
0044:        import javax.transaction.xa.XAResource;
0045:        import javax.transaction.xa.Xid;
0046:
0047:        import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;
0048:        import org.apache.derby.jdbc.EmbeddedDataSource;
0049:        import org.apache.derby.jdbc.EmbeddedXADataSource;
0050:        import org.apache.derby.jdbc.ClientConnectionPoolDataSource;
0051:        import org.apache.derby.jdbc.ClientDataSource;
0052:        import org.apache.derby.jdbc.ClientXADataSource;
0053:
0054:        import org.apache.derby.tools.JDBCDisplayUtil;
0055:        import org.apache.derby.tools.ij;
0056:        import org.apache.derbyTesting.functionTests.util.SecurityCheck;
0057:        import org.apache.derbyTesting.functionTests.util.TestUtil;
0058:        import org.apache.oro.text.perl.Perl5Util;
0059:
0060:        /**
0061:         * Test the various embedded DataSource implementations of Derby.
0062:         * 
0063:         * Performs SecurityCheck analysis on the JDBC objects returned.
0064:         * This is because this test returns to the client a number of
0065:         * different implementations of Connection, Statement etc.
0066:         * 
0067:         * @see org.apache.derbyTesting.functionTests.util.SecurityCheck
0068:         *
0069:         */
0070:        public class checkDataSource {
0071:            // Only test connection toString values for embedded.
0072:            // Client connection toString values are not correlated at this time and just 
0073:            // use default toString
0074:            // These tests are exempted from other frameworks
0075:            private boolean testConnectionToString = TestUtil
0076:                    .isEmbeddedFramework();
0077:
0078:            // Only embedded supports SimpleDataSource (JSR169).  
0079:            // These tests are exempted from other frameworks
0080:            private boolean testSimpleDataSource = TestUtil
0081:                    .isEmbeddedFramework();
0082:
0083:            /**
0084:             * A hashtable of opened connections.  This is used when checking to
0085:             * make sure connection strings are unique; we need to make sure all
0086:             * the connections are closed when we are done, so they are stored
0087:             * in this hashtable
0088:             */
0089:            protected static Hashtable conns = new Hashtable();
0090:
0091:            /**
0092:             * This is a utility that knows how to do pattern matching.  Used
0093:             * in checking the format of a connection string
0094:             */
0095:            protected static Perl5Util p5u = new Perl5Util();
0096:
0097:            /** The expected format of a connection string. In English:
0098:             * "<classname>@<hashcode> (XID=<xid>), (SESSION = <sessionid>),
0099:             *  (DATABASE=<dbname>), (DRDAID = <drdaid>)"
0100:             */
0101:            private static final String CONNSTRING_FORMAT = "\\S+@[0-9]+ "
0102:                    + "\\(XID = .*\\), \\(SESSIONID = [0-9]+\\), "
0103:                    + "\\(DATABASE = [A-Za-z]+\\), \\(DRDAID = .+\\)";
0104:
0105:            /**
0106:             * Hang onto the SecurityCheck class while running the
0107:             * tests so that it is not garbage collected during the
0108:             * test and lose the information it has collected.
0109:             */
0110:            private final Object nogc = SecurityCheck.class;
0111:
0112:            public static void main(String[] args) throws Exception {
0113:
0114:                try {
0115:                    new checkDataSource().runTest(args);
0116:
0117:                    // Print a report on System.out of the issues
0118:                    // found with the security checks. 
0119:                    SecurityCheck.report();
0120:
0121:                } catch (Exception e) {
0122:                    e.printStackTrace();
0123:                    throw e;
0124:                }
0125:                System.out.println("Completed checkDataSource");
0126:
0127:            }
0128:
0129:            public checkDataSource() {
0130:            }
0131:
0132:            protected void runTest(String[] args) throws Exception {
0133:
0134:                // Check the returned type of the JDBC Connections.
0135:                ij.getPropertyArg(args);
0136:                Connection dmc = ij.startJBMS();
0137:
0138:                dmc.createStatement().executeUpdate("create table y(i int)");
0139:
0140:                dmc
0141:                        .createStatement()
0142:                        .executeUpdate(
0143:                                "create procedure checkConn2(in dsname varchar(20)) "
0144:                                        + "parameter style java language java modifies SQL DATA "
0145:                                        + "external name 'org.apache.derbyTesting.functionTests.tests.jdbcapi."
0146:                                        + this .getNestedMethodName() + "'");
0147:                CallableStatement cs = dmc.prepareCall("call checkConn2(?)");
0148:                cs.setString(1, "Nested");
0149:                cs.execute();
0150:
0151:                checkConnection("DriverManager ", dmc);
0152:                if (testConnectionToString)
0153:                    checkJBMSToString();
0154:
0155:                Properties attrs = new Properties();
0156:                attrs.setProperty("databaseName", "wombat");
0157:                DataSource dscs = TestUtil.getDataSource(attrs);
0158:
0159:                if (testConnectionToString)
0160:                    checkToString(dscs);
0161:
0162:                DataSource ds = dscs;
0163:
0164:                checkConnection("DataSource", ds.getConnection());
0165:
0166:                DataSource dssimple = null;
0167:                if (testSimpleDataSource) {
0168:                    dssimple = TestUtil.getSimpleDataSource(attrs);
0169:                    ds = dssimple;
0170:                    checkConnection("SimpleDataSource", ds.getConnection());
0171:                }
0172:
0173:                ConnectionPoolDataSource dsp = TestUtil
0174:                        .getConnectionPoolDataSource(attrs);
0175:
0176:                if (testConnectionToString)
0177:                    checkToString(dsp);
0178:
0179:                PooledConnection pc = dsp.getPooledConnection();
0180:                SecurityCheck.inspect(pc, "javax.sql.PooledConnection");
0181:                pc.addConnectionEventListener(new EventCatcher(1));
0182:
0183:                checkConnection("ConnectionPoolDataSource", pc.getConnection());
0184:                checkConnection("ConnectionPoolDataSource", pc.getConnection());
0185:
0186:                // BUG 4471 - check outstanding updates are rolled back.
0187:                Connection c1 = pc.getConnection();
0188:
0189:                Statement s = c1.createStatement();
0190:
0191:                s.executeUpdate("create table t (i int)");
0192:
0193:                s.executeUpdate("insert into t values(1)");
0194:
0195:                c1.setAutoCommit(false);
0196:
0197:                // this update should be rolled back
0198:                s.executeUpdate("insert into t values(2)");
0199:
0200:                c1 = pc.getConnection();
0201:
0202:                ResultSet rs = c1.createStatement().executeQuery(
0203:                        "select count(*) from t");
0204:                rs.next();
0205:                int count = rs.getInt(1);
0206:
0207:                System.out
0208:                        .println(count == 1 ? "Changes rolled back OK in auto closed pooled connection"
0209:                                : ("FAIL changes committed in in auto closed pooled connection - " + count));
0210:
0211:                c1.close();
0212:
0213:                // check connection objects are closed once connection is closed
0214:                try {
0215:                    rs.next();
0216:                    System.out
0217:                            .println("FAIL - ResultSet is open for a closed connection obtained from PooledConnection");
0218:                } catch (SQLException sqle) {
0219:                    System.out.println("expected SQL Exception: ("
0220:                            + sqle.getSQLState() + ") " + sqle.getMessage());
0221:                }
0222:
0223:                try {
0224:                    s.executeUpdate("update t set i = 1");
0225:                    System.out
0226:                            .println("FAIL - Statement is open for a closed connection obtained from PooledConnection");
0227:                } catch (SQLException sqle) {
0228:                    System.out.println("expected SQL Exception: ("
0229:                            + sqle.getSQLState() + ") " + sqle.getMessage());
0230:                }
0231:
0232:                pc.close();
0233:                pc = null;
0234:
0235:                testPoolReset("ConnectionPoolDataSource", dsp
0236:                        .getPooledConnection());
0237:
0238:                XADataSource dsx = TestUtil.getXADataSource(attrs);
0239:                if (testConnectionToString)
0240:                    checkToString(dsx);
0241:
0242:                XAConnection xac = dsx.getXAConnection();
0243:                SecurityCheck.inspect(xac, "javax.sql.XAConnection");
0244:                xac.addConnectionEventListener(new EventCatcher(3));
0245:
0246:                checkConnection("XADataSource", xac.getConnection());
0247:
0248:                // BUG 4471 - check outstanding updates are rolled back wi XAConnection.
0249:                c1 = xac.getConnection();
0250:
0251:                s = c1.createStatement();
0252:
0253:                s.executeUpdate("insert into t values(1)");
0254:
0255:                c1.setAutoCommit(false);
0256:
0257:                // this update should be rolled back
0258:                s.executeUpdate("insert into t values(2)");
0259:
0260:                c1 = xac.getConnection();
0261:
0262:                rs = c1.createStatement()
0263:                        .executeQuery("select count(*) from t");
0264:                rs.next();
0265:                count = rs.getInt(1);
0266:                rs.close();
0267:
0268:                System.out
0269:                        .println(count == 2 ? "Changes rolled back OK in auto closed local XAConnection"
0270:                                : ("FAIL changes committed in in auto closed pooled connection - " + count));
0271:
0272:                c1.close();
0273:                xac.close();
0274:                xac = null;
0275:
0276:                testPoolReset("XADataSource", dsx.getXAConnection());
0277:
0278:                try {
0279:                    TestUtil.getConnection("", "shutdown=true");
0280:                } catch (SQLException sqle) {
0281:                    JDBCDisplayUtil.ShowSQLException(System.out, sqle);
0282:                }
0283:
0284:                dmc = ij.startJBMS();
0285:
0286:                cs = dmc.prepareCall("call checkConn2(?)");
0287:                SecurityCheck.inspect(cs, "java.sql.CallableStatement");
0288:                cs.setString(1, "Nested");
0289:                cs.execute();
0290:
0291:                checkConnection("DriverManager ", dmc);
0292:
0293:                // reset ds back to the Regular DataSource
0294:                ds = dscs;
0295:                checkConnection("DataSource", ds.getConnection());
0296:
0297:                // and back to EmbeddedSimpleDataSource
0298:                if (TestUtil.isEmbeddedFramework()) {
0299:                    // JSR169 (SimpleDataSource) is only available on embedded.
0300:                    ds = dssimple;
0301:                    checkConnection("EmbeddedSimpleDataSource", dssimple
0302:                            .getConnection());
0303:                }
0304:
0305:                pc = dsp.getPooledConnection();
0306:                pc.addConnectionEventListener(new EventCatcher(2));
0307:                checkConnection("ConnectionPoolDataSource", pc.getConnection());
0308:                checkConnection("ConnectionPoolDataSource", pc.getConnection());
0309:
0310:                // test "local" XAConnections
0311:                xac = dsx.getXAConnection();
0312:                xac.addConnectionEventListener(new EventCatcher(4));
0313:                checkConnection("XADataSource", xac.getConnection());
0314:                checkConnection("XADataSource", xac.getConnection());
0315:                xac.close();
0316:
0317:                // test "global" XAConnections
0318:                xac = dsx.getXAConnection();
0319:                xac.addConnectionEventListener(new EventCatcher(5));
0320:                XAResource xar = xac.getXAResource();
0321:                SecurityCheck.inspect(xar, "javax.transaction.xa.XAResource");
0322:                Xid xid = new cdsXid(1, (byte) 35, (byte) 47);
0323:                xar.start(xid, XAResource.TMNOFLAGS);
0324:                Connection xacc = xac.getConnection();
0325:                xacc.close();
0326:                checkConnection("Global XADataSource", xac.getConnection());
0327:                checkConnection("Global XADataSource", xac.getConnection());
0328:
0329:                xar.end(xid, XAResource.TMSUCCESS);
0330:
0331:                checkConnection("Switch to local XADataSource", xac
0332:                        .getConnection());
0333:                checkConnection("Switch to local XADataSource", xac
0334:                        .getConnection());
0335:
0336:                Connection backtoGlobal = xac.getConnection();
0337:
0338:                xar.start(xid, XAResource.TMJOIN);
0339:                checkConnection("Switch to global XADataSource", backtoGlobal);
0340:                checkConnection("Switch to global XADataSource", xac
0341:                        .getConnection());
0342:                xar.end(xid, XAResource.TMSUCCESS);
0343:                xar.commit(xid, true);
0344:
0345:                xac.close();
0346:
0347:                // now some explicit tests for how connection state behaves
0348:                // when switching between global transactions and local
0349:                // and setting connection state.
0350:                // some of this is already tested in simpleDataSource and checkDataSource
0351:                // but I want to make sure I cover all situations. (djd)
0352:                xac = dsx.getXAConnection();
0353:                xac.addConnectionEventListener(new EventCatcher(6));
0354:                xar = xac.getXAResource();
0355:                xid = new cdsXid(1, (byte) 93, (byte) 103);
0356:
0357:                // series 1 - Single connection object
0358:                Connection cs1 = xac.getConnection();
0359:                printState("initial local", cs1);
0360:                xar.start(xid, XAResource.TMNOFLAGS);
0361:                printState("initial  X1", cs1);
0362:                cs1
0363:                        .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
0364:                cs1.setReadOnly(true);
0365:                setHoldability(cs1, false);
0366:                printState("modified X1", cs1);
0367:                xar.end(xid, XAResource.TMSUCCESS);
0368:                // the underlying local transaction/connection must pick up the
0369:                // state of the Connection handle cs1
0370:                printState("modified local", cs1);
0371:
0372:                cs1
0373:                        .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
0374:                cs1.setReadOnly(false);
0375:                setHoldability(cs1, false);
0376:
0377:                printState("reset local", cs1);
0378:
0379:                // now re-join the transaction, should pick up the read-only
0380:                // and isolation level from the transaction,
0381:                // holdability remains that of this handle.
0382:                xar.start(xid, XAResource.TMJOIN);
0383:                printState("re-join X1", cs1);
0384:                xar.end(xid, XAResource.TMSUCCESS);
0385:
0386:                // should be the same as the reset local
0387:                printState("back to local (same as reset)", cs1);
0388:
0389:                // test suspend/resume
0390:                // now re-join the transaction, should pick up the read-only
0391:                // and isolation level from the transaction,
0392:                // holdability remains that of this handle.
0393:                xar.start(xid, XAResource.TMJOIN);
0394:                printState("re-join X1 second time", cs1);
0395:
0396:                xar.end(xid, XAResource.TMSUSPEND);
0397:                printState("local after suspend", cs1);
0398:
0399:                xar.start(xid, XAResource.TMRESUME);
0400:                printState("resume X1", cs1);
0401:
0402:                xar.end(xid, XAResource.TMSUCCESS);
0403:                printState("back to local (second time)", cs1);
0404:
0405:                cs1
0406:                        .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
0407:                cs1.setReadOnly(true);
0408:                setHoldability(cs1, true);
0409:                cs1.close();
0410:
0411:                cs1 = xac.getConnection();
0412:                printState("new handle - local ", cs1);
0413:                cs1.close();
0414:
0415:                xar.start(xid, XAResource.TMJOIN);
0416:                cs1 = xac.getConnection();
0417:                printState("re-join with new handle X1", cs1);
0418:                cs1.close();
0419:                xar.end(xid, XAResource.TMSUCCESS);
0420:
0421:                // now get a connection (attached to a local)
0422:                // attach to the global and commit it.
0423:                // state should be that of the local after the commit.
0424:                cs1 = xac.getConnection();
0425:                cs1
0426:                        .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
0427:                printState("pre-X1 commit - local", cs1);
0428:                xar.start(xid, XAResource.TMJOIN);
0429:                printState("pre-X1 commit - X1", cs1);
0430:                xar.end(xid, XAResource.TMSUCCESS);
0431:                printState("post-X1 end - local", cs1);
0432:                xar.commit(xid, true);
0433:                printState("post-X1 commit - local", cs1);
0434:                cs1.close();
0435:
0436:                //Derby-421 Setting isolation level with SQL was not getting handled correctly 
0437:                System.out
0438:                        .println("Some more isolation testing using SQL and JDBC api");
0439:                cs1 = xac.getConnection();
0440:                s = cs1.createStatement();
0441:                printState("initial local", cs1);
0442:
0443:                System.out
0444:                        .println("Issue setTransactionIsolation in local transaction");
0445:                cs1
0446:                        .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
0447:                printState("setTransactionIsolation in local", cs1);
0448:
0449:                testSetIsolationWithStatement(s, xar, cs1);
0450:
0451:                // now check re-use of *Statement objects across local/global connections.
0452:                System.out.println("TESTING RE_USE OF STATEMENT OBJECTS");
0453:                cs1 = xac.getConnection();
0454:
0455:                // ensure read locks stay around until end-of transaction
0456:                cs1
0457:                        .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
0458:                cs1.setAutoCommit(false);
0459:
0460:                checkLocks(cs1);
0461:
0462:                Statement sru1 = cs1.createStatement();
0463:                sru1.setCursorName("SN1");
0464:                sru1.executeUpdate("create table ru(i int)");
0465:                sru1.executeUpdate("insert into ru values 1,2,3");
0466:                Statement sruBatch = cs1.createStatement();
0467:                sruBatch.setCursorName("sruBatch");
0468:                Statement sruState = createFloatStatementForStateChecking(cs1);
0469:                PreparedStatement psruState = createFloatStatementForStateChecking(
0470:                        cs1, "select i from ru where i = ?");
0471:                CallableStatement csruState = createFloatCallForStateChecking(
0472:                        cs1, "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?,?)");
0473:                PreparedStatement psParams = cs1
0474:                        .prepareStatement("select * from ru where i > ?");
0475:                psParams.setCursorName("params");
0476:                psParams.setInt(1, 2);
0477:                resultSetQuery("Params-local-1", psParams.executeQuery());
0478:
0479:                sruBatch.addBatch("insert into ru values 4");
0480:                queryOnStatement("sru1-local-1", cs1, sru1);
0481:                cs1.commit(); // need to commit to switch to an global connection;
0482:                xid = new cdsXid(1, (byte) 103, (byte) 119);
0483:                xar.start(xid, XAResource.TMNOFLAGS); // simple case - underlying connection is re-used for global.
0484:                System.out
0485:                        .println("Expecting downgrade because global transaction sru1-global-2 is using a statement with holdability true");
0486:                queryOnStatement("sru1-global-2", cs1, sru1);
0487:                sruBatch.addBatch("insert into ru values 5");
0488:                Statement sru2 = cs1.createStatement();
0489:                sru2.setCursorName("OAK2");
0490:                queryOnStatement("sru2-global-3", cs1, sru2);
0491:                System.out
0492:                        .println("Expecting downgrade because global transaction sru1-global-4 is using a statement with holdability true");
0493:                queryOnStatement("sru1-global-4", cs1, sru1);
0494:                showStatementState("GLOBAL ", sruState);
0495:                showStatementState("PS GLOBAL ", psruState);
0496:                showStatementState("CS GLOBAL ", csruState);
0497:                resultSetQuery("Params-global-1", psParams.executeQuery());
0498:
0499:                xar.end(xid, XAResource.TMSUCCESS);
0500:                // now a new underlying connection is created
0501:                queryOnStatement("sru1-local-5", cs1, sru1);
0502:                queryOnStatement("sru2-local-6", cs1, sru2);
0503:                sruBatch.addBatch("insert into ru values 6,7");
0504:                Statement sru3 = cs1.createStatement();
0505:                sru3.setCursorName("SF3");
0506:                queryOnStatement("sru3-local-7", cs1, sru3);
0507:                // Two transactions should hold locks (global and the current XA);
0508:                showStatementState("LOCAL ", sruState);
0509:                showStatementState("PS LOCAL ", psruState);
0510:                showStatementState("CS LOCAL ", csruState);
0511:                resultSetQuery("Params-local-2", psParams.executeQuery());
0512:                checkLocks(cs1);
0513:                cs1.commit();
0514:
0515:                // attach the XA transaction to another connection and see what happens
0516:                XAConnection xac2 = dsx.getXAConnection();
0517:                xac2.addConnectionEventListener(new EventCatcher(5));
0518:                XAResource xar2 = xac2.getXAResource();
0519:
0520:                xar2.start(xid, XAResource.TMJOIN);
0521:                Connection cs2 = xac2.getConnection();
0522:
0523:                // these statements were generated by cs1 and thus are still
0524:                // in a local connection.
0525:                queryOnStatement("sru1-local-8", cs1, sru1);
0526:                queryOnStatement("sru2-local-9", cs1, sru2);
0527:                queryOnStatement("sru3-local-10", cs1, sru3);
0528:                sruBatch.addBatch("insert into ru values 8");
0529:                showStatementState("LOCAL 2 ", sruState);
0530:                showStatementState("PS LOCAL 2 ", psruState);
0531:                showStatementState("CS LOCAL 2", csruState);
0532:
0533:                checkLocks(cs1);
0534:
0535:                int[] updateCounts = sruBatch.executeBatch();
0536:                System.out.print("sruBatch update counts :");
0537:                for (int i = 0; i < updateCounts.length; i++) {
0538:                    System.out.print(" " + updateCounts[i] + " ");
0539:                }
0540:                System.out.println(":");
0541:                queryOnStatement("sruBatch", cs1, sruBatch);
0542:
0543:                xar2.end(xid, XAResource.TMSUCCESS);
0544:
0545:                xac2.close();
0546:
0547:                // allow close on already closed XAConnection
0548:                xac2.close();
0549:                xac2.addConnectionEventListener(null);
0550:                xac2.removeConnectionEventListener(null);
0551:
0552:                // test methods against a closed XAConnection and its resource
0553:                try {
0554:                    xac2.getXAResource();
0555:                } catch (SQLException sqle) {
0556:                    System.out.println("XAConnection.getXAResource : "
0557:                            + sqle.getMessage());
0558:                }
0559:                try {
0560:                    xac2.getConnection();
0561:                } catch (SQLException sqle) {
0562:                    System.out.println("XAConnection.getConnection : "
0563:                            + sqle.getMessage());
0564:                }
0565:                try {
0566:                    xar2.start(xid, XAResource.TMJOIN);
0567:                } catch (XAException xae) {
0568:                    showXAException("XAResource.start", xae);
0569:                }
0570:
0571:                try {
0572:                    xar2.end(xid, XAResource.TMJOIN);
0573:                } catch (XAException xae) {
0574:                    showXAException("XAResource.end", xae);
0575:                }
0576:                try {
0577:                    xar2.commit(xid, true);
0578:                } catch (XAException xae) {
0579:                    showXAException("XAResource.commit", xae);
0580:                }
0581:                try {
0582:                    xar2.prepare(xid);
0583:                } catch (XAException xae) {
0584:                    showXAException("XAResource.prepare", xae);
0585:                }
0586:                try {
0587:                    xar2.recover(0);
0588:                } catch (XAException xae) {
0589:                    showXAException("XAResource.recover", xae);
0590:                }
0591:                try {
0592:                    xar2.prepare(xid);
0593:                } catch (XAException xae) {
0594:                    showXAException("XAResource.prepare", xae);
0595:                }
0596:                try {
0597:                    xar2.isSameRM(xar2);
0598:                } catch (XAException xae) {
0599:                    showXAException("XAResource.isSameRM", xae);
0600:                }
0601:
0602:                // Patricio (on the forum) one was having an issue with set schema not working in an XA connection.
0603:                dmc = ij.startJBMS();
0604:                dmc.createStatement().executeUpdate(
0605:                        "create schema SCHEMA_Patricio");
0606:                dmc
0607:                        .createStatement()
0608:                        .executeUpdate(
0609:                                "create table SCHEMA_Patricio.Patricio (id VARCHAR(255), value INTEGER)");
0610:                dmc.commit();
0611:
0612:                dmc.close();
0613:
0614:                XAConnection xac3 = dsx.getXAConnection();
0615:                Connection conn3 = xac3.getConnection();
0616:                Statement st3 = conn3.createStatement();
0617:                st3.execute("SET SCHEMA SCHEMA_Patricio");
0618:                st3.close();
0619:
0620:                PreparedStatement ps3 = conn3
0621:                        .prepareStatement("INSERT INTO Patricio VALUES (? , ?)");
0622:                ps3.setString(1, "Patricio");
0623:                ps3.setInt(2, 3);
0624:                ps3.executeUpdate();
0625:
0626:                System.out.println("Patricio update count "
0627:                        + ps3.getUpdateCount());
0628:                ps3.close();
0629:                conn3.close();
0630:                xac3.close();
0631:
0632:                // test that an xastart in auto commit mode commits the existing work.(beetle 5178)
0633:                XAConnection xac4 = dsx.getXAConnection();
0634:                Xid xid4a = new cdsXid(4, (byte) 23, (byte) 76);
0635:                Connection conn4 = xac4.getConnection();
0636:                System.out.println("conn4 autcommit " + conn4.getAutoCommit());
0637:
0638:                Statement s4 = conn4.createStatement();
0639:                s4.executeUpdate("create table autocommitxastart(i int)");
0640:                s4
0641:                        .executeUpdate("insert into autocommitxastart values 1,2,3,4,5");
0642:
0643:                ResultSet rs4 = s4
0644:                        .executeQuery("select i from autocommitxastart");
0645:                rs4.next();
0646:                System.out.println("acxs " + rs4.getInt(1));
0647:                rs4.next();
0648:                System.out.println("acxs " + rs4.getInt(1));
0649:
0650:                xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
0651:                xac4.getXAResource().end(xid4a, XAResource.TMSUCCESS);
0652:
0653:                try {
0654:                    rs4.next();
0655:                    System.out.println("acxs " + rs.getInt(1));
0656:                } catch (SQLException sqle) {
0657:                    System.out.println("autocommitxastart expected "
0658:                            + sqle.getMessage());
0659:                }
0660:
0661:                conn4.setAutoCommit(false);
0662:
0663:                rs4 = s4.executeQuery("select i from autocommitxastart");
0664:                rs4.next();
0665:                System.out.println("acxs " + rs4.getInt(1));
0666:                rs4.next();
0667:                System.out.println("acxs " + rs4.getInt(1));
0668:
0669:                // Get a new xid to begin another transaction. 
0670:                // This should give XAER_OUTSIDE exception because
0671:                // the resource manager is busy in the local transaction
0672:                xid4a = new cdsXid(4, (byte) 93, (byte) 103);
0673:                try {
0674:                    xac4.getXAResource().start(xid4a, XAResource.TMNOFLAGS);
0675:                } catch (XAException xae) {
0676:                    showXAException("autocommitxastart expected ", xae);
0677:                    System.out.println("Expected XA error code: "
0678:                            + xae.errorCode);
0679:                }
0680:                rs4.next();
0681:                System.out.println("acxs " + rs4.getInt(1));
0682:                rs4.close();
0683:
0684:                conn4.rollback();
0685:                conn4.close();
0686:                xac4.close();
0687:
0688:                // test jira-derby 95 - a NullPointerException was returned when passing
0689:                // an incorrect database name (a url in this case) - should now give error XCY00
0690:                Connection dmc95 = ij.startJBMS();
0691:                String sqls;
0692:                try {
0693:                    testJira95ds(dmc95, "jdbc:derby:mydb");
0694:                } catch (SQLException sqle) {
0695:                    sqls = sqle.getSQLState();
0696:                    if (sqls.equals("XCY00"))
0697:                        System.out
0698:                                .println("; ok - expected exception: " + sqls);
0699:                    else
0700:                        System.out.println("; wrong, unexpected exception: "
0701:                                + sqls + " - " + sqle.toString());
0702:                } catch (Exception e) {
0703:                    System.out.println("; wrong, unexpected exception: "
0704:                            + e.toString());
0705:                }
0706:
0707:                try {
0708:                    testJira95xads(dmc95, "jdbc:derby:wombat");
0709:                } catch (SQLException sqle) {
0710:                    sqls = sqle.getSQLState();
0711:                    if (sqls.equals("XCY00"))
0712:                        System.out.println("; ok - expected exception: " + sqls
0713:                                + "\n");
0714:                    else
0715:                        System.out.println("; wrong - unexpected exception: "
0716:                                + sqls + " - " + sqle.toString());
0717:                } catch (Exception e) {
0718:                    System.out.println("; wrong, unexpected exception: "
0719:                            + e.toString());
0720:                }
0721:
0722:                if (TestUtil.isDerbyNetClientFramework())
0723:                    testClientDSConnectionAttributes();
0724:
0725:                // skip testDSRequestAuthentication for  client because of this issue: 
0726:                // DERBY-1131 : Deprecate  Derby DataSource property attributesAsPassword
0727:                // First part of this test is covered by testClientDSConnectionAttributes()
0728:                if (TestUtil.isDerbyNetClientFramework())
0729:                    return;
0730:                testDSRequestAuthentication();
0731:            }
0732:
0733:            /**
0734:             * @param s
0735:             * @param xar
0736:             * @param conn
0737:             * @throws SQLException
0738:             * @throws XAException
0739:             */
0740:            private void testSetIsolationWithStatement(Statement s,
0741:                    XAResource xar, Connection conn) throws SQLException,
0742:                    XAException {
0743:                Xid xid;
0744:                System.out
0745:                        .println("Issue SQL to change isolation in local transaction");
0746:                s.executeUpdate("set current isolation = RR");
0747:                printState("SQL to change isolation in local", conn);
0748:
0749:                xid = new cdsXid(1, (byte) 35, (byte) 47);
0750:                xar.start(xid, XAResource.TMNOFLAGS);
0751:                printState("1st global(new)", conn);
0752:                xar.end(xid, XAResource.TMSUCCESS);
0753:
0754:                printState("local", conn);
0755:                System.out
0756:                        .println("Issue SQL to change isolation in local transaction");
0757:                s.executeUpdate("set current isolation = RS");
0758:                printState("SQL to change isolation in local", conn);
0759:
0760:                // DERBY-1325 - Isolation level of local connection does not get reset after ending 
0761:                // a global transaction that was joined/resumed if the isolation level was changed 
0762:                // using SQL 
0763:                xar.start(xid, XAResource.TMJOIN);
0764:                printState("1st global(existing)", conn);
0765:                xar.end(xid, XAResource.TMSUCCESS);
0766:                printState("local", conn);
0767:                // DERBY-1325 end test 
0768:
0769:                Xid xid2 = new cdsXid(1, (byte) 93, (byte) 103);
0770:                xar.start(xid2, XAResource.TMNOFLAGS);
0771:                printState("2nd global(new)", conn);
0772:                xar.end(xid2, XAResource.TMSUCCESS);
0773:
0774:                xar.start(xid, XAResource.TMJOIN);
0775:                printState("1st global(existing)", conn);
0776:                xar.end(xid, XAResource.TMSUCCESS);
0777:
0778:                printState("local", conn);
0779:
0780:                xar.start(xid, XAResource.TMJOIN);
0781:                printState("1st global(existing)", conn);
0782:                System.out
0783:                        .println("Issue SQL to change isolation in 1st global transaction");
0784:                s.executeUpdate("set current isolation = UR");
0785:                printState(
0786:                        "change isolation of existing 1st global transaction",
0787:                        conn);
0788:                xar.end(xid, XAResource.TMSUCCESS);
0789:
0790:                printState("local", conn);
0791:
0792:                xar.start(xid2, XAResource.TMJOIN);
0793:                printState("2nd global(existing)", conn);
0794:                xar.end(xid2, XAResource.TMSUCCESS);
0795:                xar.rollback(xid2);
0796:                printState("(After 2nd global rollback) local", conn);
0797:
0798:                xar.rollback(xid);
0799:                printState("(After 1st global rollback) local", conn);
0800:            }
0801:
0802:            protected void showXAException(String tag, XAException xae) {
0803:
0804:                System.out
0805:                        .println(tag + " : XAException - " + xae.getMessage());
0806:            }
0807:
0808:            /**
0809:            	Create a statement with modified State.
0810:             */
0811:            protected Statement createFloatStatementForStateChecking(
0812:                    Connection conn) throws SQLException {
0813:                Statement s = internalCreateFloatStatementForStateChecking(conn);
0814:                s.setCursorName("StokeNewington");
0815:                s.setFetchDirection(ResultSet.FETCH_REVERSE);
0816:                s.setFetchSize(444);
0817:                s.setMaxFieldSize(713);
0818:                s.setMaxRows(19);
0819:
0820:                showStatementState("Create ", s);
0821:                return s;
0822:            }
0823:
0824:            protected Statement internalCreateFloatStatementForStateChecking(
0825:                    Connection conn) throws SQLException {
0826:                return conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
0827:                        ResultSet.CONCUR_READ_ONLY);
0828:            }
0829:
0830:            protected void showStatementState(String when, Statement s)
0831:                    throws SQLException {
0832:                System.out.println("Statement State @ " + when);
0833:                System.out.println("  getResultSetType() "
0834:                        + rsType(s.getResultSetType()));
0835:                System.out.println("  getResultSetConcurrency() "
0836:                        + rsConcurrency(s.getResultSetConcurrency()));
0837:                System.out.println("  getFetchDirection() "
0838:                        + rsFetchDirection(s.getFetchDirection()));
0839:                System.out.println("  getFetchSize() " + s.getFetchSize());
0840:                System.out
0841:                        .println("  getMaxFieldSize() " + s.getMaxFieldSize());
0842:                System.out.println("  getMaxRows() " + s.getMaxRows());
0843:            }
0844:
0845:            protected PreparedStatement createFloatStatementForStateChecking(
0846:                    Connection conn, String sql) throws SQLException {
0847:                PreparedStatement s = internalCreateFloatStatementForStateChecking(
0848:                        conn, sql);
0849:                s.setCursorName("StokeNewington");
0850:                s.setFetchDirection(ResultSet.FETCH_REVERSE);
0851:                s.setFetchSize(888);
0852:                s.setMaxFieldSize(317);
0853:                s.setMaxRows(91);
0854:
0855:                showStatementState("PS Create ", s);
0856:                return s;
0857:            }
0858:
0859:            protected CallableStatement createFloatCallForStateChecking(
0860:                    Connection conn, String sql) throws SQLException {
0861:                CallableStatement s = internalCreateFloatCallForStateChecking(
0862:                        conn, sql);
0863:                s.setCursorName("StokeNewington");
0864:                s.setFetchDirection(ResultSet.FETCH_REVERSE);
0865:                s.setFetchSize(999);
0866:                s.setMaxFieldSize(137);
0867:                s.setMaxRows(85);
0868:
0869:                showStatementState("CS Create ", s);
0870:                return s;
0871:            }
0872:
0873:            protected PreparedStatement internalCreateFloatStatementForStateChecking(
0874:                    Connection conn, String sql) throws SQLException {
0875:                return conn.prepareStatement(sql,
0876:                        ResultSet.TYPE_SCROLL_INSENSITIVE,
0877:                        ResultSet.CONCUR_READ_ONLY);
0878:            }
0879:
0880:            protected CallableStatement internalCreateFloatCallForStateChecking(
0881:                    Connection conn, String sql) throws SQLException {
0882:                return conn.prepareCall(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
0883:                        ResultSet.CONCUR_READ_ONLY);
0884:            }
0885:
0886:            /**
0887:             * Return the Java class and method for the procedure
0888:             * for the nested connection test.
0889:             * checkDataSource 30 will override.
0890:             */
0891:            protected String getNestedMethodName() {
0892:                return "checkDataSource.checkNesConn";
0893:            }
0894:
0895:            static String rsType(int type) {
0896:                switch (type) {
0897:                case ResultSet.TYPE_FORWARD_ONLY:
0898:                    return "FORWARD_ONLY";
0899:                case ResultSet.TYPE_SCROLL_SENSITIVE:
0900:                    return "SCROLL_SENSITIVE";
0901:                case ResultSet.TYPE_SCROLL_INSENSITIVE:
0902:                    return "SCROLL_INSENSITIVE";
0903:                default:
0904:                    return "?? TYPE UNKNOWN ??";
0905:
0906:                }
0907:            }
0908:
0909:            static String rsConcurrency(int type) {
0910:                switch (type) {
0911:                case ResultSet.CONCUR_READ_ONLY:
0912:                    return "READ_ONLY";
0913:                case ResultSet.CONCUR_UPDATABLE:
0914:                    return "UPDATEABLE";
0915:                default:
0916:                    return "?? CONCURRENCY UNKNOWN ??";
0917:
0918:                }
0919:            }
0920:
0921:            static String rsFetchDirection(int type) {
0922:                switch (type) {
0923:                case ResultSet.FETCH_FORWARD:
0924:                    return "FORWARD";
0925:                case ResultSet.FETCH_REVERSE:
0926:                    return "REVERSE";
0927:                case ResultSet.FETCH_UNKNOWN:
0928:                    return "UNKNOWN";
0929:                default:
0930:                    return "?? FETCH DIRECTION REALLY UNKNOWN ??";
0931:
0932:                }
0933:            }
0934:
0935:            private static void checkLocks(Connection conn) throws SQLException {
0936:                Statement s = conn.createStatement();
0937:                ResultSet rs = s
0938:                        .executeQuery("SELECT XID, sum(cast (LOCKCOUNT AS INT)) FROM SYSCS_DIAG.LOCK_TABLE AS L GROUP BY XID");
0939:                System.out.println("LOCK TABLE");
0940:
0941:                // Don't output actual XID's as they tend for every catalog change
0942:                // to the system.
0943:                int xact_index = 0;
0944:                while (rs.next()) {
0945:                    // System.out.println("  xid " + rs.getString(1) + " lock count " + rs.getInt(2));
0946:                    System.out.println("  xid row " + xact_index
0947:                            + " lock count " + rs.getInt(2));
0948:                    xact_index++;
0949:                }
0950:                s.close();
0951:                System.out.println("END LOCK TABLE");
0952:            }
0953:
0954:            private static void queryOnStatement(String tag, Connection conn,
0955:                    Statement s) throws SQLException {
0956:
0957:                try {
0958:                    if (s.getConnection() != conn)
0959:                        System.out.println(tag
0960:                                + ": mismatched Statement connection");
0961:                    resultSetQuery(tag, s.executeQuery("select * from ru"));
0962:                } catch (SQLException sqle) {
0963:                    System.out.println(tag + ": " + sqle.toString());
0964:                }
0965:            }
0966:
0967:            private static void resultSetQuery(String tag, ResultSet rs)
0968:                    throws SQLException {
0969:                String cursorName = rs.getCursorName();
0970:                System.out.print(tag + ": ru(" + cursorName + ") contents");
0971:                SecurityCheck.inspect(rs, "java.sql.ResultSet");
0972:                while (rs.next()) {
0973:                    System.out.print(" {" + rs.getInt(1) + "}");
0974:                }
0975:                System.out.println("");
0976:                rs.close();
0977:            }
0978:
0979:            private void printState(String header, Connection conn)
0980:                    throws SQLException {
0981:                System.out.println(header);
0982:                getHoldability(conn);
0983:                System.out.println("  isolation level "
0984:                        + translateIso(conn.getTransactionIsolation()));
0985:                System.out.println("  auto commit     " + conn.getAutoCommit());
0986:                System.out.println("  read only       " + conn.isReadOnly());
0987:            }
0988:
0989:            protected void setHoldability(Connection conn, boolean hold)
0990:                    throws SQLException {
0991:            }
0992:
0993:            protected void getHoldability(Connection conn) throws SQLException {
0994:            }
0995:
0996:            //calling checkConnection - for use in a procedure to get a nested connection.
0997:            public static void checkNesConn(String dsName) throws SQLException {
0998:                Connection conn = DriverManager
0999:                        .getConnection("jdbc:default:connection");
1000:                new checkDataSource().checkConnection(dsName, conn);
1001:            }
1002:
1003:            public void checkConnection(String dsName, Connection conn)
1004:                    throws SQLException {
1005:
1006:                System.out.println("Running connection checks on " + dsName);
1007:
1008:                SecurityCheck.inspect(conn, "java.sql.Connection");
1009:                SecurityCheck.inspect(conn.getMetaData(),
1010:                        "java.sql.DatabaseMetaData");
1011:
1012:                //System.out.println("  url             " + conn.getMetaData().getURL());
1013:                System.out.println("  isolation level "
1014:                        + conn.getTransactionIsolation());
1015:                System.out.println("  auto commit     " + conn.getAutoCommit());
1016:                System.out.println("  read only       " + conn.isReadOnly());
1017:
1018:                // when 4729 is fixed, remove the startsWith() clause
1019:                if (dsName.endsWith("DataSource")
1020:                        && !dsName.startsWith("Global"))
1021:                    System.out.println("  has warnings    "
1022:                            + (conn.getWarnings() != null));
1023:
1024:                Statement s1 = conn.createStatement();
1025:                checkStatement(dsName, conn, s1);
1026:                checkStatement(dsName, conn, conn.createStatement(
1027:                        ResultSet.TYPE_SCROLL_INSENSITIVE,
1028:                        ResultSet.CONCUR_READ_ONLY));
1029:
1030:                Connection c1 = conn.getMetaData().getConnection();
1031:                if (c1 != conn)
1032:                    System.out
1033:                            .println("FAIL incorrect connection object returned for DatabaseMetaData.getConnection()");
1034:
1035:                // Derby-33 - setTypeMap on connection
1036:                try {
1037:                    conn.setTypeMap(java.util.Collections.EMPTY_MAP);
1038:                    System.out.println("setTypeMap(EMPTY_MAP) - ok");
1039:                } catch (SQLException sqle) {
1040:                    System.out.println("setTypeMap(EMPTY_MAP) - FAIL "
1041:                            + sqle.getSQLState() + " - " + sqle.getMessage());
1042:                }
1043:                try {
1044:                    conn.setTypeMap(null);
1045:                    System.out
1046:                            .println("setTypeMap(null) - FAIL  - should throw exception");
1047:                } catch (SQLException sqle) {
1048:                    System.out.println("setTypeMap(null) - ok "
1049:                            + sqle.getSQLState() + " - " + sqle.getMessage());
1050:                }
1051:                try {
1052:                    // a populated map, not implemented
1053:                    java.util.Map map = new java.util.HashMap();
1054:                    map.put("name", "class");
1055:
1056:                    conn.setTypeMap(map);
1057:                    System.out
1058:                            .println("setTypeMap(map) - FAIL  - should throw exception");
1059:                } catch (SQLException sqle) {
1060:                    System.out.println("setTypeMap(map) - ok "
1061:                            + sqle.getSQLState() + " - " + sqle.getMessage());
1062:                }
1063:
1064:                checkConnectionPreClose(dsName, conn);
1065:                conn.close();
1066:
1067:                System.out.println("method calls on a closed connection");
1068:
1069:                try {
1070:                    conn.close();
1071:                    System.out.println(dsName
1072:                            + " <closedconn>.close() no error");
1073:                } catch (SQLException sqle) {
1074:                    System.out.println(dsName + " <closedconn>.close() "
1075:                            + sqle.getSQLState() + " - " + sqle.getMessage());
1076:                }
1077:                try {
1078:                    conn.createStatement();
1079:                    System.out.println(dsName
1080:                            + " <closedconn>.createStatement() no error");
1081:                } catch (SQLException sqle) {
1082:                    System.out.println(dsName
1083:                            + " <closedconn>.createStatement() "
1084:                            + sqle.getSQLState() + " - " + sqle.getMessage());
1085:                }
1086:                try {
1087:                    s1.execute("values 1");
1088:                    System.out.println(dsName
1089:                            + " <closedstmt>.execute() no error");
1090:                } catch (SQLException sqle) {
1091:                    System.out.println(dsName + " <closedstmt>.execute() "
1092:                            + sqle.getSQLState() + " - " + sqle.getMessage());
1093:                }
1094:            }
1095:
1096:            /**
1097:             * Make sure this connection's string is unique (DERBY-243)
1098:             */
1099:            protected static void checkToString(Connection conn)
1100:                    throws Exception {
1101:                checkStringFormat(conn);
1102:                String str = conn.toString();
1103:
1104:                if (conns.containsKey(str)) {
1105:                    throw new Exception(
1106:                            "ERROR: Connection toString() is not unique: "
1107:                                    + str);
1108:                }
1109:                conns.put(str, conn);
1110:            }
1111:
1112:            /** 
1113:             * Check the format of a pooled connection
1114:             **/
1115:            protected static void checkStringFormat(PooledConnection pc)
1116:                    throws Exception {
1117:                String prefix = checkStringPrefix(pc);
1118:                String connstr = pc.toString();
1119:                String format = "/" + prefix
1120:                        + " \\(ID = [0-9]+\\), Physical Connection = "
1121:                        + "<none>|" + CONNSTRING_FORMAT + "/";
1122:
1123:                if (!p5u.match(format, connstr)) {
1124:                    throw new Exception("Connection.toString() (" + connstr
1125:                            + ") " + "does not match expected format ("
1126:                            + format + ")");
1127:                }
1128:            }
1129:
1130:            /**
1131:             * Check the format of the connection string.  This is the default test
1132:             * to run if this is not a BrokeredConnection class
1133:             */
1134:            protected static void checkStringFormat(Connection conn)
1135:                    throws Exception {
1136:                String prefix = checkStringPrefix(conn);
1137:
1138:                String str = conn.toString();
1139:
1140:                // See if the connection string matches the format pattern    
1141:                if (!p5u.match("/" + CONNSTRING_FORMAT + "/", str)) {
1142:                    throw new Exception("Connection.toString() (" + str + ") "
1143:                            + "does not match expected format ("
1144:                            + CONNSTRING_FORMAT + ")");
1145:                }
1146:            }
1147:
1148:            /**
1149:             * Make sure the connection string starts with the right prefix, which
1150:             * is the classname@hashcode.
1151:             *
1152:             * @return the expected prefix string, this is used in further string
1153:             *   format checking
1154:             */
1155:            protected static String checkStringPrefix(Object conn)
1156:                    throws Exception {
1157:                String connstr = conn.toString();
1158:                String prefix = conn.getClass().getName() + "@"
1159:                        + conn.hashCode();
1160:                if (!connstr.startsWith(prefix)) {
1161:                    throw new Exception("Connection class and hash code for "
1162:                            + "connection string (" + connstr
1163:                            + ") does not match expected " + "(" + prefix + ")");
1164:                }
1165:
1166:                return prefix;
1167:            }
1168:
1169:            /**
1170:             * Clear out and close connections in the connections
1171:             * hashtable. 
1172:             */
1173:            protected static void clearConnections() throws SQLException {
1174:                java.util.Iterator it = conns.values().iterator();
1175:                while (it.hasNext()) {
1176:                    Connection conn = (Connection) it.next();
1177:                    conn.close();
1178:                }
1179:                conns.clear();
1180:            }
1181:
1182:            /**
1183:             * Get connections  using ij.startJBMS() and make sure
1184:             * they're unique
1185:             */
1186:            protected static void checkJBMSToString() throws Exception {
1187:                clearConnections();
1188:                // Open ten connections rather than just two to
1189:                // try and catch any odd uniqueness bugs.  Still
1190:                // no guarantee but is better than just two.
1191:                int numConnections = 10;
1192:                for (int i = 0; i < numConnections; i++) {
1193:                    Connection conn = ij.startJBMS();
1194:                    checkToString(conn);
1195:                }
1196:
1197:                // Now close the connections
1198:                clearConnections();
1199:            }
1200:
1201:            /**
1202:             * Check uniqueness of connection strings coming from a
1203:             * DataSouce
1204:             */
1205:            protected static void checkToString(DataSource ds) throws Exception {
1206:                clearConnections();
1207:
1208:                int numConnections = 10;
1209:                for (int i = 0; i < numConnections; i++) {
1210:                    Connection conn = ds.getConnection();
1211:                    checkToString(conn);
1212:                }
1213:
1214:                clearConnections();
1215:            }
1216:
1217:            /**
1218:             * Check uniqueness of strings with a pooled data source.
1219:             * We want to check the PooledConnection as well as the
1220:             * underlying physical connection. 
1221:             */
1222:            protected static void checkToString(ConnectionPoolDataSource pds)
1223:                    throws Exception {
1224:                int numConnections = 10;
1225:
1226:                //  First get a bunch of pooled connections
1227:                //  and make sure they're all unique
1228:                Hashtable pooledConns = new Hashtable();
1229:                for (int i = 0; i < numConnections; i++) {
1230:                    PooledConnection pc = pds.getPooledConnection();
1231:                    checkStringFormat(pc);
1232:                    String str = pc.toString();
1233:                    if (pooledConns.get(str) != null) {
1234:                        throw new Exception("Pooled connection toString "
1235:                                + "value " + str + " is not unique");
1236:                    }
1237:                    pooledConns.put(str, pc);
1238:                }
1239:
1240:                // Now check that connections from each of these
1241:                // pooled connections have different string values
1242:                Iterator it = pooledConns.values().iterator();
1243:                clearConnections();
1244:                while (it.hasNext()) {
1245:                    PooledConnection pc = (PooledConnection) it.next();
1246:                    Connection conn = pc.getConnection();
1247:                    checkToString(conn);
1248:                }
1249:                clearConnections();
1250:
1251:                // Now clear out the pooled connections
1252:                it = pooledConns.values().iterator();
1253:                while (it.hasNext()) {
1254:                    PooledConnection pc = (PooledConnection) it.next();
1255:                    pc.close();
1256:                }
1257:                pooledConns.clear();
1258:            }
1259:
1260:            /**
1261:             * Check uniqueness of strings for an XA data source
1262:             */
1263:            protected static void checkToString(XADataSource xds)
1264:                    throws Exception {
1265:                int numConnections = 10;
1266:
1267:                //  First get a bunch of pooled connections
1268:                //  and make sure they're all unique
1269:                Hashtable xaConns = new Hashtable();
1270:                for (int i = 0; i < numConnections; i++) {
1271:                    XAConnection xc = xds.getXAConnection();
1272:                    checkStringFormat(xc);
1273:                    String str = xc.toString();
1274:                    if (xaConns.get(str) != null) {
1275:                        throw new Exception("XA connection toString "
1276:                                + "value " + str + " is not unique");
1277:                    }
1278:                    xaConns.put(str, xc);
1279:                }
1280:
1281:                // Now check that connections from each of these
1282:                // pooled connections have different string values
1283:                Iterator it = xaConns.values().iterator();
1284:                clearConnections();
1285:                while (it.hasNext()) {
1286:                    XAConnection xc = (XAConnection) it.next();
1287:                    Connection conn = xc.getConnection();
1288:                    checkToString(conn);
1289:                }
1290:                clearConnections();
1291:
1292:                // Now clear out the pooled connections
1293:                it = xaConns.values().iterator();
1294:                while (it.hasNext()) {
1295:                    XAConnection xc = (XAConnection) it.next();
1296:                    xc.close();
1297:                }
1298:                xaConns.clear();
1299:            }
1300:
1301:            protected void checkConnectionPreClose(String dsName,
1302:                    Connection conn) throws SQLException {
1303:                if (dsName.endsWith("DataSource")) {
1304:
1305:                    // see if setting the state is carried over to any future connection from the
1306:                    // data source object.
1307:                    try {
1308:                        conn.setReadOnly(true);
1309:                    } catch (SQLException sqle) {
1310:                        // cannot set read-only in an active transaction, & sometimes
1311:                        // connections are active at this point.
1312:                    }
1313:                }
1314:            }
1315:
1316:            protected void checkStatement(String dsName, Connection conn,
1317:                    Statement s) throws SQLException {
1318:
1319:                SecurityCheck.inspect(s, "java.sql.Statement");
1320:
1321:                Connection c1 = s.getConnection();
1322:                if (c1 != conn)
1323:                    System.out
1324:                            .println("FAIL incorrect connection object returned for Statement.getConnection()");
1325:
1326:                s.addBatch("insert into y values 1");
1327:                s.addBatch("insert into y values 2,3");
1328:                int[] states = s.executeBatch();
1329:                if (states[0] != 1)
1330:                    System.out
1331:                            .println("FAIL invalid update count for first batch statement");
1332:                if (states[1] != 2)
1333:                    System.out
1334:                            .println("FAIL invalid update count for second batch statement");
1335:
1336:                ResultSet rs = s.executeQuery("VALUES 1");
1337:                if (rs.getStatement() != s)
1338:                    System.out
1339:                            .println(dsName
1340:                                    + " FAIL incorrect Statement object returned for ResultSet.getStatement");
1341:                rs.close();
1342:                s.close();
1343:            }
1344:
1345:            private static void testDSRequestAuthentication()
1346:                    throws SQLException {
1347:
1348:                EmbeddedDataSource ds = new EmbeddedDataSource();
1349:
1350:                System.out.println("DataSource - EMPTY");
1351:                dsConnectionRequests(ds);
1352:
1353:                System.out
1354:                        .println("DataSource - connectionAttributes=databaseName=wombat");
1355:                ds.setConnectionAttributes("databaseName=wombat");
1356:                dsConnectionRequests(ds);
1357:                ds.setConnectionAttributes(null);
1358:
1359:                System.out.println("DataSource - attributesAsPassword=true");
1360:                ds.setAttributesAsPassword(true);
1361:                dsConnectionRequests(ds);
1362:                ds.setAttributesAsPassword(false);
1363:
1364:                System.out
1365:                        .println("DataSource - attributesAsPassword=true, connectionAttributes=databaseName=kangaroo");
1366:                ds.setAttributesAsPassword(true);
1367:                ds.setConnectionAttributes("databaseName=kangaroo");
1368:                dsConnectionRequests(ds);
1369:                ds.setAttributesAsPassword(false);
1370:                ds.setConnectionAttributes(null);
1371:
1372:                System.out.println("Enable Authentication");
1373:                ds.setDatabaseName("wombat");
1374:                Connection cadmin = ds.getConnection();
1375:                CallableStatement cs = cadmin
1376:                        .prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
1377:                cs.setString(1, "derby.user.fred");
1378:                cs.setString(2, "wilma");
1379:                cs.execute();
1380:
1381:                cs.setString(1, "derby.authentication.provider");
1382:                cs.setString(2, "BUILTIN");
1383:                cs.execute();
1384:
1385:                cs.setString(1, "derby.connection.requireAuthentication");
1386:                cs.setString(2, "true");
1387:                cs.execute();
1388:
1389:                cs.close();
1390:
1391:                cadmin.close();
1392:
1393:                ds.setShutdownDatabase("shutdown");
1394:                try {
1395:                    ds.getConnection();
1396:                } catch (SQLException sqle) {
1397:                    System.out.println(sqle.getSQLState() + ":"
1398:                            + sqle.getMessage());
1399:                }
1400:
1401:                ds.setDatabaseName(null);
1402:                ds.setShutdownDatabase(null);
1403:
1404:                System.out.println("AUTHENTICATION NOW ENABLED");
1405:
1406:                System.out.println("DataSource - attributesAsPassword=true");
1407:                ds.setAttributesAsPassword(true);
1408:                dsConnectionRequests(ds);
1409:                ds.setAttributesAsPassword(false);
1410:
1411:                // ensure the DS property password is not treated as a set of attributes.
1412:                System.out
1413:                        .println("DataSource - attributesAsPassword=true, user=fred, password=databaseName=wombat;password=wilma");
1414:                ds.setAttributesAsPassword(true);
1415:                ds.setUser("fred");
1416:                ds.setPassword("databaseName=wombat;password=wilma");
1417:                dsConnectionRequests(ds);
1418:                ds.setAttributesAsPassword(false);
1419:                ds.setUser(null);
1420:                ds.setPassword(null);
1421:                ds = null;
1422:
1423:                // now with ConnectionPoolDataSource
1424:                EmbeddedConnectionPoolDataSource cpds = new EmbeddedConnectionPoolDataSource();
1425:                System.out.println("ConnectionPoolDataSource - EMPTY");
1426:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1427:
1428:                System.out
1429:                        .println("ConnectionPoolDataSource - connectionAttributes=databaseName=wombat");
1430:                cpds.setConnectionAttributes("databaseName=wombat");
1431:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1432:                cpds.setConnectionAttributes(null);
1433:
1434:                System.out
1435:                        .println("ConnectionPoolDataSource - attributesAsPassword=true");
1436:                cpds.setAttributesAsPassword(true);
1437:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1438:                cpds.setAttributesAsPassword(false);
1439:
1440:                // ensure the DS property password is not treated as a set of attributes.
1441:                System.out
1442:                        .println("ConnectionPoolDataSource - attributesAsPassword=true, user=fred, password=databaseName=wombat;password=wilma");
1443:                cpds.setAttributesAsPassword(true);
1444:                cpds.setUser("fred");
1445:                cpds.setPassword("databaseName=wombat;password=wilma");
1446:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1447:                cpds.setAttributesAsPassword(false);
1448:                cpds.setUser(null);
1449:                cpds.setPassword(null);
1450:                cpds = null;
1451:
1452:                // now with XADataSource
1453:                EmbeddedXADataSource xads = new EmbeddedXADataSource();
1454:                System.out.println("XADataSource - EMPTY");
1455:                dsConnectionRequests((XADataSource) xads);
1456:
1457:                System.out.println("XADataSource - databaseName=wombat");
1458:                xads.setDatabaseName("wombat");
1459:                dsConnectionRequests((XADataSource) xads);
1460:                xads.setDatabaseName(null);
1461:
1462:                System.out
1463:                        .println("XADataSource - connectionAttributes=databaseName=wombat");
1464:                xads.setConnectionAttributes("databaseName=wombat");
1465:                dsConnectionRequests((XADataSource) xads);
1466:                xads.setConnectionAttributes(null);
1467:
1468:                System.out.println("XADataSource - attributesAsPassword=true");
1469:                xads.setAttributesAsPassword(true);
1470:                dsConnectionRequests((XADataSource) xads);
1471:                xads.setAttributesAsPassword(false);
1472:
1473:                System.out
1474:                        .println("XADataSource - databaseName=wombat, attributesAsPassword=true");
1475:                xads.setDatabaseName("wombat");
1476:                xads.setAttributesAsPassword(true);
1477:                dsConnectionRequests((XADataSource) xads);
1478:                xads.setAttributesAsPassword(false);
1479:                xads.setDatabaseName(null);
1480:            }
1481:
1482:            /**
1483:             * Check that database name set using setConnectionAttributes is not used
1484:             * by ClientDataSource. This method tests DERBY-1130.
1485:             * 
1486:             * @throws SQLException
1487:             */
1488:            private static void testClientDSConnectionAttributes()
1489:                    throws SQLException {
1490:
1491:                ClientDataSource ds = new ClientDataSource();
1492:
1493:                System.out.println("DataSource - EMPTY");
1494:                dsConnectionRequests(ds);
1495:
1496:                System.out
1497:                        .println("DataSource - connectionAttributes=databaseName=wombat");
1498:                ds.setConnectionAttributes("databaseName=wombat");
1499:                dsConnectionRequests(ds);
1500:                ds.setConnectionAttributes(null);
1501:
1502:                // Test that database name specified in connection attributes is not used
1503:                System.out
1504:                        .println("DataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo");
1505:                ds.setConnectionAttributes("databaseName=kangaroo");
1506:                ds.setDatabaseName("wombat");
1507:                dsConnectionRequests(ds);
1508:                ds.setConnectionAttributes(null);
1509:                ds.setDatabaseName(null);
1510:
1511:                // now with ConnectionPoolDataSource
1512:                ClientConnectionPoolDataSource cpds = new ClientConnectionPoolDataSource();
1513:                System.out.println("ConnectionPoolDataSource - EMPTY");
1514:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1515:
1516:                System.out
1517:                        .println("ConnectionPoolDataSource - connectionAttributes=databaseName=wombat");
1518:                cpds.setConnectionAttributes("databaseName=wombat");
1519:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1520:                cpds.setConnectionAttributes(null);
1521:
1522:                // Test that database name specified in connection attributes is not used
1523:                System.out
1524:                        .println("ConnectionPoolDataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo");
1525:                cpds.setConnectionAttributes("databaseName=kangaroo");
1526:                cpds.setDatabaseName("wombat");
1527:                dsConnectionRequests((ConnectionPoolDataSource) cpds);
1528:                cpds.setConnectionAttributes(null);
1529:                cpds.setDatabaseName(null);
1530:
1531:                // now with XADataSource
1532:                ClientXADataSource xads = new ClientXADataSource();
1533:                System.out.println("XADataSource - EMPTY");
1534:                dsConnectionRequests((XADataSource) xads);
1535:
1536:                System.out
1537:                        .println("XADataSource - connectionAttributes=databaseName=wombat");
1538:                xads.setConnectionAttributes("databaseName=wombat");
1539:                dsConnectionRequests((XADataSource) xads);
1540:                xads.setConnectionAttributes(null);
1541:
1542:                // Test that database name specified in connection attributes is not used
1543:                System.out
1544:                        .println("XADataSource - databaseName=wombat and connectionAttributes=databaseName=kangaroo");
1545:                xads.setConnectionAttributes("databaseName=kangaroo");
1546:                xads.setDatabaseName("wombat");
1547:                dsConnectionRequests((XADataSource) xads);
1548:                xads.setConnectionAttributes(null);
1549:                xads.setDatabaseName(null);
1550:            }
1551:
1552:            private static void dsConnectionRequests(DataSource ds) {
1553:
1554:                SecurityCheck.inspect(ds, "javax.sql.DataSource");
1555:
1556:                try {
1557:                    Connection c1 = ds.getConnection();
1558:                    System.out.println("  getConnection() - OK");
1559:                    c1.close();
1560:                } catch (SQLException sqle) {
1561:                    System.out.println("  getConnection() - "
1562:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1563:                }
1564:
1565:                try {
1566:                    Connection c1 = ds.getConnection(null, null);
1567:                    System.out.println("  getConnection(null, null) - OK");
1568:                    c1.close();
1569:                } catch (SQLException sqle) {
1570:                    System.out.println("  getConnection(null, null) - "
1571:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1572:                }
1573:                try {
1574:                    Connection c1 = ds.getConnection("fred", null);
1575:                    System.out.println("  getConnection(fred, null) - OK");
1576:                    c1.close();
1577:                } catch (SQLException sqle) {
1578:                    System.out.println("  getConnection(fred, null) - "
1579:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1580:                }
1581:                try {
1582:                    Connection c1 = ds.getConnection("fred", "wilma");
1583:                    System.out.println("  getConnection(fred, wilma) - OK");
1584:                    c1.close();
1585:                } catch (SQLException sqle) {
1586:                    System.out.println("  getConnection(fred, wilma) - "
1587:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1588:                }
1589:                try {
1590:                    Connection c1 = ds.getConnection(null, "wilma");
1591:                    System.out.println("  getConnection(null, wilma) - OK");
1592:                    c1.close();
1593:                } catch (SQLException sqle) {
1594:                    System.out.println("  getConnection(null, wilma) - "
1595:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1596:                }
1597:                try {
1598:                    Connection c1 = ds.getConnection(null,
1599:                            "databaseName=wombat");
1600:                    System.out
1601:                            .println("  getConnection(null, databaseName=wombat) - OK");
1602:                    c1.close();
1603:                } catch (SQLException sqle) {
1604:                    System.out
1605:                            .println("  getConnection(null, databaseName=wombat) - "
1606:                                    + sqle.getSQLState()
1607:                                    + ":"
1608:                                    + sqle.getMessage());
1609:                }
1610:                try {
1611:                    Connection c1 = ds.getConnection("fred",
1612:                            "databaseName=wombat");
1613:                    System.out
1614:                            .println("  getConnection(fred, databaseName=wombat) - OK");
1615:                    c1.close();
1616:                } catch (SQLException sqle) {
1617:                    System.out
1618:                            .println("  getConnection(fred, databaseName=wombat) - "
1619:                                    + sqle.getSQLState()
1620:                                    + ":"
1621:                                    + sqle.getMessage());
1622:                }
1623:                try {
1624:                    Connection c1 = ds.getConnection("fred",
1625:                            "databaseName=wombat;password=wilma");
1626:                    System.out
1627:                            .println("  getConnection(fred, databaseName=wombat;password=wilma) - OK");
1628:                    c1.close();
1629:                } catch (SQLException sqle) {
1630:                    System.out
1631:                            .println("  getConnection(fred, databaseName=wombat;password=wilma) - "
1632:                                    + sqle.getSQLState()
1633:                                    + ":"
1634:                                    + sqle.getMessage());
1635:                }
1636:                try {
1637:                    Connection c1 = ds.getConnection("fred",
1638:                            "databaseName=wombat;password=betty");
1639:                    System.out
1640:                            .println("  getConnection(fred, databaseName=wombat;password=betty) - OK");
1641:                    c1.close();
1642:                } catch (SQLException sqle) {
1643:                    System.out
1644:                            .println("  getConnection(fred, databaseName=wombat;password=betty) - "
1645:                                    + sqle.getSQLState()
1646:                                    + ":"
1647:                                    + sqle.getMessage());
1648:                }
1649:            }
1650:
1651:            private static void dsConnectionRequests(ConnectionPoolDataSource ds) {
1652:                try {
1653:                    PooledConnection pc = ds.getPooledConnection();
1654:                    System.out.println("  getPooledConnection() - OK");
1655:                    Connection c1 = pc.getConnection();
1656:                    c1.close();
1657:                    pc.close();
1658:                } catch (SQLException sqle) {
1659:                    System.out.println("  getPooledConnection() - "
1660:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1661:                }
1662:
1663:                try {
1664:                    PooledConnection pc = ds.getPooledConnection(null, null);
1665:                    System.out
1666:                            .println("  getPooledConnection(null, null) - OK");
1667:                    Connection c1 = pc.getConnection();
1668:                    c1.close();
1669:                } catch (SQLException sqle) {
1670:                    System.out.println("  getPooledConnection(null, null) - "
1671:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1672:                }
1673:                try {
1674:                    PooledConnection pc = ds.getPooledConnection("fred", null);
1675:                    System.out
1676:                            .println("  getPooledConnection(fred, null) - OK");
1677:                    Connection c1 = pc.getConnection();
1678:                    c1.close();
1679:                    pc.close();
1680:                } catch (SQLException sqle) {
1681:                    System.out.println("  getPooledConnection(fred, null) - "
1682:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1683:                }
1684:                try {
1685:                    PooledConnection pc = ds.getPooledConnection("fred",
1686:                            "wilma");
1687:                    System.out
1688:                            .println("  getPooledConnection(fred, wilma) - OK");
1689:                    Connection c1 = pc.getConnection();
1690:                    c1.close();
1691:                    pc.close();
1692:                } catch (SQLException sqle) {
1693:                    System.out.println("  getPooledConnection(fred, wilma) - "
1694:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1695:                }
1696:                try {
1697:                    PooledConnection pc = ds.getPooledConnection(null, "wilma");
1698:                    System.out
1699:                            .println("  getPooledConnection(null, wilma) - OK");
1700:                    Connection c1 = pc.getConnection();
1701:                    c1.close();
1702:                    pc.close();
1703:                } catch (SQLException sqle) {
1704:                    System.out.println("  getPooledConnection(null, wilma) - "
1705:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1706:                }
1707:                try {
1708:                    PooledConnection pc = ds.getPooledConnection(null,
1709:                            "databaseName=wombat");
1710:                    System.out
1711:                            .println("  getPooledConnection(null, databaseName=wombat) - OK");
1712:                    Connection c1 = pc.getConnection();
1713:                    c1.close();
1714:                    pc.close();
1715:                } catch (SQLException sqle) {
1716:                    System.out
1717:                            .println("  getPooledConnection(null, databaseName=wombat) - "
1718:                                    + sqle.getSQLState()
1719:                                    + ":"
1720:                                    + sqle.getMessage());
1721:                }
1722:                try {
1723:                    PooledConnection pc = ds.getPooledConnection("fred",
1724:                            "databaseName=wombat");
1725:                    System.out
1726:                            .println("  getPooledConnection(fred, databaseName=wombat) - OK");
1727:                    Connection c1 = pc.getConnection();
1728:                    c1.close();
1729:                    pc.close();
1730:                } catch (SQLException sqle) {
1731:                    System.out
1732:                            .println("  getPooledConnection(fred, databaseName=wombat) - "
1733:                                    + sqle.getSQLState()
1734:                                    + ":"
1735:                                    + sqle.getMessage());
1736:                }
1737:                try {
1738:                    PooledConnection pc = ds.getPooledConnection("fred",
1739:                            "databaseName=wombat;password=wilma");
1740:                    System.out
1741:                            .println("  getPooledConnection(fred, databaseName=wombat;password=wilma) - OK");
1742:                    Connection c1 = pc.getConnection();
1743:                    c1.close();
1744:                    pc.close();
1745:                } catch (SQLException sqle) {
1746:                    System.out
1747:                            .println("  getPooledConnection(fred, databaseName=wombat;password=wilma) - "
1748:                                    + sqle.getSQLState()
1749:                                    + ":"
1750:                                    + sqle.getMessage());
1751:                }
1752:                try {
1753:                    PooledConnection pc = ds.getPooledConnection("fred",
1754:                            "databaseName=wombat;password=betty");
1755:                    System.out
1756:                            .println("  getPooledConnection(fred, databaseName=wombat;password=betty) - OK");
1757:                    Connection c1 = pc.getConnection();
1758:                    c1.close();
1759:                    pc.close();
1760:                } catch (SQLException sqle) {
1761:                    System.out
1762:                            .println("  getPooledConnection(fred, databaseName=wombat;password=betty) - "
1763:                                    + sqle.getSQLState()
1764:                                    + ":"
1765:                                    + sqle.getMessage());
1766:                }
1767:            }
1768:
1769:            private static void dsConnectionRequests(XADataSource ds) {
1770:                try {
1771:                    XAConnection xc = ds.getXAConnection();
1772:                    System.out.println("  getXAConnection() - OK");
1773:                    Connection c1 = xc.getConnection();
1774:                    c1.close();
1775:                    xc.close();
1776:                } catch (SQLException sqle) {
1777:                    System.out.println("  getXAConnection() - "
1778:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1779:                }
1780:
1781:                try {
1782:                    XAConnection xc = ds.getXAConnection(null, null);
1783:                    System.out.println("  getXAConnection(null, null) - OK");
1784:                    Connection c1 = xc.getConnection();
1785:                    c1.close();
1786:                } catch (SQLException sqle) {
1787:                    System.out.println("  getXAConnection(null, null) - "
1788:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1789:                }
1790:                try {
1791:                    XAConnection xc = ds.getXAConnection("fred", null);
1792:                    System.out.println("  getXAConnection(fred, null) - OK");
1793:                    Connection c1 = xc.getConnection();
1794:                    c1.close();
1795:                    xc.close();
1796:                } catch (SQLException sqle) {
1797:                    System.out.println("  getXAConnection(fred, null) - "
1798:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1799:                }
1800:                try {
1801:                    XAConnection xc = ds.getXAConnection("fred", "wilma");
1802:                    System.out.println("  getXAConnection(fred, wilma) - OK");
1803:                    Connection c1 = xc.getConnection();
1804:                    c1.close();
1805:                    xc.close();
1806:                } catch (SQLException sqle) {
1807:                    System.out.println("  getXAConnection(fred, wilma) - "
1808:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1809:                }
1810:                try {
1811:                    XAConnection xc = ds.getXAConnection(null, "wilma");
1812:                    System.out.println("  getXAConnection(null, wilma) - OK");
1813:                    Connection c1 = xc.getConnection();
1814:                    c1.close();
1815:                    xc.close();
1816:                } catch (SQLException sqle) {
1817:                    System.out.println("  getXAConnection(null, wilma) - "
1818:                            + sqle.getSQLState() + ":" + sqle.getMessage());
1819:                }
1820:                try {
1821:                    XAConnection xc = ds.getXAConnection(null,
1822:                            "databaseName=wombat");
1823:                    System.out
1824:                            .println("  getXAConnection(null, databaseName=wombat) - OK");
1825:                    Connection c1 = xc.getConnection();
1826:                    c1.close();
1827:                    xc.close();
1828:                } catch (SQLException sqle) {
1829:                    System.out
1830:                            .println("  getXAConnection(null, databaseName=wombat) - "
1831:                                    + sqle.getSQLState()
1832:                                    + ":"
1833:                                    + sqle.getMessage());
1834:                }
1835:                try {
1836:                    XAConnection xc = ds.getXAConnection("fred",
1837:                            "databaseName=wombat");
1838:                    System.out
1839:                            .println("  getXAConnection(fred, databaseName=wombat) - OK");
1840:                    Connection c1 = xc.getConnection();
1841:                    c1.close();
1842:                    xc.close();
1843:                } catch (SQLException sqle) {
1844:                    System.out
1845:                            .println("  getXAConnection(fred, databaseName=wombat) - "
1846:                                    + sqle.getSQLState()
1847:                                    + ":"
1848:                                    + sqle.getMessage());
1849:                }
1850:                try {
1851:                    XAConnection xc = ds.getXAConnection("fred",
1852:                            "databaseName=wombat;password=wilma");
1853:                    System.out
1854:                            .println("  getXAConnection(fred, databaseName=wombat;password=wilma) - OK");
1855:                    Connection c1 = xc.getConnection();
1856:                    c1.close();
1857:                    xc.close();
1858:                } catch (SQLException sqle) {
1859:                    System.out
1860:                            .println("  getXAConnection(fred, databaseName=wombat;password=wilma) - "
1861:                                    + sqle.getSQLState()
1862:                                    + ":"
1863:                                    + sqle.getMessage());
1864:                }
1865:                try {
1866:                    XAConnection xc = ds.getXAConnection("fred",
1867:                            "databaseName=wombat;password=betty");
1868:                    System.out
1869:                            .println("  getXAConnection(fred, databaseName=wombat;password=betty) - OK");
1870:                    Connection c1 = xc.getConnection();
1871:                    c1.close();
1872:                    xc.close();
1873:                } catch (SQLException sqle) {
1874:                    System.out
1875:                            .println("  getXAConnection(fred, databaseName=wombat;password=betty) - "
1876:                                    + sqle.getSQLState()
1877:                                    + ":"
1878:                                    + sqle.getMessage());
1879:                }
1880:            }
1881:
1882:            protected Xid getXid(int xid, byte b1, byte b2) {
1883:                return new cdsXid(xid, b1, b2);
1884:            }
1885:
1886:            public static String translateIso(int iso) {
1887:                switch (iso) {
1888:                case Connection.TRANSACTION_READ_COMMITTED:
1889:                    return "READ_COMMITTED";
1890:                case Connection.TRANSACTION_SERIALIZABLE:
1891:                    return "SERIALIZABLE";
1892:                case Connection.TRANSACTION_REPEATABLE_READ:
1893:                    return "REPEATABLE_READ";
1894:                case Connection.TRANSACTION_READ_UNCOMMITTED:
1895:                    return "READ_UNCOMMITTED";
1896:                }
1897:                return "unknown";
1898:            }
1899:
1900:            /**
1901:            	When a connection is being pooled, the underlying JDBC embedded
1902:            	connection object is re-used. As each application gets a new
1903:            	Connection object, that is really a wrapper around the old connection
1904:            	it should reset any connection spoecific state on the embedded connection
1905:            	object.
1906:             */
1907:            private static void testPoolReset(String type, PooledConnection pc)
1908:                    throws SQLException {
1909:                System.out.println("Start testPoolReset " + type);
1910:                testPoolResetWork("C", pc.getConnection());
1911:                testPoolResetWork("", pc.getConnection());
1912:                testPoolResetWork("D", pc.getConnection());
1913:
1914:                pc.close();
1915:                System.out.println("End testPoolReset " + type);
1916:            }
1917:
1918:            private static void testPoolResetWork(String tableAction,
1919:                    Connection conn) throws SQLException {
1920:                Statement s = conn.createStatement();
1921:                if (tableAction.equals("C")) {
1922:                    s
1923:                            .execute("CREATE TABLE testPoolResetWork (id int generated always as identity, name varchar(25))");
1924:                }
1925:
1926:                ResultSet rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()");
1927:                rs.next();
1928:                String val = rs.getString(1);
1929:                if (!rs.wasNull() || (val != null))
1930:                    System.out
1931:                            .println("FAIL - initial call to IDENTITY_VAL_LOCAL is not NULL!"
1932:                                    + val);
1933:                rs.close();
1934:
1935:                s
1936:                        .executeUpdate("INSERT INTO testPoolResetWork(name) values ('derby-222')");
1937:
1938:                rs = s.executeQuery("VALUES IDENTITY_VAL_LOCAL()");
1939:                rs.next();
1940:                val = rs.getString(1);
1941:                System.out.println("IDENTITY_VAL_LOCAL=" + val);
1942:                rs.close();
1943:
1944:                if (tableAction.equals("D")) {
1945:                    s.execute("DROP TABLE testPoolResetWork");
1946:                }
1947:
1948:                s.close();
1949:                conn.close();
1950:
1951:            }
1952:
1953:            public void testJira95ds(Connection conn, String dbName)
1954:                    throws SQLException {
1955:                System.out.print("\ntesting jira 95 for DataSource");
1956:                EmbeddedDataSource ds = new EmbeddedDataSource();
1957:                ds.setDatabaseName(dbName);
1958:                Connection conn1 = ds.getConnection();
1959:                conn1.close();
1960:            }
1961:
1962:            public void testJira95xads(Connection conn, String dbName)
1963:                    throws SQLException {
1964:                System.out.print("testing jira 95 for XADataSource");
1965:                EmbeddedXADataSource dxs = new EmbeddedXADataSource();
1966:                dxs.setDatabaseName(dbName);
1967:                Connection conn2 = dxs.getXAConnection().getConnection();
1968:                conn2.close();
1969:            }
1970:
1971:        }
1972:
1973:        class cdsXid implements  Xid, Serializable {
1974:            private static final long serialVersionUID = 64467338100036L;
1975:
1976:            private final int format_id;
1977:            private byte[] global_id;
1978:            private byte[] branch_id;
1979:
1980:            cdsXid(int xid, byte b1, byte b2) {
1981:                format_id = xid;
1982:                global_id = new byte[Xid.MAXGTRIDSIZE];
1983:                branch_id = new byte[Xid.MAXBQUALSIZE];
1984:
1985:                for (int i = 0; i < global_id.length; i++) {
1986:                    global_id[i] = b1;
1987:                }
1988:
1989:                for (int i = 0; i < branch_id.length; i++) {
1990:                    branch_id[i] = b2;
1991:                }
1992:            }
1993:
1994:            /**
1995:             * Obtain the format id part of the Xid.
1996:             * <p>
1997:             *
1998:             * @return Format identifier. O means the OSI CCR format.
1999:             **/
2000:            public int getFormatId() {
2001:                return (format_id);
2002:            }
2003:
2004:            /**
2005:             * Obtain the global transaction identifier part of XID as an array of 
2006:             * bytes.
2007:             * <p>
2008:             *
2009:             * @return A byte array containing the global transaction identifier.
2010:             **/
2011:            public byte[] getGlobalTransactionId() {
2012:                return (global_id);
2013:            }
2014:
2015:            /**
2016:             * Obtain the transaction branch qualifier part of the Xid in a byte array.
2017:             * <p>
2018:             *
2019:             * @return A byte array containing the branch qualifier of the transaction.
2020:             **/
2021:            public byte[] getBranchQualifier() {
2022:                return (branch_id);
2023:            }
2024:
2025:        }
2026:
2027:        class EventCatcher implements  ConnectionEventListener {
2028:            private final int catcher;
2029:
2030:            EventCatcher(int which) {
2031:                catcher = which;
2032:            }
2033:
2034:            // ConnectionEventListener methods
2035:            public void connectionClosed(ConnectionEvent event) {
2036:                System.out.print("EVENT(" + catcher + "):connectionClosed");
2037:                SQLException sqle = event.getSQLException();
2038:                if (sqle != null)
2039:                    System.out.print(" SQLSTATE=" + sqle.getSQLState());
2040:                System.out.println("");
2041:            }
2042:
2043:            public void connectionErrorOccurred(ConnectionEvent event) {
2044:                System.out.print("EVENT(" + catcher
2045:                        + "):connectionErrorOccurred");
2046:                SQLException sqle = event.getSQLException();
2047:                if (sqle != null)
2048:                    System.out.print(" SQLSTATE=" + sqle.getSQLState());
2049:                System.out.println("");
2050:
2051:            }
2052:
2053:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.