コード例 #1
0
  public void populateHistory(Database dbp) {
    Histrec hrec = new Histrec();
    hrec.set_amount(10);

    byte[] arr = new byte[4]; // sizeof(int)
    int i;
    DatabaseEntry kdbt = new DatabaseEntry(arr);
    kdbt.setSize(arr.length);
    DatabaseEntry ddbt = new DatabaseEntry(hrec.data);
    ddbt.setSize(hrec.data.length);

    try {
      for (i = 1; i <= history; i++) {
        kdbt.setRecordNumber(i);

        hrec.set_aid(random_id(ACCOUNT));
        hrec.set_bid(random_id(BRANCH));
        hrec.set_tid(random_id(TELLER));

        dbp.append(null, kdbt, ddbt);
      }
    } catch (DatabaseException dbe) {
      errExit(dbe, "Failure initializing history file");
    }
  }
コード例 #2
0
    //
    // XXX Figure out the appropriate way to pick out IDs.
    //
    int txn() {
      Cursor acurs = null;
      Cursor bcurs = null;
      Cursor hcurs = null;
      Cursor tcurs = null;
      Transaction t = null;

      Defrec rec = new Defrec();
      Histrec hrec = new Histrec();
      int account, branch, teller;

      DatabaseEntry d_dbt = new DatabaseEntry();
      DatabaseEntry d_histdbt = new DatabaseEntry();
      DatabaseEntry k_dbt = new DatabaseEntry();
      DatabaseEntry k_histdbt = new DatabaseEntry();

      account = TpcbExample.this.random_id(TpcbExample.ACCOUNT);
      branch = TpcbExample.this.random_id(TpcbExample.BRANCH);
      teller = TpcbExample.this.random_id(TpcbExample.TELLER);

      // The history key will not actually be retrieved,
      // but it does need to be set to something.
      byte[] hist_key = new byte[4];
      k_histdbt.setData(hist_key);
      k_histdbt.setSize(4 /* == sizeof(int)*/);

      byte[] key_bytes = new byte[4];
      k_dbt.setData(key_bytes);
      k_dbt.setSize(4 /* == sizeof(int)*/);

      d_dbt.setData(rec.data);
      d_dbt.setUserBuffer(rec.length(), true);

      hrec.set_aid(account);
      hrec.set_bid(branch);
      hrec.set_tid(teller);
      hrec.set_amount(10);
      // Request 0 bytes since we're just positioning.
      d_histdbt.setPartial(0, 0, true);

      // START PER-TRANSACTION TIMING.
      //
      // Technically, TPCB requires a limit on response time, you only
      // get to count transactions that complete within 2 seconds.
      // That's not an issue for this sample application -- regardless,
      // here's where the transaction begins.
      try {
        t = dbenv.beginTransaction(null, null);

        acurs = adb.openCursor(t, null);
        bcurs = bdb.openCursor(t, null);
        tcurs = tdb.openCursor(t, null);
        hcurs = hdb.openCursor(t, null);

        // Account record
        k_dbt.setRecordNumber(account);
        if (acurs.getSearchKey(k_dbt, d_dbt, null) != OperationStatus.SUCCESS)
          throw new Exception("acurs get failed");
        rec.set_balance(rec.get_balance() + 10);
        acurs.putCurrent(d_dbt);

        // Branch record
        k_dbt.setRecordNumber(branch);
        if (bcurs.getSearchKey(k_dbt, d_dbt, null) != OperationStatus.SUCCESS)
          throw new Exception("bcurs get failed");
        rec.set_balance(rec.get_balance() + 10);
        bcurs.putCurrent(d_dbt);

        // Teller record
        k_dbt.setRecordNumber(teller);
        if (tcurs.getSearchKey(k_dbt, d_dbt, null) != OperationStatus.SUCCESS)
          throw new Exception("ccurs get failed");
        rec.set_balance(rec.get_balance() + 10);
        tcurs.putCurrent(d_dbt);

        // History record
        d_histdbt.setPartial(0, 0, false);
        d_histdbt.setData(hrec.data);
        d_histdbt.setUserBuffer(hrec.length(), true);
        if (hdb.append(t, k_histdbt, d_histdbt) != OperationStatus.SUCCESS)
          throw new DatabaseException("put failed");

        acurs.close();
        acurs = null;
        bcurs.close();
        bcurs = null;
        tcurs.close();
        tcurs = null;
        hcurs.close();
        hcurs = null;

        // null out t in advance; if the commit fails,
        // we don't want to abort it in the catch clause.
        Transaction tmptxn = t;
        t = null;
        tmptxn.commit();

        // END TIMING
        return (0);
      } catch (Exception e) {
        try {
          if (acurs != null) acurs.close();
          if (bcurs != null) bcurs.close();
          if (tcurs != null) tcurs.close();
          if (hcurs != null) hcurs.close();
          if (t != null) t.abort();
        } catch (DatabaseException dbe) {
          // not much we can do here.
        }

        if (TpcbExample.this.verbose) {
          System.out.println(
              "Transaction A="
                  + String.valueOf(account)
                  + " B="
                  + String.valueOf(branch)
                  + " T="
                  + String.valueOf(teller)
                  + " failed");
          System.out.println("Reason: " + e.toString());
        }
        return (-1);
      }
    }