nhibernate should i flush session explicitly after "SaveOrUpdate"?
I have written an integrationtest that fails:
[Test]
public void Find_WorkItemWithMatchingDescriptionExistsInRepository_ReturnsWorkItem()
{
// arrange
WorkItemRepository repository = new WorkItemRepository(IsolatingFactory);
const string Description = "A";
WorkItem itemWithMatchingDescription = WorkItem.Create(1, Description);
repository.Commit(itemWithMatchingDescription);
// act
List<WorkItem> searchResult = repository.Find(Description);
// assert
CollectionAssert.Contains(searchResult, itemWithMatchingDescription);
}
When watching the sql output from nhibernate i notice that it creates a select-statement for the repository.Find() operation, but no insert operation before the select. If i put an explicit session.Flush() after the repository.Commit it works as expected, but I find this behaviour odd. Why doesn't nhibernate flush the session automatically when there are pending insert and a query is performed against the session?
I found this in the documentation:
9.6. Flush
From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory. This process, flush, occurs by default at the following points
* from some invocations of Find() or Enumerable()
* from NHibernate.ITransaction.Commit()
* from ISession.Flush()
It looks as the default nhibernate behaviour is to flush before quering by find. My repository uses Ling to nhibernate for the query specification, maybe this is a bug.
Update
If I change my mapping from:
public WorkItemClassMap()
{
Not.LazyLoad();
Id(wi => wi.Id).GeneratedBy.Assigned();
Map(wi => wi.Description).Length(500);
Version(wi => wi.LastChanged).UnsavedValue(new DateTime().ToString());
}
To:
public WorkItemClassMap()
{
Not.LazyLoad();
Id(wi => wi.Id).GeneratedBy.Identity();
Map(wi => wi.Description).Length(500);
}
It works as expected (nhibernate flushes the insert to the database before performing the query). I can't see any good reason for this behavior so I'm assuming its a bug. Looks like a need to do manual flush before querying in my repositories, which is not something I really want to do.
Is WorkItem in your example mapped with a composite ID? I just coded a simple test. My dummy class would auto flush when I had a single identity column. I wrote the same test though with a composite ID and I get the behavior you describe above.
You should use different ISession
instances for the insert and select methods. It's best if these sessions are in a using
block so that they are properly disposed of and inside a database transaction:
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
// perform your insert here
tx.Commit();
}
Assuming that your repository wither creates or takes a session that already exists. When you make the WorkItem, and commit it, it just goes on a list of jobs for that session to do. If your repository does not create a transaction and your commit method does not explicity commit the transaction. Then the update or save will wait until the session closes, so although you can see the object that you created, you will not be able to get the scope identity, for instance, until you close the session or flush it. Certainly searchResult won't know anything about the object you created without a flush or closing transaction.
There are perils with transactions the same as sessions in general but if you are making new objects you should be using explicit transactions. NHProfiler will readily warn you about this.
链接地址: http://www.djcxy.com/p/23232.html上一篇: 在查询之前刷新?