troubleshooting Question

Best Practise For Dealing With Connections in Timer

Avatar of Nasser Hamdan
Nasser HamdanFlag for United Arab Emirates asked on
C#
4 Comments1 Solution896 ViewsLast Modified:
I have an application that keep listeneing in the some table, and whenever its updated i send this update to another table, the problem iam registering the exception in event log, and i have alot of errors hapenning, some like "Connection is dead", "Connection already Open", "This OdbcTransaction has completed; it is no longer usable", and such and so, what i want know is, what is the best way to deal with ODBC connections in Timer applications ? and this is my class code:

public class OMSReciever
      {
            private OdbcConnection OMSCnn;
            private OdbcCommand OMSCmd;       

            private OdbcConnection BMSCnn;
            private OdbcCommand BMSCmd;

            private OdbcTransaction OMSTxn;
            private OdbcTransaction BMSTxn;

            private EventLog oEV = new EventLog();

            private FixListener.GeneralFunctions objGenF = new FixListener.GeneralFunctions();

            public OMSReciever()
            {
                                    
               

                  BMSCnn = new OdbcConnection("DSN=BMS;uid=omsuser;pwd=");
                  BMSCmd = BMSCnn.CreateCommand();

                  OMSCnn = new OdbcConnection("DSN=OMS;uid=omsuser;pwd=");
                  OMSCmd = BMSCnn.CreateCommand();

                  // Creating the logSource if not exist
                  if(!EventLog.SourceExists("FIXRecLogSource"))
                        EventLog.CreateEventSource("FIXRecLogSource",
                              "FIXRecLog");

                  oEV.Source = "FIXRecLogSource";                        
                  oEV.Log = "FIXRecLog";            

            }

public void HandleMessageReceived(string msgType, object msg)
      {            
            string clOrdID       = "";
            string orgOrdID  = "";            
            string strMsg       = "";
            string execQty = "";
            string avgPX = "";

            try
            {
                  
                  AptNetradeLib.AptFIXMessage fixMsg = msg as AptNetradeLib.AptFIXMessage;
            
                  string mgType = fixMsg.MessageType;
                  

                  if (fixMsg.MessageType == "8") // Execution Report
                  {
            
                        System.Collections.Hashtable table = new System.Collections.Hashtable();

                        string PriceLimit      = fixMsg.GetField("44", 0);
                        string sendingTime  = fixMsg.GetField("52", 0);
                        string side                  = fixMsg.GetField("54", 0);

                        if (fixMsg.IndexOfField("14", 0) != -1) execQty = fixMsg.GetField("14", 0);

                        if (fixMsg.IndexOfField("58", 0) != -1)      strMsg = fixMsg.GetField("58", 0);
                                                
                        if (strMsg.Length > 100) strMsg = strMsg.Substring(0,99);
 
                        string symbol             = fixMsg.GetField("55", 0);
                        string execType             = fixMsg.GetField("150", 0);
                        
                                    //Get the time as MM/dd/yyyy 15:57:16
                        string sYear      = sendingTime.Substring(0, 4);
                        string sMonth      = sendingTime.Substring(4, 2);
                        string sDay            = sendingTime.Substring(6, 2);
                        string sTime      = sendingTime.Substring(9);

                        sendingTime            = sMonth + "/" + sDay + "/" + sYear + " " + sTime;                  
                        sendingTime            = sendingTime;

                        switch (execType.ToLower())
                        {
                              case "1":
                                    OMSRec.order_part_comp_filled(clOrdID,lastShares,lastPX,avgPX,side);  
                                    break;

                  


                        }
                                          
                        if (clOrdID =="")      clOrdID            = orgOrdID;                        
                        if (clOrdID =="")      clOrdID            = "0";                        
                        if (ExecID=="")            ExecID            = "0";
                        if (side=="")            side            = "0";
                        if (ordStatus=="")  ordStatus      = "0";
                        if (execType=="")      execType      = "0";
                        if (lastShares=="") lastShares      = "0";
                        if (lastPX=="")            lastPX            = "0";

                        string strSql = "insert into oms_external_signals(" +
                                                "external_signal_id," +
                                                "ioi_id," +
                                                "ioi_type,"+
                                                "ioi_status,"+
                                                "signal_type," +
                                                "order_id," +                              
                                                "security_id," +
                                                "signal_date," +
                                                "signal_qty," +
                                                "signal_price" +
                                                ")" +
                                                " values(" +
                                                "'" + ExecID + "'," +
                                                "'0'," +
                                                ""+ side +"," +
                                                ""+ ordStatus +"," +
                                                "" + execType + "," +
                                                "" + clOrdID + "," +                        
                                                "'DSM." + symbol + "'," +
                                                "'" + sendingTime + "'," +
                                                "" + lastShares + "," +
                                                "" + lastPX + "" +
                                                ")";

                        OMSCnn.Open();  
                        OMSCmd.CommandText = strSql;
                        OMSCmd.ExecuteNonQuery();  
                        OMSCnn.Close();
                                                
                        // Must do our calculation in the database here not on the triggers                  
                        frm.Invoke(new AddMessage(frm.AddIncomingMessage), new object[] { msgType, (fixMsg.SequenceNumber), (table) });

                  }

Any suggestions ??


      }


Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros