Exemplo n.º 1
0
  @Test
  public void testEmptyCommit() throws SQLException {
    // Empty auto commit commit
    connection.setAutoCommit(true);
    connection.commit();

    // Empty commit
    connection.setAutoCommit(false);
    connection.commit();
  }
  protected static void initTableValues(byte[][] splits, long ts) throws Exception {
    ensureTableCreated(getUrl(), "LongInKeyTest", splits, ts - 2);

    // Insert all rows at ts
    String url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts;
    Connection conn = DriverManager.getConnection(url);
    conn.setAutoCommit(true);
    PreparedStatement stmt = conn.prepareStatement("upsert into " + "LongInKeyTest VALUES(?)");
    stmt.setLong(1, 2);
    stmt.execute();
    conn.close();
  }
Exemplo n.º 3
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);
  }
Exemplo n.º 4
0
  @Test
  public void insert1000() throws SQLException {
    stat.executeUpdate("create table in1000 (a);");
    PreparedStatement prep = conn.prepareStatement("insert into in1000 values (?);");
    conn.setAutoCommit(false);
    for (int i = 0; i < 1000; i++) {
      prep.setInt(1, i);
      prep.executeUpdate();
    }
    conn.commit();

    ResultSet rs = stat.executeQuery("select count(a) from in1000;");
    assertTrue(rs.next());
    assertEquals(1000, rs.getInt(1));
    rs.close();
  }
Exemplo n.º 5
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"));
  }
Exemplo n.º 6
0
  @Test
  public void createEncrypted() throws SQLException, IOException {
    File tmp = File.createTempFile("sqlitetest", ".db");
    tmp.deleteOnExit();
    String url = "jdbc:sqlite:" + tmp.getAbsolutePath();

    Properties props = new Properties();
    final String password = "******"dog";
    props.put("key", password);
    Connection conn = DriverManager.getConnection(url, props);
    conn.setAutoCommit(false);

    Statement st = conn.createStatement();
    st.executeUpdate("create table ants (col int)");
    st.executeUpdate("insert into ants values( 300 )");
    st.executeUpdate("insert into ants values( 400 )");
    st.close();
    conn.commit();
    conn.close();

    // Try reading without key.
    props.remove("key");
    conn = DriverManager.getConnection(url, props);

    try {
      st = conn.createStatement();
      ResultSet rs = st.executeQuery("select count(*) from ants");
      fail("Database not encrypted.");
    } catch (SQLException ignore) {
    }

    conn.close();
    props.put("key", password);
    conn = DriverManager.getConnection(url, props);

    st = conn.createStatement();
    ResultSet rs = st.executeQuery("select count(*) from ants");
    assertTrue(rs.next());
    assertEquals(2, rs.getInt(1));
    conn.close();
  }