Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate stateless session - what is the data aliasing effect?

Tags:

nhibernate

The NHibernate documentation for the stateless session interface states, among others:

Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache.

I couldn't find an explanation for this. What does 'data aliasing effects' mean?

If you could give examples... that'd be great.

like image 433
Cristian Diaconescu Avatar asked Aug 02 '13 10:08

Cristian Diaconescu


1 Answers

consider the following example

table Orders
id | customer_id | quantity
---------------------------
1  | 1           | 5
2  | 1           | 20


var orders = statelessSession.Query<Oders>().ToList();
orders[0].Customer.HasDiscount = true;
Assert.False(orders[0].Customer == orders[1].Customer);
Assert.False(orders[1].Customer.HasDiscount);

// while

var orders = session.Query<Oders>().ToList();
orders[0].Customer.HasDiscount = true;
Assert.True(orders[1].Customer.HasDiscount);

so using stateless session the customers are not the same instance hence updates are not seen where they should and ReferenceEquals will return false. You have two alias of the same Customer

like image 104
Firo Avatar answered Jan 02 '23 22:01

Firo