Ejemplo n.º 1
0
  @Test
  public void testPrepareStatement() throws SQLException {
    PreparedStatement ps = connection.prepareStatement(SELECT);
    assertNotNull(ps);
    ps = connection.prepareStatement(INSERT, Statement.NO_GENERATED_KEYS);
    assertNotNull(ps);
    ps = connection.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
    assertNotNull(ps);

    // Ignored, despite this being a select
    ps = connection.prepareStatement(SELECT, Statement.NO_GENERATED_KEYS);
    assertNotNull(ps);
    connection.enableStrictMode(true);
    try {
      connection.prepareStatement(SELECT, Statement.NO_GENERATED_KEYS);
      fail("expected exception");
    } catch (IllegalArgumentException e) {
    }
  }
Ejemplo n.º 2
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"));
  }