문제

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.

도움이 되었습니까?

해결책

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top