Ejemplo n.º 1
0
  @Test
  public void testReadOwnWrites() throws Exception {
    String selectSql = "SELECT * FROM " + FULL_TABLE_NAME;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
      conn.setAutoCommit(false);
      ResultSet rs = conn.createStatement().executeQuery(selectSql);
      assertFalse(rs.next());

      String upsert =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk) VALUES(?, ?, ?, ?, ?, ?)";
      PreparedStatement stmt = conn.prepareStatement(upsert);
      // upsert two rows
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.execute();
      TestUtil.setRowKeyColumns(stmt, 2);
      stmt.execute();

      // verify rows can be read even though commit has not been called
      rs = conn.createStatement().executeQuery(selectSql);
      TestUtil.validateRowKeyColumns(rs, 1);
      TestUtil.validateRowKeyColumns(rs, 2);
      assertFalse(rs.next());

      conn.commit();

      // verify rows can be read after commit
      rs = conn.createStatement().executeQuery(selectSql);
      TestUtil.validateRowKeyColumns(rs, 1);
      TestUtil.validateRowKeyColumns(rs, 2);
      assertFalse(rs.next());
    }
  }
Ejemplo n.º 2
0
  @Test
  public void testTxnClosedCorrecty() throws Exception {
    String selectSql = "SELECT * FROM " + FULL_TABLE_NAME;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
      conn.setAutoCommit(false);
      ResultSet rs = conn.createStatement().executeQuery(selectSql);
      assertFalse(rs.next());

      String upsert =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk) VALUES(?, ?, ?, ?, ?, ?)";
      PreparedStatement stmt = conn.prepareStatement(upsert);
      // upsert two rows
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.execute();
      TestUtil.setRowKeyColumns(stmt, 2);
      stmt.execute();

      // verify rows can be read even though commit has not been called
      rs = conn.createStatement().executeQuery(selectSql);
      TestUtil.validateRowKeyColumns(rs, 1);
      TestUtil.validateRowKeyColumns(rs, 2);
      assertFalse(rs.next());

      conn.close();
      // wait for any open txns to time out
      Thread.sleep(DEFAULT_TXN_TIMEOUT_SECONDS * 1000 + 10000);
      assertTrue("There should be no invalid transactions", txManager.getInvalidSize() == 0);
    }
  }
Ejemplo n.º 3
0
  @Test
  public void testInflightDeleteNotSeen() throws Exception {
    String selectSQL = "SELECT * FROM " + FULL_TABLE_NAME;
    try (Connection conn1 = DriverManager.getConnection(getUrl());
        Connection conn2 = DriverManager.getConnection(getUrl())) {
      conn1.setAutoCommit(false);
      conn2.setAutoCommit(true);
      ResultSet rs = conn1.createStatement().executeQuery(selectSQL);
      assertFalse(rs.next());

      String upsert =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk) VALUES(?, ?, ?, ?, ?, ?)";
      PreparedStatement stmt = conn1.prepareStatement(upsert);
      // upsert two rows
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.execute();
      TestUtil.setRowKeyColumns(stmt, 2);
      stmt.execute();

      conn1.commit();

      rs = conn1.createStatement().executeQuery("SELECT count(*) FROM " + FULL_TABLE_NAME);
      assertTrue(rs.next());
      assertEquals(2, rs.getInt(1));

      String delete = "DELETE FROM " + FULL_TABLE_NAME + " WHERE varchar_pk = 'varchar1'";
      stmt = conn1.prepareStatement(delete);
      int count = stmt.executeUpdate();
      assertEquals(1, count);

      rs = conn1.createStatement().executeQuery("SELECT count(*) FROM " + FULL_TABLE_NAME);
      assertTrue(rs.next());
      assertEquals(1, rs.getInt(1));
      assertFalse(rs.next());

      rs = conn2.createStatement().executeQuery("SELECT count(*) FROM " + FULL_TABLE_NAME);
      assertTrue(rs.next());
      assertEquals(2, rs.getInt(1));
      assertFalse(rs.next());

      conn1.commit();

      rs = conn2.createStatement().executeQuery("SELECT count(*) FROM " + FULL_TABLE_NAME);
      assertTrue(rs.next());
      assertEquals(1, rs.getInt(1));
      assertFalse(rs.next());
    }
  }
