Why does session.Clear () need to reflect changes in db in this example?

I have the following code:

public class A
{
    private ISessionFactory _sf;
    A(ISessionFactory sf)
    {
        _sf = sf;
    }

    public void SomeFunc()
    {
        using (var session = _sf.OpenSession())
        using (var transaction = session.BeginTransaction())
        {
            // query for a object
            // change its properties
            // save the object

            transaction.commit();
        }
    }
}

Used as follows in unit test

_session.CreateCriteria ... // some setting up values for this test

var objectA = new A(_sessionFactory);
objectA.SomeFunc();

// _session.Clear();

var someVal = _session.CreateCriteria ... // retrieve value from db to 
                                   //check if it was set to the 
                                   //proper value
                                   //it uses a restriction on a property
                                   //and a uniqueresult to get the object.
                                   //it doesnt use get or load.

Assert.That(someVal, Is.EqualTo(someOtherValue)); // this is false as long 
                                   //as the _session.Clear() is commented. 
                                   //If uncommented, the test passes

I am testing a sqlite database . In my tests, I make some changes to db to properly configure it. Then I call SomeFunc (). He makes the necessary changes. As soon as I return to my test, the session, however, will not receive the updated values. It still returns the value as it was before the SomeFunc () function call. I have to execute _session.Clear () so that the changes are reflected in my assertion in the test.

Why is this needed?

Edit: cache.use_second_level_cache and cache.use_query_cache are set to false

Edit2 . Read the following instructions in the NH Documentation .

ISession SQL, ADO.NET . , ,

* from some invocations of Find() or Enumerable()
* from NHibernate.ITransaction.Commit()
* from ISession.Flush()

10.1 :

, (). .

, ? , . UniqueResult() List() db , ?

, , , ?

+3
3

, . NHibernate . .

0

( 1) .

A ISessionFactory .

ISession, SomeFunc, , _session , 1.

0

. A.SomeFunc, - unit test. ( ). . , . .

When you call _session.Clear (), you make the session "forget" everything by clearing the session cache. When you re-query, you read the latest data from the database, including changes from another session.

0
source

Source: https://habr.com/ru/post/1750700/


All Articles