Nhibernate错误:BuidSessionFactory()死锁/丢弃连接

时间:2010-12-28 13:56:03

标签: nhibernate sessionfactory

下午好,

我已经被困在这个问题上了好几天了,我觉得我对Nhibernate / SQL Server的理解还不够,所以我可能会想出来。

问题在于涉及拨打电话的任何单元测试:

Nhibernate.Cfg.Configuaration.BuildSessionFactory();

刚陷入运行状态,我被迫按“停止”以让NUnit做出回应。此时会出现错误对话框,其中包含以下信息:

“无法建立连接,因为目标计算机主动拒绝它”

使用“例外文字”;

    System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:51293

Server stack trace: 
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint ipEndPoint)
   at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket()
   at System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortAndSid, Boolean openNew)
   at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream)
   at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITransportHeaders& responseHeaders, Stream& responseStream)
   at System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at NUnit.Core.TestRunner.get_Running()
   at NUnit.Core.ProxyTestRunner.get_Running()
   at NUnit.Util.TestLoader.get_Running()
   at NUnit.Gui.NUnitForm.get_IsTestRunning()
   at NUnit.Gui.NUnitForm.CancelRun()
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我的NHibernate配置如此;

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=MachineName\SQLEXPRESS;database=TestingDB;Integrated Security=true;</property>
    <property name ="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, Nhibernate.ByteCode.LinFu</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

并使用TestFixtureSetup调用以下内容:

_configuration.Configure();
_configuration.AddAssembly(typeof(MyClass).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();

测试只是挂在最后一行。为了尝试理解出现了什么问题,我启用了log4net,它在运行测试时提供了以下输出。

[TestRunnerThread] INFO  NHibernate.Cfg.Environment - NHibernate 2.1.2.4000 (2.1.2.4000)
[TestRunnerThread] INFO  NHibernate.Cfg.Environment - hibernate-configuration section not found in application configuration file
[TestRunnerThread] INFO  NHibernate.Cfg.Environment - Bytecode provider name : lcg
[TestRunnerThread] INFO  NHibernate.Cfg.Environment - Using reflection optimizer
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - connection.provider=NHibernate.Connection.DriverConnectionProvider
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - dialect=NHibernate.Dialect.MsSql2008Dialect
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - connection.driver_class=NHibernate.Driver.SqlClientDriver
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - connection.connection_string=Server=MachineName\SQLEXPRESS;database=MySolution_Testing;Integrated Security=true;
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - proxyfactory.factory_class=NHibernate.ByteCode.LinFu.ProxyFactoryFactory, Nhibernate.ByteCode.LinFu
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - show_sql=true
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - properties: System.Collections.Generic.Dictionary`2[System.String,System.String]
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - Mapping resource: MySolution.Models.Mappings.MenuItem.hbm.xml
[TestRunnerThread] INFO  NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2008Dialect
[TestRunnerThread] INFO  NHibernate.Cfg.XmlHbmBinding.Binder - Mapping class: MySolution.Models.MenuItem -> MenuItems
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: ID -> MenuItemID, type: Int32
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: Name -> MenuItemName, type: String
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: ControllerName -> MenuItemController, type: String
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: ActionName -> MenuItemAction, type: String
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - Mapping resource: MySolution.Models.Mappings.Perspective.hbm.xml
[TestRunnerThread] INFO  NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2008Dialect
[TestRunnerThread] INFO  NHibernate.Cfg.XmlHbmBinding.Binder - Mapping class: MySolution.Models.UserPerspective -> Perspectives
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: ID -> PerspectiveID, type: Int32
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: Name -> PerspectiveName, type: String
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: Description -> PerspectiveDescription, type: String
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: Password -> PerspectivePassword, type: String
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - Mapping resource: MySolution.Models.Mappings.ViewGroup.hbm.xml
[TestRunnerThread] INFO  NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2008Dialect
[TestRunnerThread] INFO  NHibernate.Cfg.XmlHbmBinding.Binder - Mapping class: MySolution.Models.ViewGroup -> ViewGroups
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: ID -> ViewGroupID, type: Int32
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: Perspective -> Link_PerspectiveID, type: MySolution.Models.UserPerspective
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped property: MenuItems, type: Iesi.Collections.Generic.ISet`1[[MySolution.Models.MenuItem, MySolution.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]](MySolution.Models.ViewGroup.MenuItems)
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - checking mappings queue
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - processing one-to-many association mappings
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Second pass for collection: MySolution.Models.ViewGroup.MenuItems
[TestRunnerThread] INFO  NHibernate.Cfg.XmlHbmBinding.Binder - mapping collection: MySolution.Models.ViewGroup.MenuItems -> MenuItems
[TestRunnerThread] DEBUG NHibernate.Cfg.XmlHbmBinding.Binder - Mapped collection key: MenuItemID, one-to-many: MySolution.Models.MenuItem
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - processing one-to-one association property references
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - processing foreign key constraints
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - resolving reference to class: MySolution.Models.ViewGroup
[TestRunnerThread] DEBUG NHibernate.Cfg.Configuration - resolving reference to class: MySolution.Models.UserPerspective
[TestRunnerThread] INFO  NHibernate.Cfg.Configuration - processing filters (second pass)
[TestRunnerThread] INFO  NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2008Dialect
[TestRunnerThread] INFO  NHibernate.Exceptions.SQLExceptionConverterFactory - Using dialect defined converter
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Generate SQL with comments: disabled
[TestRunnerThread] INFO  NHibernate.Connection.ConnectionProviderFactory - Initializing connection provider: NHibernate.Connection.DriverConnectionProvider
[TestRunnerThread] INFO  NHibernate.Connection.ConnectionProvider - Configuring ConnectionProvider
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Transaction factory: NHibernate.Transaction.AdoNetWithDistrubtedTransactionFactory
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Optimize cache for minimal puts: False
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Connection release mode: auto
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Default batch fetch size: 1
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - echoing all SQL to stdout
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Statistics: disabled
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Query translator: NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Query language substitutions: {}
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - cache provider: NHibernate.Cache.NoCacheProvider, NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4
[TestRunnerThread] DEBUG NHibernate.Cfg.SettingsFactory - Wrap result sets: disabled
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Batcher factory: NHibernate.AdoNet.NonBatchingBatcherFactory, NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Default entity-mode: Poco
[TestRunnerThread] INFO  NHibernate.Cfg.SettingsFactory - Named query checking : enabled
[TestRunnerThread] INFO  NHibernate.Impl.SessionFactoryImpl - building session factory
[TestRunnerThread] DEBUG NHibernate.Impl.SessionFactoryImpl - Session factory constructed with filter configurations : {}
[TestRunnerThread] DEBUG NHibernate.Impl.SessionFactoryImpl - instantiating session factory with properties: {'use_reflection_optimizer'='True', 'connection.provider'='NHibernate.Connection.DriverConnectionProvider', 'dialect'='NHibernate.Dialect.MsSql2008Dialect', 'connection.driver_class'='NHibernate.Driver.SqlClientDriver', 'connection.connection_string'='Server=MachineName\SQLEXPRESS;database=MySolution_Testing;Integrated Security=true;', 'proxyfactory.factory_class'='NHibernate.ByteCode.LinFu.ProxyFactoryFactory, Nhibernate.ByteCode.LinFu', 'show_sql'='true'}
[TestRunnerThread] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
[TestRunnerThread] DEBUG NHibernate.Connection.ConnectionProvider - Closing connection

正如你所看到的,NHibernate似乎自己正确地配置它,但是,最后两行似乎暗示当实际要求连接时,出现错误并且全部关闭。这不会报告给NUnit,因此它只是无限期地等待有效连接使用。

我有其他使用Fluent配置的项目似乎正常运行,但这些旧项目使用SQL Compact数据库而不是SQL Express。

是否有人能够就此问题提出解决方案?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

最可能的原因是堆栈溢出导致nunit-agent.exe进程崩溃。这会导致nunit.exe测试运行器失去与代理进程的连接。有关详细信息,请参阅NUnit crashing with NHibernate

我知道这已经快一年了,但希望你或其他人可能觉得这个有用。