TheCloudlessSky / NHibernate.Caches.Redis

An NHibernate caching provider for Redis.
MIT License
59 stars 39 forks source link

The connection multiplexer can only be configured once exception #35

Closed futurify-ydang closed 5 years ago

futurify-ydang commented 5 years ago

I have configure second level cache for NHibernate to use redis cache in my project. But i just face The connection multiplexer can only be configured once exception. Can you show me the reason? Thanks

var connectionMultiplexer = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true");
            connectionMultiplexer.GetServer("localhost", 6379).FlushAllDatabases();
            RedisCacheProvider.SetConnectionMultiplexer(connectionMultiplexer);
            RedisCacheProvider.SetOptions(new RedisCacheProviderOptions()
            {
                Serializer = new NetDataContractCacheSerializer(),
                CacheConfigurations = new[]
                {
                    new RedisCacheConfiguration("NHibernate.Cache.StandardQueryCache")
                    {
                        Expiration = TimeSpan.FromSeconds(9)
                    }
                }
            });

            cfg.DataBaseIntegration(x =>
            {
                x.ConnectionProvider<DriverConnectionProvider>();
                x.ConnectionStringName = GlobalConstant.DefaultConnectionString;
                x.LogSqlInConsole = true;
                x.LogFormattedSql = true;

                switch (ServerHelper.ServerType)
                {
                    case GlobalConstant.PostgresServer:
                        x.Dialect<PostGis20Dialect>();
                        x.Driver<NpgsqlDriver>();
                        //<property name="hbm2ddl.keywords">none</property>
                        break;

                    case GlobalConstant.MySqlServer:
                        x.Dialect<MySQL57SpatialDialect>();
                        x.Driver<MySqlDataDriver>();
                        break;

                    default:
                        x.Dialect<MsSql2012GeometryDialect>();
                        x.Driver<SqlClientDriver>();
                        break;
                }
            });
            cfg.SetProperty("current_session_context_class", "web");
            cfg.SetProperty("cache.provider_class", "NHibernate.Caches.Redis.RedisCacheProvider, NHibernate.Caches.Redis");            //cfg.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache");
            cfg.SetProperty("cache.use_query_cache", "true");
            cfg.SetProperty("cache.use_second_level_cache", "true");
            cfg.AddAssembly(Assembly.GetExecutingAssembly());
TheCloudlessSky commented 5 years ago

Hey! If RedisCacheProvider.SetConnectionMultiplexer is called more than once, it will throw this exception. So, you're either calling it from somewhere else or the code that you pasted is being called more than once.

futurify-ydang commented 5 years ago

thanks for your comments. I have call it twice so that this exception has been thrown