public boolean commit(int xid)
      throws RemoteException, TransactionAbortedException, InvalidTransactionException {
    System.out.println("TM starts to commit " + xid);
    if (!xidIsValid(xid)) {
      File f = new File(logFile);
      if (f.exists()) throw new TransactionAbortedException(xid, "");
      else throw new InvalidTransactionException(xid, "");
    }
    if (!enlistList.containsKey(xid)) {
      throw new TransactionAbortedException(xid, "Nonexist xid for TM.");
    }

    Set<ResourceManager> commitSet = enlistList.get(xid);
    System.out.println(xid + " TM commitSet size: " + commitSet.size());
    ResourceManager rm = null;
    Iterator<ResourceManager> i = commitSet.iterator();
    try {
      while (i.hasNext()) {
        rm = i.next();
        rm.vote(xid, TwoPC_ST.Prepared);
        rm.getMyRMIName();
        System.out.println(rm.getMyRMIName() + " in TM commit " + xid);
      }
    } catch (Exception e) {
      System.out.println("When vote, there is an exception");
      enlistList.get(xid).remove(rm);
      abort(xid);
      throw new TransactionAbortedException(xid, "invalid rm");
    }

    if (dieTMBeforeCommit) dieNow();

    // log "committed" here, and enlistList
    try {
      File logF = new File(logFile);
      FileWriter fw = new FileWriter(logF.getAbsoluteFile(), true);
      bw = new BufferedWriter(fw);
      bw.write(Integer.toString(xid));
      bw.write(" committed\n");
      bw.close();

      ObjectOutputStream protocolDBBackup =
          new ObjectOutputStream(new FileOutputStream(protocolDBFile));
      protocolDBBackup.writeObject(protocolDB);
      protocolDBBackup.close();

      ObjectOutputStream enlistFileBackup =
          new ObjectOutputStream(new FileOutputStream(enlistFile));
      enlistFileBackup.writeObject(enlistList);
      enlistFileBackup.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    if (dieTMAfterCommit) dieNow();
    for (ResourceManager r : commitSet) {
      r.commit(xid);
    }
    protocolDB.remove(xid);
    enlistList.remove(xid);
    try {
      ObjectOutputStream protocolDBBackup =
          new ObjectOutputStream(new FileOutputStream(protocolDBFile));
      protocolDBBackup.writeObject(protocolDB);
      protocolDBBackup.close(); // enlistFile

      ObjectOutputStream enlistFileBackup =
          new ObjectOutputStream(new FileOutputStream(enlistFile));
      enlistFileBackup.writeObject(enlistList);
      enlistFileBackup.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return true;
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    Ping obj = null;
    Registry registry = null;

    try {
      /*
       * create registry
       */
      TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");

      System.err.println("creating Registry...");

      registry = TestLibrary.createRegistryOnUnusedPort();
      int port = TestLibrary.getRegistryPort(registry);
      /*
       * create object with custom ref and bind in registry
       */
      System.err.println("creating UseCustomRef...");
      UseCustomRef cr = new UseCustomRef();
      RemoteRef ref = cr.getRef();
      if (!(ref instanceof CustomServerRef)) {
        TestLibrary.bomb("test failed: reference not " + "instanceof CustomServerRef");
      }

      String name = "//:" + port + "/UseCustomRef";
      //      String name = "UseCustomRef";
      System.err.println("binding object in registry...");
      Naming.rebind(name, cr);

      /*
       * look up object and invoke its ping method
       */
      System.err.println("ping object...");
      obj = (Ping) Naming.lookup(name);
      obj.ping();

      /*
       * pass object with custom ref in remote call
       */
      System.err.println("pass object in remote call...");
      obj.receiveAndPing(cr);

      /*
       * write remote object with custom ref to output stream
       */
      System.err.println("writing remote object to stream...");
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(bout);
      out.writeObject(cr);
      out.flush();
      out.close();

      /*
       * read back remote object from output stream
       */
      System.err.println("reading remote object from stream...");
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
      cr = (UseCustomRef) in.readObject();

      /*
       * re-export object and ping
       */
      System.err.println("re-export object read...");
      cr.exportObject();
      System.err.println("look up object again...");
      Naming.rebind(name, cr);
      System.err.println("ping object read...");
      obj = (Ping) Naming.lookup(name);
      obj.ping();
      System.err.println("TEST PASSED");
      Naming.unbind(name);
      cr = null;

    } catch (Exception e) {
      TestLibrary.bomb("test failed with exception: ", e);
    } finally {
      TestLibrary.unexport(obj);
      TestLibrary.unexport(registry);

      registry = null;
      obj = null;
    }
  }