NHibernate和数据库连接故障转移?

编程入门 行业动态 更新时间:2024-10-19 04:23:58
本文介绍了NHibernate和数据库连接故障转移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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和数据库连接故障转移?

本文发布于:2023-10-23 05:41:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库连接   故障   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!