Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threading Problem Calling DB Query

Threading Problem Calling DB Query

Scheduled Pinned Locked Moved C#
databasehelpsqlitelinqiot
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm posting this here because I believe this to be a threading issue, not a DB issue. FYI, I'm using SQLite with dotConnect and EF. I'm calling a DAL function from two different places on start of my app. Basically there are two lists both being populated from a DAL call. Both places use Background workers to make the same call:

    private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
    {
    var sensors = AppCore.AppDAL.GetSensors();
    e.Result = sensors;
    }

    Then in the DAL I have a call to get the sensors:

    public List GetSensors()
    {
    lock (lockObj)
    {
    var query = getSensorQuery();
    var results = query.OrderBy(x => x.SensorName).ToList();

        // THIS CALLS GetSensorCategoryBELOW
        results.ForEach(x => x.Category = GetSensorCategory(x.SensorTypeID));
    
        return results;
    }
    

    }

    private IQueryable getSensorQuery()
    {
    var results = (from s in dataContext.Sensors
    select new SensorEntity
    {
    ID = s.ID,
    SensorTypeID = s.SensorTypeID,
    SensorName = s.Name,
    Description = s.Description,
    Address = s.SourceAddress.Value,
    LastActivityDT = s.LastUpdate
    });

    return results;
    

    }

    Then for each sensor I get it's Category

    public SensorCategoryEntity GetSensorCategory(long ID)
    {
    var query = getSensorCategoryQuery();
    var results = query.Where(x => x.ID == ID).FirstOrDefault(); //<== FAILS HERE
    return results;
    }

    private IQueryable getSensorCategoryQuery()
    {
    var query = (from sc in dataContext.SensorTypes
    select new SensorCategoryEntity
    {
    ID = sc.ID,
    Name = sc.Name
    });

    return query;
    

    }

    At the point indicated I get "Object cannot be cast from DBNull to other types." If I put in a Thread.Sleep, like below, then it works fine. if I block out the second call, it works fine.

    private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
    {
    Thread.Sleep(500);

    var sensors = Falcon6Core.Falcon6DAL.GetSensors();
    e.Result = sensors;
    

    }

    I don't really understand what's going on here. I'm not inserting data... it's just a simple query.. Why would t

    L M 3 Replies Last reply
    0
    • K Kevin Marois

      I'm posting this here because I believe this to be a threading issue, not a DB issue. FYI, I'm using SQLite with dotConnect and EF. I'm calling a DAL function from two different places on start of my app. Basically there are two lists both being populated from a DAL call. Both places use Background workers to make the same call:

      private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
      {
      var sensors = AppCore.AppDAL.GetSensors();
      e.Result = sensors;
      }

      Then in the DAL I have a call to get the sensors:

      public List GetSensors()
      {
      lock (lockObj)
      {
      var query = getSensorQuery();
      var results = query.OrderBy(x => x.SensorName).ToList();

          // THIS CALLS GetSensorCategoryBELOW
          results.ForEach(x => x.Category = GetSensorCategory(x.SensorTypeID));
      
          return results;
      }
      

      }

      private IQueryable getSensorQuery()
      {
      var results = (from s in dataContext.Sensors
      select new SensorEntity
      {
      ID = s.ID,
      SensorTypeID = s.SensorTypeID,
      SensorName = s.Name,
      Description = s.Description,
      Address = s.SourceAddress.Value,
      LastActivityDT = s.LastUpdate
      });

      return results;
      

      }

      Then for each sensor I get it's Category

      public SensorCategoryEntity GetSensorCategory(long ID)
      {
      var query = getSensorCategoryQuery();
      var results = query.Where(x => x.ID == ID).FirstOrDefault(); //<== FAILS HERE
      return results;
      }

      private IQueryable getSensorCategoryQuery()
      {
      var query = (from sc in dataContext.SensorTypes
      select new SensorCategoryEntity
      {
      ID = sc.ID,
      Name = sc.Name
      });

      return query;
      

      }

      At the point indicated I get "Object cannot be cast from DBNull to other types." If I put in a Thread.Sleep, like below, then it works fine. if I block out the second call, it works fine.

      private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
      {
      Thread.Sleep(500);

      var sensors = Falcon6Core.Falcon6DAL.GetSensors();
      e.Result = sensors;
      

      }

      I don't really understand what's going on here. I'm not inserting data... it's just a simple query.. Why would t

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Does https://www.sqlite.org/threadsafe.html[^] help?

      K 1 Reply Last reply
      0
      • L Lost User

        Does https://www.sqlite.org/threadsafe.html[^] help?

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Not really. Right at the top is says "Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction" and "The default mode is serialized." I haven't changed the threading mode, so it should be default.

        If it's not broken, fix it until it is

        1 Reply Last reply
        0
        • K Kevin Marois

          I'm posting this here because I believe this to be a threading issue, not a DB issue. FYI, I'm using SQLite with dotConnect and EF. I'm calling a DAL function from two different places on start of my app. Basically there are two lists both being populated from a DAL call. Both places use Background workers to make the same call:

          private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
          {
          var sensors = AppCore.AppDAL.GetSensors();
          e.Result = sensors;
          }

          Then in the DAL I have a call to get the sensors:

          public List GetSensors()
          {
          lock (lockObj)
          {
          var query = getSensorQuery();
          var results = query.OrderBy(x => x.SensorName).ToList();

              // THIS CALLS GetSensorCategoryBELOW
              results.ForEach(x => x.Category = GetSensorCategory(x.SensorTypeID));
          
              return results;
          }
          

          }

          private IQueryable getSensorQuery()
          {
          var results = (from s in dataContext.Sensors
          select new SensorEntity
          {
          ID = s.ID,
          SensorTypeID = s.SensorTypeID,
          SensorName = s.Name,
          Description = s.Description,
          Address = s.SourceAddress.Value,
          LastActivityDT = s.LastUpdate
          });

          return results;
          

          }

          Then for each sensor I get it's Category

          public SensorCategoryEntity GetSensorCategory(long ID)
          {
          var query = getSensorCategoryQuery();
          var results = query.Where(x => x.ID == ID).FirstOrDefault(); //<== FAILS HERE
          return results;
          }

          private IQueryable getSensorCategoryQuery()
          {
          var query = (from sc in dataContext.SensorTypes
          select new SensorCategoryEntity
          {
          ID = sc.ID,
          Name = sc.Name
          });

          return query;
          

          }

          At the point indicated I get "Object cannot be cast from DBNull to other types." If I put in a Thread.Sleep, like below, then it works fine. if I block out the second call, it works fine.

          private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
          {
          Thread.Sleep(500);

          var sensors = Falcon6Core.Falcon6DAL.GetSensors();
          e.Result = sensors;
          

          }

          I don't really understand what's going on here. I'm not inserting data... it's just a simple query.. Why would t

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          dbContext is not thread safe. Based on the few code fragments you've posted, it looks like you're sharing dbContext between your threads. Assuming your "lock" is even a solution, I think it is in the wrong place and should have been put in the top of the loadWorker_DoWork() method. (I would have given each thread their own dbContext).

          1 Reply Last reply
          0
          • K Kevin Marois

            I'm posting this here because I believe this to be a threading issue, not a DB issue. FYI, I'm using SQLite with dotConnect and EF. I'm calling a DAL function from two different places on start of my app. Basically there are two lists both being populated from a DAL call. Both places use Background workers to make the same call:

            private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
            {
            var sensors = AppCore.AppDAL.GetSensors();
            e.Result = sensors;
            }

            Then in the DAL I have a call to get the sensors:

            public List GetSensors()
            {
            lock (lockObj)
            {
            var query = getSensorQuery();
            var results = query.OrderBy(x => x.SensorName).ToList();

                // THIS CALLS GetSensorCategoryBELOW
                results.ForEach(x => x.Category = GetSensorCategory(x.SensorTypeID));
            
                return results;
            }
            

            }

            private IQueryable getSensorQuery()
            {
            var results = (from s in dataContext.Sensors
            select new SensorEntity
            {
            ID = s.ID,
            SensorTypeID = s.SensorTypeID,
            SensorName = s.Name,
            Description = s.Description,
            Address = s.SourceAddress.Value,
            LastActivityDT = s.LastUpdate
            });

            return results;
            

            }

            Then for each sensor I get it's Category

            public SensorCategoryEntity GetSensorCategory(long ID)
            {
            var query = getSensorCategoryQuery();
            var results = query.Where(x => x.ID == ID).FirstOrDefault(); //<== FAILS HERE
            return results;
            }

            private IQueryable getSensorCategoryQuery()
            {
            var query = (from sc in dataContext.SensorTypes
            select new SensorCategoryEntity
            {
            ID = sc.ID,
            Name = sc.Name
            });

            return query;
            

            }

            At the point indicated I get "Object cannot be cast from DBNull to other types." If I put in a Thread.Sleep, like below, then it works fine. if I block out the second call, it works fine.

            private void loadWorker_DoWork(object sender, DoWorkEventArgs e)
            {
            Thread.Sleep(500);

            var sensors = Falcon6Core.Falcon6DAL.GetSensors();
            e.Result = sensors;
            

            }

            I don't really understand what's going on here. I'm not inserting data... it's just a simple query.. Why would t

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            I was under the impression you could only have 1 datareader active at any one time, I assume EF spit uses datareader as the underlying connection type. The second connection may be being reject on that basis.

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups