0

I'm trying to connect with nHibernate and Unhaddins in different machines , each has a database in Oracle. They have different tables with different database.

I'll need to read a table, let's call it as C_SM_SEND.

Inside this Table, I have a column called Body, that will contain a XML.

Another process will read this table, get this XML and will insert his values to another Table, let's name it as PC_TO_SM_PRIM_DATA. Let's not worry about columns, or the names of Table.

What's matter now is: I'll have to read one table in one DB in one Machine => "MACHINE1/ORCL", reading a table named "C_SM_SEND", and insert values in another table of another DB of another Machine => "MACHINE2/SERV", writing a table named PC_TO_SM_PRIM_DATA.

Also, remember: The mapping of each table is different!

I've read some articles than explain how NHibernate connects to different machines, using uNHAddIns, none of what I've read worked.

My application is being made to standards MVVM, IoC, and also use the repository.

I've read also this article of Fabio Maulo, but no success:

Configure Session Factory Providers

Any help will be welcome.

Best Regards,

Gustavo.

Gustavo Gonçalves
  • 528
  • 1
  • 12
  • 33

1 Answers1

0

What you need is one SessionFactory per database. So if you have 2 databases that you're using, that's 2 SessionFactories - one for each database. All the abstractions in uNHAddins really just boil down 2 creating and managing these SessionFactories.

Let's say you're using 2 databases - Database1 and Database2. Database1 has that one table - C_SM_SEND. Database2 has the other table - PC_TO_SM_PRIM_DATA. A really simple way to structure your code is to have 2 classes that build SessionFactories:

public class Database1
{
    private ISessionFactory _sessionFactory;

    public Database1SessionFactory()
    {
        //Build your session factory for Database1 here
        //with the entity C_SM_SEND, connection string to Database1, etc, etc.
    }

    public ISession OpenSession()
    {
        return _sessionFactory.OpenSession();
    }
}

public class Database2
{
    private ISessionFactory _sessionFactory;

    public Database1SessionFactory()
    {
        //Build your session factory for Database2 here
        //with the entity PC_TO_SM_PRIM_DATA, connection string to Database2, etc, etc.
    }

    public ISession OpenSession()
    {
        return _sessionFactory.OpenSession();
    }
}

You can map Database1 and Database2 using your IoC. That leaves just opening the right session and reading/writing your entities.

Transactional operations can be performed across both databases by:

using (var tx = new TransactionScope())
{
    //session1.Save(obj1);
    //session2.Save(obj2);
    tx.Complete();
}
Thilak Nathen
  • 1,333
  • 1
  • 8
  • 13
  • I think it's good to have only one session (maybe I'm confusing with instances) because if there's an error while writing to one table of 'Database2', it can rollback to 'Database2' and 'Database1'. This will allow a rollback of 2 connections? – Gustavo Gonçalves Dec 28 '12 at 19:11
  • Rolling back transactions spanning multiple databases involves using distributed transactions. Updated answer with code using TransactionScope and 2 sessions. – Thilak Nathen Jan 02 '13 at 23:54