Question

Im developing a multithreading application using code smith Nhibernate Template, i read that i must use a session or every thread, problem is i dont know how to get a new session from CodeSmith Generated classes ...

can any body provide me a very very simple example of how to use CodeSmith Nhibernate in a 2 different threads? or at least privide me of code to create new session ?

thanks in advance.

Was it helpful?

Solution

The CodeSmith generated Manager objects are already thread safe, and ensure that each thread will get it's own NHibernate session object. To ensure that threads get opened and closed properly, it is very important that you always dispose of the managers. Here is an example:

IManagerFactory managerFactory = new ManagerFactory();
using (ICategoryManager categoryManager = managerFactory.GetCategoryManager())
{
    Category categoryA = new Category();
    categoryA.Id = "TEST1";
    categoryA.Name = "Test 1";
    categoryA.Descn = "Hello world!";

    categoryManager.Save(categoryA);
    categoryManager.Session.CommitChanges();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top