-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Problem creating connection to MySQL
PostPosted: Sun Oct 29, 2006 7:18 pm 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
This post is similar to other connection problems with MySQL in this forum, but none of the solutions to these problems work for me.

The config.BuildSessionFactory() method is returning a factory with the ConnectionProvider set as UserSuppliedConnectionProvider, but this should be DriverConnectionProvider (the config.Properties shows DriverConnectionProvider correctly specified).

Obviously this is not correct, and when trying to create a session, the session.Connection throws an exception of type 'NHibernate.ADOException'

The inner exceptions boil down to "The user must provide an ADO.NET connection - NHibernate is not creating it."

I have been led to believe that the UserSuppliedConnectionProvider is a fallback if the DriverConnectionProvider fails - the question is, why is it failing? MySQL.Data.dll is referenced in the project, and is present in the output directory, as is ICSharpCode.SharpZipLib.

Anyone have any ideas?

Hibernate version:
NHibernate ver 1.2

Mapping documents:

hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="connection.provider_class">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    <property name="connection.connection_string">Server=localhost;Database=theBombShack;User ID=bomber;Password=trhp,btrzE3Mx4bh</property>
  </session-factory>
</hibernate-configuration>


Example mapping file:
Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   assembly="TheBombShack.SpecialOps"
                   namespace="TheBombShack.SpecialOps"
                   default-lazy="false">
  <class name="clsAircraftModel" table="AircraftModel">
    <id name="AircraftModelID" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Designation" length="100" />
    <property name="Manufacturer" length="100"/>
    <property name="MaximumSpeed"/>
    <bag name="Parts" table="modelparts" inverse="true">
      <key column="AircraftModelID"/>
      <many-to-many class="clsPart" column="PartID" />
    </bag>
  </class>
</hibernate-mapping>



Full stack trace of any exception that occurs:
This happens when trying to read from the database, but I am aware that it is not a direct symptom of this error.
Code:
   at NHibernate.Impl.SessionFactoryImpl.OpenConnection()
   at NHibernate.Impl.SessionImpl.Connect()
   at NHibernate.Impl.SessionImpl.get_Connection()
   at NHibernate.Impl.BatcherImpl.Prepare(IDbCommand cmd)
   at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
   at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, RowSelection selection, ISessionImplementor session)
   at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes)
   at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
   at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria, IList results)
   at NHibernate.Impl.SessionImpl.Find[T](CriteriaImpl criteria)
   at NHibernate.Impl.CriteriaImpl.List[T]()
   at TheBombShack.Remote.clsNHibernateManager.getAll[T]() in D:\Documents\NMIT Stuff\BIT3\SDV300\Assignment\TheBombShack\TheBombShack.Remote\clsNHibernateManager.cs:line 107

Name and version of the database you are using:
MySQL ver 5.0


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 9:44 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Why not use SQL Server express edition?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 10:24 pm 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
grennis wrote:
Why not use SQL Server express edition?


because SQL Express is not Open Source, and includes all sorts of limitations (No of processors, database size, ram, OS) that I don't want or need.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 3:04 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
How are you creating the session factory? Since you are using hibernate.cfg.xml, you have to call some overload of Configuration.Configure() for it to be picked up.


Top
 Profile  
 
 Post subject: -
PostPosted: Mon Oct 30, 2006 7:51 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
I think you should try to include a reference to the mysql ado driver in your project...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 3:39 pm 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
sergey: this is my initialisation code
Code:
public clsNHibernateManager()
        {
            config = new Configuration();
            config.Configure(); //Read hibernate.cfg.xml
            config.AddAssembly("TheBombShack.Remote");

            ISessionFactory lcFactory = config.BuildSessionFactory();

            session = lcFactory.OpenSession();
            session.Disconnect();
        }


I've stepped through this with a debugger, and everything runs fine until config.BuildSessionFactory(). No exceptions are thrown back into my program, but the factory doesn't have the right drivers ready for the session, which causes the session to throw an exception any time it tries to do anything.

You may be wondering about the session.Disconnect() call there... its because this is a Windows Forms application, not a web one. I keep the session open the entire time, and just reconnect and disconnect as needed (this part is irrelevant however, I just want the driver to be picked up!) The program used to work fine, this part of the application used to be a .dll. I have changed it so that this dll is now a Windows Service exe and is accessed through remoting. I have tested the other aspects involved with the Windows Service and I know that it is working, just NHibernate isn't yet. I'm thinking it's something to do with the way files are referenced, but I have tried many different things and am getting a little lost in it I'm afraid!

Has anyone had any experience with NHibernate and Windows Services? I wouldn't have thought it would be any different from a windows forms exe (it does run as an exe, just without a window)... but maybe I am mistaken.

markov: both the mysql driver and ICSharpCode.SharpZipLib are referenced in the project and the files are present in the output directory, see my first post.

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 2:05 am 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
Well I seem to have gotten it to work, but I'm not happy with how I managed it!

It appears to have been something to do with my hibernate.cfg.xml file:
Before:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="connection.provider_class">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    <property name="connection.connection_string">Server=localhost;Database=theBombShack;User ID=bomber;Password=trhp,btrzE3Mx4bh</property>
  </session-factory>
</hibernate-configuration>

After:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    <property name="connection.connection_string">Server=localhost;Database=theBombShack;User ID=bomber;Password=trhp,btrzE3Mx4bh</property>
  </session-factory>
</hibernate-configuration>


Spot the difference? Took me a while!
connection.provider_class should have been connection.provider
You wouldn't believe how much head banging I went through on this one!
Would it be possible in future versions of NHibernate to have the configuration complain about this sort of thing, rather than silently fall back to user defined drivers?

Anyway, thanks to all who tried to help..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 2:33 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Enter a JIRA issue and we'll see ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 9:15 pm 
Newbie

Joined: Tue Jul 15, 2008 9:11 pm
Posts: 1
Location: Australia
i had same problem but slightly different solution.

with this config, it didnt work:

Code:
Configuration cfg = new Configuration();
cfg.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty("connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
cfg.SetProperty("connection.connection_string", m_connectionString);
cfg.SetProperty("dialect", "NHibernate.Dialect.MySQLDialect");
cfg.SetProperty("hibernate.dialect", "NHibernate.Dialect.MySQLDialect");


then i added these and it worked:

Code:
cfg.SetProperty("hibernate.connection.connection_string", m_connectionString);
cfg.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
cfg.SetProperty("hibernate.use_reflection_optimizer", "true");
cfg.SetProperty("hibernate.connection.release_mode", "on_close");
cfg.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty("hibernate.show_sql", "true");


dan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.