Exemplo n.º 1
0
  @Test
  public void testHeuristicRollbackBeforePrepare() throws JAXBException {
    txSupport.startTx();

    final List<LoggingParticipant> participants =
        Arrays.asList(
            new LoggingParticipant[] {
              new LoggingParticipant(new Prepared()), new LoggingParticipant(new Prepared())
            });

    String lastParticipantid = null;

    for (LoggingParticipant p : participants) {
      lastParticipantid =
          ParticipantsManagerFactory.getInstance()
              .enlist(APPLICATION_ID, txSupport.getDurableParticipantEnlistmentURI(), p);
    }

    ParticipantsManagerFactory.getInstance()
        .reportHeuristic(lastParticipantid, HeuristicType.HEURISTIC_ROLLBACK);

    final String transactionStatus = TxSupport.getStatus(txSupport.commitTx());

    Assert.assertEquals(TxStatus.TransactionRolledBack.name(), transactionStatus);

    if (participants.get(0).getInvocations().size() == 1) {
      Assert.assertEquals(
          Arrays.asList(new String[] {"rollback"}), participants.get(0).getInvocations());
    } else {
      Assert.assertEquals(
          Arrays.asList(new String[] {"prepare", "rollback"}),
          participants.get(0).getInvocations());
    }
  }
Exemplo n.º 2
0
    @SuppressWarnings({"UnusedDeclaration"})
    @PUT
    @Path("{pId}/{tId}/terminator")
    public Response terminate(
        @PathParam("pId") @DefaultValue("") String pId,
        @PathParam("tId") @DefaultValue("") String tId,
        String content) {
      TxStatus status = TxSupport.toTxStatus(content);

      // String status = TxSupport.getStatus(content);
      Work work = faults.get(pId);

      if (work == null) return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build();

      String fault = work.fault;

      if (status.isPrepare()) {
        if ("READONLY".equals(fault)) {
          //                    faults.remove(pId);
          work.status = TxStatus.TransactionReadOnly.name();
        } else if ("PREPARE_FAIL".equals(fault)) {
          //                    faults.remove(pId);
          return Response.status(HttpURLConnection.HTTP_CONFLICT).build();
          // throw new WebApplicationException(HttpURLConnection.HTTP_CONFLICT);
        } else {
          if ("PDELAY".equals(fault)) {
            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
          }
          work.status = TxStatus.TransactionPrepared.name();
        }
      } else if (status.isCommit() || status.isCommitOnePhase()) {
        if ("H_HAZARD".equals(fault)) work.status = TxStatus.TransactionHeuristicHazard.name();
        else if ("H_ROLLBACK".equals(fault))
          work.status = TxStatus.TransactionHeuristicRollback.name();
        else if ("H_MIXED".equals(fault)) work.status = TxStatus.TransactionHeuristicMixed.name();
        else {
          if ("CDELAY".equals(fault)) {
            try {
              Thread.sleep(3000);
            } catch (InterruptedException e) {
              // ok
            }
          }
          work.status =
              status.isCommitOnePhase()
                  ? TxStatus.TransactionCommittedOnePhase.name()
                  : TxStatus.TransactionCommitted.name();

          work.end(true);
        }
      } else if (status.isAbort()) {
        if ("H_HAZARD".equals(fault)) work.status = TxStatus.TransactionHeuristicHazard.name();
        else if ("H_COMMIT".equals(fault)) work.status = TxStatus.TransactionHeuristicCommit.name();
        else if ("H_MIXED".equals(fault)) work.status = TxStatus.TransactionHeuristicMixed.name();
        else {
          if ("ADELAY".equals(fault)) {
            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
              // ok
            }
          }
          work.status = TxStatus.TransactionRolledBack.name();
          work.end(false);
          //                    faults.remove(pId);
        }
      } else {
        return Response.status(HttpURLConnection.HTTP_BAD_REQUEST).build();
        // throw new WebApplicationException(HttpURLConnection.HTTP_BAD_REQUEST);
      }

      // return TxSupport.toStatusContent(work.status);
      return Response.ok(TxSupport.toStatusContent(work.status)).build();
    }