Hi,
I'm in the middle of adding second level caching to an existing
application and this is the first time that I really touch this -
quite interesting - topic.
Basically, what I want to do in this first phase is to pre-load all
referential data (i.e. the slowly changing stuff) when the session
factory is created, both the entities and the queries. This should
allow me to mark all references to these entities as lazy in the
mappings of the "dynamic" entities and, moreover, I shouldn't have to
eagerly load them anymore in queries on these "dynamic" queries.
So I hope to win both in performance and simplicity with this
approach.
But now I stumbled on a problem when a dynamic entity (Leaver) with a
reference to a referential entity (Role) is loaded, then the session
is closed and after that the referential entity is touched.
This piece of code should make it more clear:
Leaver leaver;
using (IUnitOfWork unitOfWork = OpenUnitOfWork())
{
LeaverRepository leaverRepository = new
LeaverRepository(unitOfWork);
leaver = leaverRepository.RetrieveLeaver
(leaverFNumber);
}
Assert.IsTrue(NHibernateUtil.IsInitialized
(leaver.Role)); // => FAILS!
Here is what the logging shows:
DefaultLoadEventListener:0 - loading entity: [Role#LHR]
DefaultLoadEventListener:0 - creating new proxy for entity <<==
So it seems that the second level cache is not checked when the Leaver
is hydrated, resulting in a proxy. When the proxy is touched after the
session is closed, the error occurs.
As said, the reference Leaver.Role is lazy and the referential entity
is sitting in the second level cache.
If I load the Role from the second level cache into the session first,
then there is no problem:
Leaver leaver;
using (IUnitOfWork unitOfWork = OpenUnitOfWork())
{
RoleRepository roleRepository = new RoleRepository
(unitOfWork);
roleRepository.RetrieveAll();
LeaverRepository leaverRepository = new
LeaverRepository(unitOfWork);
leaver = leaverRepository.RetrieveLeaver
(leaverFNumber);
}
Assert.IsTrue(NHibernateUtil.IsInitialized
(leaver.Role)); // => WORKS!
Logs:
DefaultLoadEventListener:0 - loading entity: [Role#LHR]
DefaultLoadEventListener:0 - entity found in session cache <<==
Is this by design or did I discover a bug?
Kind regards,
Tolomaüs
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---