Ejemplo n.º 4
0
  public void testCurrentDate() throws Exception {
    String selectSql = "SELECT current_date() FROM " + FULL_TABLE_NAME;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
      conn.setAutoCommit(false);
      ResultSet rs = conn.createStatement().executeQuery(selectSql);
      assertFalse(rs.next());

      String upsert =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk) VALUES(?, ?, ?, ?, ?, ?)";
      PreparedStatement stmt = conn.prepareStatement(upsert);
      // upsert two rows
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.execute();
      conn.commit();

      rs = conn.createStatement().executeQuery(selectSql);
      assertTrue(rs.next());
      Date date1 = rs.getDate(1);
      assertFalse(rs.next());

      Thread.sleep(1000);

      rs = conn.createStatement().executeQuery(selectSql);
      assertTrue(rs.next());
      Date date2 = rs.getDate(1);
      assertFalse(rs.next());
      assertTrue(
          "current_date() should change while executing multiple statements",
          date2.getTime() > date1.getTime());
    }
  }
Ejemplo n.º 5
0
  private void testRowConflicts() throws Exception {
    try (Connection conn1 = DriverManager.getConnection(getUrl());
        Connection conn2 = DriverManager.getConnection(getUrl())) {
      conn1.setAutoCommit(false);
      conn2.setAutoCommit(false);
      String selectSql = "SELECT * FROM " + FULL_TABLE_NAME;
      conn1.setAutoCommit(false);
      ResultSet rs = conn1.createStatement().executeQuery(selectSql);
      boolean immutableRows =
          conn1
              .unwrap(PhoenixConnection.class)
              .getTable(new PTableKey(null, FULL_TABLE_NAME))
              .isImmutableRows();
      assertFalse(rs.next());
      // upsert row using conn1
      String upsertSql =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk, a.int_col1) VALUES(?, ?, ?, ?, ?, ?, ?)";
      PreparedStatement stmt = conn1.prepareStatement(upsertSql);
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.setInt(7, 10);
      stmt.execute();
      // upsert row using conn2
      upsertSql =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk, b.int_col2) VALUES(?, ?, ?, ?, ?, ?, ?)";
      stmt = conn2.prepareStatement(upsertSql);
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.setInt(7, 11);
      stmt.execute();

      conn1.commit();
      // second commit should fail
      try {
        conn2.commit();
        if (!immutableRows) fail();
      } catch (SQLException e) {
        if (immutableRows) fail();
        assertEquals(
            e.getErrorCode(), SQLExceptionCode.TRANSACTION_CONFLICT_EXCEPTION.getErrorCode());
      }
    }
  }
Ejemplo n.º 6
0
  @Test
  public void testDelete() throws Exception {
    String selectSQL = "SELECT * FROM " + FULL_TABLE_NAME;
    try (Connection conn1 = DriverManager.getConnection(getUrl());
        Connection conn2 = DriverManager.getConnection(getUrl())) {
      conn1.setAutoCommit(false);
      ResultSet rs = conn1.createStatement().executeQuery(selectSQL);
      assertFalse(rs.next());

      String upsert =
          "UPSERT INTO "
              + FULL_TABLE_NAME
              + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk) VALUES(?, ?, ?, ?, ?, ?)";
      PreparedStatement stmt = conn1.prepareStatement(upsert);
      // upsert two rows
      TestUtil.setRowKeyColumns(stmt, 1);
      stmt.execute();
      conn1.commit();

      TestUtil.setRowKeyColumns(stmt, 2);
      stmt.execute();

      // verify rows can be read even though commit has not been called
      int rowsDeleted = conn1.createStatement().executeUpdate("DELETE FROM " + FULL_TABLE_NAME);
      assertEquals(2, rowsDeleted);

      // Delete and second upsert not committed yet, so there should be one row.
      rs = conn2.createStatement().executeQuery("SELECT count(*) FROM " + FULL_TABLE_NAME);
      assertTrue(rs.next());
      assertEquals(1, rs.getInt(1));

      conn1.commit();

      // verify rows are deleted after commit
      rs = conn1.createStatement().executeQuery(selectSQL);
      assertFalse(rs.next());
    }
  }