Search code examples
nhibernatespring.net

Multiple DB Providers - No DB Provider Available


I've got two DB providers configured in my spring config like this (spread across two different files)

<db:provider id="FirstDbProvider"
                   provider="MySql"                   connectionString="Database=xxxxxxx;Host=xxxxxxx;Username=xxxxxxx;Password=xxxxxxxxx"/>

  <db:provider id="SecondDbProvider"
                   provider="MySql"                   connectionString="Database=xxxxxxx;Host=xxxxxxx;Username=xxxxxxx;Password=xxxxxxxxx"/>

But when I try and run my app, it fails when creating the sessionFactory that makes use of SecondDBProvider. I get this

SecondSessionFactory' defined in 'file [Second.Dao.xml] line 21' : Initialization of object failed : There was no DB provider available, unable to create connection

Should I be OK to use two DB Providers in this way? Any ideas what might be causing this error. I can connect to the database from the command line using the credentials etc specified in my DB Provider.

It works perfectly if I go back to just having one database. I can add the second db:provider to my spring.dao.xml file and it all still works (i.e. I can retrieve an entity from the first database). It's only when I add a second sessionFactory that it stops working and I can't retrieve anything, not even from the first database.

So, I guess something somewhere is being shared perhaps and causing a conflict

Update

Here's the config (I renamed some things for obvious reasons)

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:db="http://www.springframework.net/database">

  <!-- Referenced by main application context configuration file -->
  <description>
    Config for Database 1
  </description>

  <!-- Property placeholder configurer for database settings -->
  <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    <property name="ConfigSections" value="databaseSettings"/>
  </object>

  <!-- Database Configuration -->
  <db:provider id="FirstDbProvider"
                   provider="MySql"
                   connectionString="Database=xxxxx;Host=xxxxxxx;Username=xxxxxx;Password=xxxxxx"/>

  <!-- NHibernate Configuration -->
  <object id="FirstSessionFactory" type="Utils.Hibernate.CustomLocalSessionFactoryObject, Utils">
    <property name="DbProvider" ref="FirstDbProvider"/>
    <property name="MappingAssemblies">
      <list>
        <value>FirstDao.Dao</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="dialect" value="NHibernate.Dialect.MySQLDialect"/>
        <entry key="connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
        <entry key="use_proxy_validator" value="false" />
        <entry key="show_sql" value="true"/>
        <entry key="format_sql" value="false"/>
        <entry key="generate_statistics" value="false"/>
        <entry key="adonet.batch_size" value="20"/>
        <entry key="hbm2ddl.auto" value="update"/>
        <entry key="prepare_sql" value="true"/>
      </dictionary>
    </property>
    <property name="ExposeTransactionAwareSessionFactory" value="true" />
  </object>


  <!-- Transaction Management Strategy - local database transactions -->
  <object id="transactionManager"
        type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">

    <property name="DbProvider" ref="FirstDbProvider"/>
    <property name="SessionFactory" ref="FirstSessionFactory"/>

  </object>

  <!-- Exception translation object post processor -->
  <object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>

  <!-- Data Access Objects -->
  <object id="firstDao" type="FirstDao.Dao.MyDao, HRDataCheck.Dao">
    <property name="SessionFactory" ref="FirstSessionFactory"/>
  </object>

</objects>

Here's my second file (for the other DBProvider)

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:db="http://www.springframework.net/database">


  <!-- Database Configuration -->
  <db:provider id="SecondDbProvider"
                   provider="MySql"
                   connectionString="Database=xxxxxx;Host=xxxxxx;Username=xxxxx;Password=xxxxxxxx"/>

  <!-- NHibernate Configuration -->
  <object id="SecondSessionFactory" type="Utils.Hibernate.CustomLocalSessionFactoryObject, Utils">
    <property name="DbProvider" ref="SecondDbProvider"/>
    <property name="MappingAssemblies">
      <list>
        <value>SecondDao.Dao</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="dialect" value="NHibernate.Dialect.MySQLDialect"/>
        <entry key="connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
        <entry key="use_proxy_validator" value="false" />
        <entry key="show_sql" value="true"/>
        <entry key="format_sql" value="false"/>
        <entry key="generate_statistics" value="false"/>
        <entry key="adonet.batch_size" value="20"/>
        <entry key="prepare_sql" value="true"/>
      </dictionary>
    </property>
    <property name="ExposeTransactionAwareSessionFactory" value="true" />
  </object>


  <!-- Data Access Objects -->
  <object id="secondDao" type="Blah.SecondDao, SecondDaoAssembly">
    <property name="SessionFactory" ref="SecondSessionFactory"/>
  </object>

</objects>

Solution

  • Here is your problem: You have no transactions management setup for your SecondSessionFactory.

    <object id="transactionManager"
             type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
         <property name="DbProvider" ref="FirstDbProvider"/>
         <property name="SessionFactory" ref="FirstSessionFactory"/>
    
       </object>
    

    try <!--Transaction Management Strategy - local database transactions--> <object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data"> <property name="TransactionSynchronization" value="Always"/> </object>