NHibernate MySqlException

时间:2018-06-11 09:54:33

标签: c# mysql nhibernate database-connection

我正在尝试将NHibernate用于我的MySQL,但我仍然在我的localhost数据库上遇到连接问题。

产生的异常:

MySql.Data.MySqlClient.MySqlException
  HResult=0x80004005
  Message=Unable to connect to any of the specified MySQL hosts.
  Source=MySql.Data
  StackTrace:
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at NHibernate.Connection.DriverConnectionProvider.GetConnection()
   at NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
   at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
   at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactoryImplementor sessionFactory)
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at TestE.Model.NHibernateHelper.get_Session() in C:\Users\hajek\source\repos\TestE\TestE\NHibernateHelper.cs:line 24
   at TestE.Dao.DaoBase1..ctor() in C:\Users\hajek\source\repos\TestE\TestE\Dao\DaoBase.cs:line 20
   at TestE.Dao.ItemDao..ctor() in C:\Users\hajek\source\repos\TestE\TestE\Dao\ItemDao.cs:line 13
   at TestE.Program.Main(String[] args) in C:\Users\hajek\source\repos\TestE\TestE\Program.cs:line 16
Inner Exception 1:
WaitHandleCannotBeOpenedException: No handle of the given name exists.

代码:

namespace TestE.Model
{
    public class NHibernateHelper
    {
        private static ISessionFactory factory;
        private static MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
        public static ISession Session
        {
            get
            {
                if (factory == null)
                {
                    Configuration cfg = new Configuration();
                    factory = cfg.Configure("hibernate.cfg.xml").BuildSessionFactory();

                }

                return factory.OpenSession();
            }
        }



    }
}

hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- 
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
for your own use before compile tests in VisualStudio.
-->
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="TestE">
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    <property name="connection.connection_string">
      Database=todo_list;Data Source=localhost;User Id=root;Password=root;
      Protocol=memory;Old Guids=True;
    </property>
    <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
  </session-factory>
</hibernate-configuration>

配置位于~/bin/debug,Nhibernate可以看到它,但仍然无法连接到数据库。

-----抱歉我的英语.------

1 个答案:

答案 0 :(得分:0)

默认情况下,Windows上的共享内存is not enabled。这可能就是您的连接失败的原因。

从连接字符串中删除Protocol=memory;以使用常规TCP / IP连接。