Running Multi-threads in IBatis ORM 

 

    1. Install IBatis45 from Nuget package : Here I realized that I was using older version of IBatis which was not supporting multhithreading, so I updated IBatis to IBatish45 from package manager console. Afterwards, need to import it in code 

      using IBatisNet.DataMapper;

    2. Next is to make the necessary code change in our class where we are going to hit the DB

      1. ISqlMapper sqlMapper = Mapper.Instance();
      2. sqlMapper.SessionStore = new HybridWebThreadSessionStore(sqlMapper.Id);
      3. mapper.QueryForList<EventJob>(transaction, trasctioncollection);

 

  1. You can notice in above code snippet we have used HybridWebThreadSessionStore and had passed the mapper id to it. This is basically responsible of creating multiple threads. Using traditional old IBatis library will throw exception if we try same.

  2. Finally, we need to do a Parallel.Forwach loop on the inside the method where we are calling above mentioned code snippet.

    1. System.Threading.Tasks.Parallel.ForEach(transactioncollection, transactionparameters=>
    2.             {     
    3. //your logic goes here
    4. });

 

Using above code will run foreach multhi-threaded and hence the issue got resolved. Hope same might help you in your case where you might be using IBatis ORM like me.

 

blog comments powered by Disqus