예제 #1
0
 @Test
 public void testOtherDatabaseNames() throws SQLException {
   mockConnection.setEmptyResults();
   new Connection(mockConnection, "foobar").createStatement().executeQuery(SELECT);
   assertTrue(mockConnection.lastRequest.hasDatabaseName());
   assertEquals("foobar", mockConnection.lastRequest.getDatabaseName());
 }
예제 #2
0
  @Test
  public void testClientIds() throws SQLException {
    mockConnection.setEmptyResults();
    connection.createStatement().executeQuery(SELECT);
    int id1 = mockConnection.lastRequest.getTransactionId();

    new Connection(mockConnection, "test").createStatement().executeQuery(SELECT);
    int id2 = mockConnection.lastRequest.getTransactionId();
    assertTrue(id1 != id2);
  }
예제 #3
0
  @Test
  public void testFailedTransactionRollback() throws SQLException {
    connection.setAutoCommit(false);

    // Failed statement
    mockConnection.setError(42, "bad");
    Statement s = connection.createStatement();
    try {
      s.executeUpdate("DELETE bad");
      fail("expected exception");
    } catch (SQLException e) {
    }
    assertNull(mockConnection.lastFinish);

    // All future statements should also fail
    mockConnection.setUpdateCount(42);
    try {
      s.executeUpdate("DELETE good");
      fail("expected exception");
    } catch (SQLException e) {
    }

    connection.enableStrictMode(true);
    try {
      connection.commit();
      fail("expected exception");
    } catch (SQLException e) {
    }

    // rollback this transaction
    connection.rollback();
    assertNull(mockConnection.lastFinish);
    try {
      // Can't rollback: no transaction!
      connection.rollback();
      fail("expected exception");
    } catch (SQLException e) {
    }

    assertEquals(42, s.executeUpdate("DELETE good"));
  }
예제 #4
0
  @Test
  public void testDeadlockTransactionRollback() throws SQLException {
    connection.setAutoCommit(false);

    // Failed statement
    mockConnection.setError(Jdbc.ErrorCode.DEADLOCK.getNumber(), "deadlock");
    Statement s = connection.createStatement();
    try {
      s.executeUpdate("DELETE bad");
      fail("expected exception");
    } catch (SQLException e) {
      assertEquals(Jdbc.ErrorCode.DEADLOCK.getNumber(), e.getErrorCode());
      assertEquals("40001", e.getSQLState());
    }
    assertNull(mockConnection.lastFinish);
  }