Nhibernate and SetCacheable with second level cache

1.5k views Asked by At

I am using Rhino.Security repository to manage my users/roles.
The process of creation, deletion and association works fine but I am facing a problem when I query using one method: GetAssociatedUsersGroupFor.
The first time I call the method I don't get any groups for my user cause I haven't created any association yet. At this point I associate a user with a couple of groups.
I check in the DB and I can see the association.
Now I call again GetAssociatedUsersGroupFor but I can't get any groups.
With the profiler I've seen that the database isn't involved this time. I've checked the code and I've found that the function uses a Nhibernate SetCacheable:

    public virtual UsersGroup[] GetAssociatedUsersGroupFor(IUser user)
    {
        ICollection<UsersGroup> usersGroups =
            SecurityCriterions.AllGroups(user)
                .GetExecutableCriteria(session)
                .AddOrder(Order.Asc("Name"))
            .SetCacheable(true)
            .List<UsersGroup>();
        return usersGroups.ToArray();
    }

Since I don't want to change Rhino.Security code, I would like to know if I can override or change this behaviour in any way.

UPDATE:

I use this instruction to get associated groups:

AuthorizationRepository.GetAssociatedUsersGroupFor(User);

I associate with this code:

AuthorizationRepository.AssociateUserWith(User, grpName);

and detach:

AuthorizationRepository.DetachUserFromGroup(User, groupToRemove.Name);

UPDATE

I've found out that I had left the second level cache in my config:

<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>

I thought it was a good thing to have a II level cache, to improve the performance but, it seems I was totally wrong.
Is there anyone who can try to help me to understand what's happening here?

1

There are 1 answers

0
LeftyX On BEST ANSWER

There's a problem with rhino.security.
I've managed to solve the problem, even if I don't like it.
I've called SessionFactory.EvictQueries() when I change an association and everything works fine.