gt:         Statement s1 = c1.createStatement();
context: /*
   * This file is part of Splice Machine.
   * Splice Machine is free software: you can redistribute it and/or modify it under the terms of the
   * GNU Affero General Public License as published by the Free Software Foundation, either
   * version 3, or (at your option) any later version.
   * Splice Machine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU Affero General Public License for more details.
   * You should have received a copy of the GNU Affero General Public License along with Splice Machine.
   * If not, see <http://www.gnu.org/licenses/>.
   *
   * Some parts of this source code are based on Apache Derby, and the following notices apply to
   * Apache Derby:
   *
   * Apache Derby is a subproject of the Apache DB project, and is licensed under
   * the Apache License, Version 2.0 (the "License"); you may not use these files
   * except in compliance with the License. You may obtain a copy of the License at:
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed
   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
   * specific language governing permissions and limitations under the License.
   *
   * Splice Machine, Inc. has modified the Apache Derby code in this file.
   *
   * All such Splice Machine modifications are Copyright 2012 - 2020 Splice Machine, Inc.,
   * and are licensed to you under the GNU Affero General Public License.
   */
  
  package com.splicemachine.dbTesting.functionTests.tests.lang;
  
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.sql.Connection;
  import java.util.Properties;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import com.splicemachine.dbTesting.junit.JDBC;
  import com.splicemachine.dbTesting.junit.BaseJDBCTestCase;
  import com.splicemachine.dbTesting.junit.TestConfiguration;
  import com.splicemachine.dbTesting.junit.DatabasePropertyTestSetup;
  import com.splicemachine.dbTesting.junit.CleanDatabaseTestSetup;
  
  /**
   * Tests the lazy creation functionality of default schema: the schema
   * is only first created when the first database object is created in
   * the schema.
   */
  public class LazyDefaultSchemaCreationTest extends BaseJDBCTestCase {
  
      final private static String LOCK_TIMEOUT = "40XL1";
  
      /**
       * Creates a new {@code LazyDefaultSchemaCreationTest} instance.
       *
       * @param name the name of the test
       */
      public LazyDefaultSchemaCreationTest(String name) {
          super(name);
      }
  
  
      /**
       * Reproduces hang seen in DERBY-48
       */
      public void testDerby48testNewSchemaHang () throws SQLException
      {
          Connection c1 = openUserConnection("newuser");
          c1.setAutoCommit(false);
          Statement s1 = c1.createStatement();
  
          // Will auto-create schema NEWUSER:
          s1.executeUpdate("create table t1(i int)");
          s1.close();
  
          // DERBY-48: The next connect causes a hang on write lock the
          // new schema row being created by c1 that is not yet
          // committed if the fix for DERBY-48 is not yet in place.
          // The fix makes the the auto-create happen in a nested transaction
          // which commit immediately, so the hang should not be present.
  
          Connection c2 = null;
  
          try {
              c2 = openUserConnection("newuser");
          } catch (SQLException e) {
              if (e.getSQLState().equals(LOCK_TIMEOUT)) {
                  c1.rollback();
                  c1.close();
                  fail("DERBY-48 still seen", e);
              } else {
                  throw e;
              }
          }
  
          c1.rollback();
  
          // Since the auto-create happened in a nested transaction
          // which has committed, the schema should still be around
          // after the rollback. Note that this is a side-effect of the
          // fix for DERBY-48, not required behavior for SQL, but it is
          // user visible behavior, so we test it here to make sure that
          // patch works as intended:
  
          JDBC.assertSingleValueResultSet(
              c1.createStatement().executeQuery(
                  "select schemaname from sys.sysschemas " +
                  "where schemaname='NEWUSER'"),
              "NEWUSER");
  
          c1.rollback();
  
          c1.close();
          c2.close();
      }
  
      /**
       * Test that we recover from self locking in the auto-create
       * nested transaction (cf solution for DERBY-48).
       */
      public void testDerby48SelfLockingRecovery () throws SQLException
      {
          Connection c1 = openUserConnection("newuser");
          c1.setAutoCommit(false);
          c1.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
          Statement s1 = c1.createStatement();
  
          // Set read locks in parent transaction
          s1.executeQuery("select count(*) from sys.sysschemas");
  
          // ..which conflicts with the auto-create in a subtransaction
          // which will self-lock here, but should recover to try again
          // in outer transaction:
          s1.executeUpdate("create table t1(i int)");
  
          JDBC.assertSingleValueResultSet(
              s1.executeQuery(
                  "select schemaname from sys.sysschemas " +
                  "where schemaname='NEWUSER'"),
              "NEWUSER");
  
          c1.rollback();
  
          // Since the fallback does the auto-create of the schema in
          // the outer transaction, a rollback will remove it:
          JDBC.assertEmpty(
              s1.executeQuery
              ("select * from sys.sysschemas where schemaname='NEWUSER'"));
  
          c1.rollback();
      }
  
      /**
       * Test that we do get to see the self locking in the auto-create
       * nested transaction (cf solution for DERBY-48) when deadlock
       * detection is on, i.e. 40XL2 (LOCK_TIMEOUT_LOG) rather than
       * 40XL1 (LOCK_TIMEOUT) happens.
       *
       * After fix for DERBY-5564 LOCK_TIMEOUT will be returned whether
       * diagnostics are on or not.
       */
      public void testDerby48SelfLockingRecoveryDeadlockDetectionOn ()
              throws SQLException
      {
          Connection c1 = openUserConnection("newuser");
          c1.setAutoCommit(false);
          c1.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
          Statement s1 = c1.createStatement();
  
          // Set read locks in parent transaction
          s1.executeQuery("select count(*) from sys.sysschemas");
  
          // ..which conflicts with the auto-create in a subtransaction
          // which will self-lock here, but should throw now:
          // in outer transaction:
          try {
              s1.executeUpdate("create table t1(i int)");
              fail("Expected exception " + LOCK_TIMEOUT);
          } catch (SQLException e) {
              assertSQLState("Expected state: ", LOCK_TIMEOUT, e);
          }
  
          JDBC.assertEmpty(
              s1.executeQuery
              ("select * from sys.sysschemas where schemaname='NEWUSER'"));
  
          c1.rollback();
      }
  
      /**
       * Test that the timeout lock diagnostics do not create an
       * infinite recursion as in DERBY-3678 (although that particular
       * use case will not cause an infinite recursion after the fix to
       * DERBY-48). The scenario in this test case does create the
       * infinite recursion prior to the fix of DERBY-3678, however.
       *
       * After fix for DERBY-5564 LOCK_TIMEOUT SQL state should be returned
       * for a lock timeout whether diagnostics are on or not.
       */
      public void testDerby3678 ()
              throws SQLException
      {
          Connection c1 = openUserConnection("newuser");
          Connection c2 = null;
  
          c1.setAutoCommit(false);
  