2

I am having an issue using nHibernate and Rhino.Security. After struggling for hours to get the right config setup, I finally got the code to run without any errors. However, no entries are being saved to the database unless I call session.Flush().

Looking at various examples on the net; I should not have to call flush.

Here’s my config code:

var cfg = new Configuration()
             .SetProperty(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName)
             .SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).AssemblyQualifiedName)
             .SetProperty(Environment.ConnectionString, "………")
             .SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
             .SetProperty(Environment.ReleaseConnections, "on_close")
             .SetProperty(Environment.UseSecondLevelCache, "true")
             .SetProperty(Environment.UseQueryCache, "true")
             .SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName)
             .AddAssembly("GA.CAP.Website")
        ;

        Security.Configure<AspNetUser>(cfg, SecurityTableStructure.Prefix);

        var factory = cfg.BuildSessionFactory();

        var session = factory.OpenSession();


        var authorizationRepository = new AuthorizationRepository(session);
        IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
        var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
        IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);
        var permissionService = new PermissionsService(authorizationRepository, session);
        IoC.Container.RegisterInstance<IPermissionsService>(permissionService);
        var authService = new AuthorizationService(permissionService, authorizationRepository);
        IoC.Container.RegisterInstance<IAuthorizationService>(authService);

Test code:

    authorizationRepository.CreateUsersGroup("GAAdmins");
    var group = authorizationRepository.GetUsersGroupByName("GAAdmins");

The GetUsersGroupByName call returns null. If I add a session.Flush call in between the two calls, it works fine and returns the group.

Based on examples in various blogs, such as this one, I should not have to call flush. In addition, the test cases included with the Rhino.Security code do not do any flushing as shown here:

This is straight out of Rhino.Security's test case fixture:

    // on first deploy
    Operation operation = authorizationRepository.CreateOperation("/Account/View");
    // when creating account
    UsersGroup group = authorizationRepository.CreateUsersGroup("Belongs to " + account.Name);

    // setting permission so only associated users can view
    permissionsBuilderService
        .Allow(operation)
        .For(group)
        .On(account)
        .DefaultLevel()
        .Save();

    // when adding user to account
    authorizationRepository.AssociateUserWith(user, group);

    bool allowed = authorizationService.IsAllowed(user, account, "/Account/View");
    Assert.True(allowed);

Is there some setting I am missing somewhere?

Thanks,

Rick

rboarman
  • 8,248
  • 8
  • 57
  • 87
  • I have re-read your question, and removed my answer as it did not address your specific problem. Sorry about that! – Sam Aug 26 '10 at 04:36
  • i haven't used NHibernate yet, but the behavior sounds right for other ORM frameworks I have used - the GetUsersGroupByName presumably does a sql call and group hasn't been written (unless you do the Flush). Unless the auth repo is keeping its own write cache (not just read cache) that can be used for fulfilling read requests, this seems like it's By Design. Since you mentioned 'examples in various blogs', mind sharing those? I'd like to learn more about NHibernate, of course, and maybe I can help here to boot :) – James Manning Aug 26 '10 at 04:58
  • I added a link and a code example at the bottom of my post. – rboarman Aug 26 '10 at 15:04

1 Answers1

3

That is expected, RS uses the same session as your code, and calling Flush internally may result in unintended consequences. Commit your transaction or call Flush

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41