// This is only a convenience method, because I like to keep track of the last error, with which // a transaction failed, in the status management. Makes it easier for administrators to fix the // problem and then retry that LUW. // public static void SetError(RfcTID tid, String errorMessage) { try { tidStore.SetStatus(tid.TID, TidStatus.RolledBack, errorMessage); } catch (Exception) {} }
//If DB is down, throw an exception at this point. .Net Connector will then abort //the tRFC and the R/3 backend will try again later. public bool CheckTransactionID(RfcServerContextInfo serverContext, RfcTID tid) { if (string.IsNullOrEmpty(serverContext.TransactionID.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID.TID in CheckTransactionID is null"), EventLogEntryType.Error); } if (tid == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in CheckTransactionID is null"), EventLogEntryType.Error); } if (tid.TID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in CheckTransactionID is null"), EventLogEntryType.Error); } if (tid.TID != null) { if (string.IsNullOrEmpty(tid.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID.ToString in CheckTransactionID is null or Empty"), EventLogEntryType.Error); } else { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("Clarks SAP Adapter TID set to:{0} in CheckTransactionID callback", tid.TID.ToString()), EventLogEntryType.Information); } } // Notify any registered client with sessionId if (IDocSessionStart != null) { SAPSessionEventArgs sapSessionEvents = new SAPSessionEventArgs(); sapSessionEvents.SessionId = serverContext.SessionID; IDocSessionStart(sapSessionEvents); } System.Diagnostics.Trace.WriteLine(string.Format("Rfc Function Activated with SessionId:{0}", serverContext.SessionID)); lock (tids) { if (tids.Contains(tid.TID)) { return(false); } else { tids.Add(tid.TID); return(true); } } // "true" means that NCo will now execute the transaction, "false" means // that we have already executed this transaction previously, so NCo will // skip the function execution step and will immediately return an OK code to R/3. }
public bool CheckTransactionID(RfcServerContextInfo serverContext, RfcTID tid) { Console.WriteLine(); Console.Write("TRFC: Checking transaction ID {0} --> ", tid.TID); TidStatus status = tidStore.CreateEntry(tid.TID); switch (status) { case TidStatus.Created: case TidStatus.RolledBack: // In these case we have to execute the tRFC LUW. Console.WriteLine("New transaction or one that had failed previously"); return(true); default: // In the remaining cases we have already executed this LUW successfully, so we // can (or rather have to) skip a second execution and send an OK code to R/3 immediately. Console.WriteLine("Already executed successfully"); return(false); } // "true" means that NCo will now execute the transaction, "false" means // that we have already executed this transaction previously, so NCo will // skip the function execution step and will immediately return an OK code to R/3. // In a real life scenario, if DB is down/unavailable, throw an exception at this point. // The .Net Connector will then abort the current tRFC request and the R/3 backend will // try again later. }
public bool CheckTransactionID(RfcServerContextInfo ctx, RfcTID tid) { Console.WriteLine(ctx.FunctionName); Console.WriteLine(); Console.Write("TRFC: Checking transaction ID {0} --> ", tid.TID); return(true); }
// clean up the resources public void ConfirmTransactionID(RfcServerContextInfo serverContext, RfcTID tid) { if (serverContext.TransactionID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID in ConfirmTransactionID is null"), EventLogEntryType.Error); } else { if (string.IsNullOrEmpty(serverContext.TransactionID.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID.TID in ConfirmTransactionID is null"), EventLogEntryType.Error); } } if (tid == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in ConfirmTransactionID is null"), EventLogEntryType.Error); } if (tid.TID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in ConfirmTransactionID is null"), EventLogEntryType.Error); } if (tid.TID != null) { if (string.IsNullOrEmpty(tid.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID.ToString in ConfirmTransactionID is null or Empty"), EventLogEntryType.Error); } else { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("Clarks SAP Adapter TID set to:{0} in ConfirmTransactionID callback", tid.TID.ToString()), EventLogEntryType.Information); } } try { //clean up the resources //partner won't react on an exception at this point } finally { lock (tids) { tids.Remove(tid.TID); } } }
// react on rollback e.g. rollback on the database public void Rollback(RfcServerContextInfo serverContext, RfcTID tid) { if (serverContext.TransactionID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID in Rollback is null"), EventLogEntryType.Error); } else { if (string.IsNullOrEmpty(serverContext.TransactionID.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID.TID in Rollback is null"), EventLogEntryType.Error); } } if (tid == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in Rollback is null"), EventLogEntryType.Error); throw new ApplicationException("tid in Rollback is null"); } if (tid.TID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in Rollback is null"), EventLogEntryType.Error); throw new ApplicationException("tid.TID in Rollback is null"); } if (tid.TID != null) { if (string.IsNullOrEmpty(tid.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID.ToString in Rollback is null or Empty"), EventLogEntryType.Error); throw new ApplicationException("tid.TID.ToString in Rollback is null or Empty"); } else { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("Clarks SAP Adapter TID set to:{0}", tid.TID.ToString()), EventLogEntryType.Information); } } Console.WriteLine("Rollback transaction ID " + tid); /* Make sure the TID is removed from the list, otherwise CheckTransactionID will * return false the next time the backend retries this failed transaction, and then * it will never be executed... */ tids.Remove(tid.TID); }
// react on commit e.g. commit on the database // if necessary throw an exception, if the commit was not possible public void Commit(RfcServerContextInfo serverContext, RfcTID tid) { if (serverContext.TransactionID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID in Commit is null"), EventLogEntryType.Error); } else { if (string.IsNullOrEmpty(serverContext.TransactionID.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("serverContext.TransactionID.TID in Commit is null"), EventLogEntryType.Error); } } if (tid == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in Commit is null"), EventLogEntryType.Error); throw new ApplicationException("tid in Commit is null"); } if (tid.TID == null) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID in Commit is null"), EventLogEntryType.Error); throw new ApplicationException("tid.TID in Commit is null"); } if (tid.TID != null) { if (string.IsNullOrEmpty(tid.TID.ToString())) { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("tid.TID.ToString in Commit is null or Empty"), EventLogEntryType.Error); throw new ApplicationException("tid.TID.ToString in Commit is null or Empty"); } else { EventLog.WriteEntry( "Clarks SAP Adapter", string.Format("Clarks SAP Adapter TID set to:{0} in Commit callback", tid.TID.ToString()), EventLogEntryType.Information); } } lock (tids) { if (!tids.Contains(tid.TID)) { throw new Exception("tid " + tid.TID + " doesn't exist, hence it cannot be committed"); } } }
// React to rollback, e.g. rollback on the database public void Rollback(RfcServerContextInfo serverContext, RfcTID tid) { Console.WriteLine(); Console.WriteLine("TRFC: Rollback transaction ID {0}", tid.TID); // Roll back all changes of the previous function modules in this LUW. // If this LUW contains only one function module, we could already do this in the processing // server function. // We assume that the error message for this TID has already been added to the TidStore at the // point where the error happened. // tidStore.SetStatus(tid.TID, TidStatus.RolledBack, null); }
// React to commit, e.g. commit to the database; // Throw an exception if committing failed public void Commit(RfcServerContextInfo serverContext, RfcTID tid) { Console.WriteLine(); Console.WriteLine("TRFC: Commit transaction ID {0}", tid.TID); // Do whatever is necessary to persist the data/changes of the function modules belonging to this LUW. // Throw an exception, if that fails. // If we know, that the LUWs that we are processing, consist of only a single function module, // the processing server function may already persist everything at the end and set the status // for that TID to Executed. // // No exception after this point try { tidStore.SetStatus(tid.TID, TidStatus.Committed, null); } catch (Exception) {} }
// Clean up the resources. Backend will never send this transaction again, so no need to // protect against it any longer. public void ConfirmTransactionID(RfcServerContextInfo serverContext, RfcTID tid) { Console.WriteLine(); Console.WriteLine("TRFC: Confirm transaction ID {0}", tid.TID); try { // Our implementation keeps the ones that failed, so an admin can later view error messages. string error = ""; TidStatus status = tidStore.GetStatus(tid.TID, out error); if (status == TidStatus.RolledBack) { return; } tidStore.DeleteEntry(tid.TID); } catch (Exception) { } }
public void Rollback(RfcServerContextInfo ctx, RfcTID tid) { Console.WriteLine(); Console.WriteLine("TRFC: Rollback transaction ID {0}", tid.TID); // throw new NotImplementedException(); }
public void Commit(RfcServerContextInfo ctx, RfcTID tid) { // throw new NotImplementedException(); Console.WriteLine(); Console.WriteLine("TRFC: Commit transaction ID {0}", tid.TID); }
public bool CheckTransactionID(RfcServerContextInfo ctx, RfcTID tid) { Console.WriteLine(ctx.FunctionName); Console.WriteLine(); Console.Write("TRFC: Checking transaction ID {0} --> ", tid.TID); return true; }