我使用NHibernate连接到传统的RDBMS系统。在高生产负荷的RDBMS服务失败。为了保持可用性,我们有一个故障转移RDBMS服务。有没有一种方法来配置NHibernate的使用故障切换连接字符串时,主连接是跌?
I am using NHibernate to connect to a legacy rdbms system. Under high production load the rdbms service fails. To maintain the availability we have a failover rdbms service. Is there a way to configure NHibernate to use the FailOver Connection String when the primary connection is down?
附加信息:我使用的城堡了NHibernate的。如果城堡提供处理故障的连接,然后也将做到这一点对我来说。
Additional Info: I am using Castle over NHibernate. If Castle provides handling of failover connections then that will also do it for me.
推荐答案您可以建立自己的 NHibernate.Connection.IConnectionProvider 提供故障转移支持。
You can build your own NHibernate.Connection.IConnectionProvider which provides failover support.
这应该是的ConnectionProvider 的子类,覆盖 CloseConnection() 的getConnection() 和配置()。
This should be a subclass of ConnectionProvider which overrides CloseConnection() GetConnection() and Configure().
连接提供商在Hibernate中指定的配置作为物业 connection.provider 。
The connection provider is specified in your hibernate configuration as property connection.provider.
下面是一个未经测试的实现是基于DriverConnectionProvider。
Here is an untested implementation which is based on DriverConnectionProvider.
public class FailoverConnectionProvider : ConnectionProvider { static string FailoverConnectionStringProperty = "connection.failover_connection_string"; string failover_connstring; public override void CloseConnection(IDbConnection conn) { base.CloseConnection(conn); conn.Dispose(); } public override IDbConnection GetConnection() { try { return GetConnection( ConnectionString ); } catch { return GetConnection( failover_connstring ); } } IDbConnection GetConnection( string connstring ) { log.Debug("Obtaining IDbConnection from Driver"); IDbConnection conn = Driver.CreateConnection(); try { conn.ConnectionString = connstring; conn.Open(); } catch (Exception) { conn.Dispose(); throw; } return conn; } public override void Configure(IDictionary<string, string> settings) { base.Configure( settings ); settings.TryGetValue(FailoverConnectionStringProperty, out failover_connstring); if (failover_connstring == null) { throw new HibernateException("Could not find connection string setting (set " + FailoverConnectionStringProperty + " property)"); } } }更多推荐
NHibernate和数据库连接故障转移?
发布评论