【问题标题】:NHibernate + MySql Intermittent TimeoutExceptionNHibernate + MySql 间歇性超时异常
【发布时间】:2011-04-02 01:20:44
【问题描述】:

我刚刚将我的 NHibernate 应用程序从 SQL Server 迁移到 MySql。一切似乎都很好,除了我得到间歇性的 TimeoutExceptions。我可以找到很多关于 MySql 超时的信息,甚至是one blog post 关于 Hibernate + MySql + C3PO (不管是什么)的超时信息。但是没有关于如何调整 NHib 来阻止这个问题。你知道吗?

这是堆栈跟踪:

NHibernate.TransactionException: Begin failed with SQL exception ---> System.TimeoutException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   --- End of inner exception stack trace ---
   at MyNetworkStream.HandleOrRethrowException(Exception e)
   at MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadOk(Boolean read)
   at MySql.Data.MySqlClient.NativeDriver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.Driver.SetDatabase(String dbName)
   at MySql.Data.MySqlClient.MySqlConnection.ChangeDatabase(String databaseName)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at NHibernate.Connection.DriverConnectionProvider.GetConnection()
   at NHibernate.AdoNet.ConnectionManager.GetConnection()
   at NHibernate.Impl.SessionImpl.get_Connection()
   at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
   --- End of inner exception stack trace ---
   at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
   at NHibernate.Transaction.AdoTransaction.Begin()
   at NHibernate.AdoNet.ConnectionManager.BeginTransaction()
   at NHibernate.Impl.SessionImpl.BeginTransaction()

【问题讨论】:

  • 现在你可以放一个 Timer 来做一些虚拟 SELECT,以避免超时?
  • Tim 你有没有解决这个问题,因为我越来越想知道这个......

标签: mysql nhibernate timeout


【解决方案1】:

我使用 MYSQL 数据库在 Rackspace 云上运行 NHibernate。我发现我正在超时,因为连接由于睡眠而处于半开放状态。最终我的连接用完了,我的网站超时了。

最后我将Pooling=false; 添加到我的连接字符串中。我不确定这是否是最终解决方案,但它已经解决了我的问题。

您可以在 MySQL 编辑器中使用 show processlist 检查休眠连接...

【讨论】:

  • 谢谢。哎呀!池化=假?您是否担心这样做会影响性能?
  • 很可能是的,它确实取决于用例;但是我认为池代码中有一个错误,将连接标记为sleeping 但不允许唤醒它们,因此最终您会用完连接。就像我说的那样,这不是最终的解决方法,但至少目前对我来说是一种解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多