Running Multi-threads in IBatis ORM
- 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
- Next is to make the necessary code change in our class where we are going to hit the DB
- ISqlMapper sqlMapper = Mapper.Instance();
- sqlMapper.SessionStore = new HybridWebThreadSessionStore(sqlMapper.Id);
- mapper.QueryForList<EventJob>(transaction, trasctioncollection);
- 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.
- Finally, we need to do a Parallel.Forwach loop on the inside the method where we are calling above mentioned code snippet.
- System.Threading.Tasks.Parallel.ForEach(transactioncollection, transactionparameters=>
- //your logic goes here
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.