Program: 155509
Project: 155509
Buggy source:
/** 
 * Enlist the specified   {@link XAResourceHolderState}. A XID is generated and the resource is started with XAResource.TMNOFLAGS or XAResource.TMJOIN if it could be joined with another previously enlisted one. <br/> There are 3 different cases that can happen when a   {@link XAResourceHolderState} is enlisted:<ul> <li>already enlisted and not ended: do nothing</li> <li>already enlisted and ended: try to join. if you can join, keep a reference on the passed-in {@link XAResourceHolderState} and drop the previous one. if you cannot join, it's the same as case 3</li><li>not enlisted: create a new branch and keep a reference on the passed-in  {@link XAResourceHolderState}</li> </ul>
 * @param xaResourceHolderState the {@link XAResourceHolderState} to be enlisted.
 * @throws XAException if a resource error occured.
 * @throws BitronixSystemException if an internal error occured.
 */
public void enlist(XAResourceHolderState xaResourceHolderState) throws XAException, BitronixSystemException {
  XAResourceHolderState alreadyEnlistedHolder=findXAResourceHolderState(xaResourceHolderState.getXAResource());
  if (alreadyEnlistedHolder != null && !alreadyEnlistedHolder.isEnded()) {
    xaResourceHolderState.setXid(alreadyEnlistedHolder.getXid());
    log.warn("String_Node_Str" + xaResourceHolderState);
    return;
  }
  XAResourceHolderState toBeJoinedHolderState=null;
  if (alreadyEnlistedHolder != null) {
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + alreadyEnlistedHolder);
    toBeJoinedHolderState=getManagedResourceWithSameRM(xaResourceHolderState);
  }
  BitronixXid xid;
  int flag;
  if (toBeJoinedHolderState != null) {
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + xaResourceHolderState + "String_Node_Str"+ toBeJoinedHolderState);
    xid=toBeJoinedHolderState.getXid();
    flag=XAResource.TMJOIN;
  }
 else {
    xid=UidGenerator.generateXid(gtrid);
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + xid);
    flag=XAResource.TMNOFLAGS;
  }
  if (flag != XAResource.TMJOIN && xaResourceHolderState.getTwoPcOrderingPosition() == ResourceScheduler.ALWAYS_LAST_POSITION) {
    List alwaysLastResources=resources.getNaturalOrderResourcesForPosition(ResourceScheduler.ALWAYS_LAST_POSITION_KEY);
    if (alwaysLastResources != null && alwaysLastResources.size() > 0)     throw new BitronixSystemException("String_Node_Str" + xaResourceHolderState + "String_Node_Str"+ alwaysLastResources.get(0));
  }
  xaResourceHolderState.setXid(xid);
  xaResourceHolderState.start(flag);
  if (toBeJoinedHolderState == null) {
    resources.addResource(xaResourceHolderState);
  }
}

Fixed version (ground truth):
/** 
 * Enlist the specified   {@link XAResourceHolderState}. A XID is generated and the resource is started with XAResource.TMNOFLAGS or XAResource.TMJOIN if it could be joined with another previously enlisted one. <br/> There are 3 different cases that can happen when a   {@link XAResourceHolderState} is enlisted:<ul> <li>already enlisted and not ended: do nothing</li> <li>already enlisted and ended: try to join. if you can join, keep a reference on the passed-in {@link XAResourceHolderState} and drop the previous one. if you cannot join, it's the same as case 3</li><li>not enlisted: create a new branch and keep a reference on the passed-in  {@link XAResourceHolderState}</li> </ul>
 * @param xaResourceHolderState the {@link XAResourceHolderState} to be enlisted.
 * @throws XAException if a resource error occured.
 * @throws BitronixSystemException if an internal error occured.
 */
public void enlist(XAResourceHolderState xaResourceHolderState) throws XAException, BitronixSystemException {
  XAResourceHolderState alreadyEnlistedHolder=findXAResourceHolderState(xaResourceHolderState.getXAResource());
  if (alreadyEnlistedHolder != null && !alreadyEnlistedHolder.isEnded()) {
    xaResourceHolderState.setXid(alreadyEnlistedHolder.getXid());
    log.warn("String_Node_Str" + xaResourceHolderState);
    return;
  }
  XAResourceHolderState toBeJoinedHolderState=null;
  if (alreadyEnlistedHolder != null) {
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + alreadyEnlistedHolder);
    toBeJoinedHolderState=getManagedResourceWithSameRM(xaResourceHolderState);
  }
  BitronixXid xid;
  int flag;
  if (toBeJoinedHolderState != null) {
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + xaResourceHolderState + "String_Node_Str"+ toBeJoinedHolderState);
    xid=toBeJoinedHolderState.getXid();
    flag=XAResource.TMJOIN;
  }
 else {
    xid=UidGenerator.generateXid(gtrid);
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + xid);
    flag=XAResource.TMNOFLAGS;
  }
  if (flag != XAResource.TMJOIN && xaResourceHolderState.getTwoPcOrderingPosition() == ResourceScheduler.ALWAYS_LAST_POSITION) {
    List alwaysLastResources=resources.getNaturalOrderResourcesForPosition(ResourceScheduler.ALWAYS_LAST_POSITION_KEY);
    if (alwaysLastResources != null && alwaysLastResources.size() > 0)     throw new BitronixSystemException("String_Node_Str" + xaResourceHolderState + "String_Node_Str"+ alwaysLastResources.get(0));
  }
  xaResourceHolderState.setXid(xid);
  xaResourceHolderState.start(flag);
  if (log.isDebugEnabled())   log.debug("String_Node_Str" + xaResourceHolderState + "String_Node_Str"+ Decoder.decodeXAResourceFlag(flag));
  if (toBeJoinedHolderState != null) {
    resources.removeResource(toBeJoinedHolderState);
  }
  resources.addResource(xaResourceHolderState);
}