Login

Username

Password





Register   Reset password

Get Cuyahoga at SourceForge.net. Fast, secure and Free Open Source software downloads

Forum

Welcome Guest Search | Active Topics | Members

How can connect to a 2nd database using NHibernate ? Options
goolz
Posted: Monday, July 26, 2010 6:54:21 AM
Rank: Advanced Member
Groups: Member

Joined: 6/18/2007
Posts: 39
Points: 117
how can connect to different DBs using NH (DB1 for CUY core modules and DB2 for customized modules) ? does anyone has any suggestion or samples?
goolz
Posted: Monday, July 26, 2010 6:58:42 AM
Rank: Advanced Member
Groups: Member

Joined: 6/18/2007
Posts: 39
Points: 117
goolz
Posted: Wednesday, October 13, 2010 3:25:54 AM
Rank: Advanced Member
Groups: Member

Joined: 6/18/2007
Posts: 39
Points: 117
Build a module that use a different database than CUY.
Scenario:
You have a site using CUY as a CMS, also you have another site that has a different database.
For example first site using a database called ‘cuy_db’ and the database of other site called ‘sample_db’ .
Now you built a module base on CUY framework and put it on ‘cuy_db’ and want to read data from ‘sample_db’:
1: add the properties of 2nd database to Config/properties.config :
Code:

< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
    < properties>
        < !-- Database -->
        
        < !-- SQL Server settings -->
< !—first DB Connection String -->
        < connectionString>Data Source=(local)\SQLEXPRESS;Initial Catalog= cuy_db;Integrated Security=True< /connectionString>
        < nhibernateDriver>NHibernate.Driver.SqlClientDriver< /nhibernateDriver>
        < nhibernateDialect>NHibernate.Dialect.MsSql2000Dialect< /nhibernateDialect>

< !—second DB Connection String -->
        < connectionString2nd>Data Source=(local)\SQLEXPRESS;Initial Catalog= sample_db;Integrated Security=True< /connectionString2nd >
        < nhibernateDriver2nd>NHibernate.Driver.SqlClientDriver< /nhibernateDriver2nd>
        < nhibernateDialect2nd>NHibernate.Dialect.MsSql2000Dialect< /nhibernateDialect2nd>
        
        < !-- Email. -->
        < smtpHost>localhost< /smtpHost> < !-- required-->
        < smtpPort>< /smtpPort>
        < smtpUsername>< /smtpUsername>
        < smtpPassword>< /smtpPassword>
        < emailEncoding>utf-8< /emailEncoding> < !-- iso-8859-1, , us-ascii, etc.  -->
        
        < tempDir>< /tempDir>

    < /properties>
< /configuration>

2: change Config/facilities.config to: (check < assembly>CUY.Modules.Test< /assembly> and < factory id="nhibernate.factory2" alias="seconddb" > )
Code:

< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
    < facilities>
        < facility type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"
            id="nhibernate" isWeb="true" useReflectionOptimizer="false">
            < factory id="nhibernate.factory">
                < settings>
                    < item key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider< /item>
                    < item key="hibernate.connection.driver_class">#{nhibernateDriver}< /item>
                    < item key="hibernate.dialect">#{nhibernateDialect}< /item>
                    < item key="hibernate.connection.connection_string">#{connectionString}< /item>
                    < item key="hibernate.cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache< /item>
                    < item key="hibernate.cache.use_query_cache">true< /item>
                    < item key="relativeExpiration">30< /item>
                < /settings>
                < assemblies>
                    < assembly>Cuyahoga.Core< /assembly>
                < /assemblies>
            < /factory>
< !—for second database-->
            < factory id="nhibernate.factory2" alias="seconddb">
                < settings>
                    < item key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider< /item>
                    < item key="hibernate.connection.driver_class">#{connectionString2nd}< /item>
                    < item key="hibernate.dialect">#{connectionString2nd}< /item>
                    < item key="hibernate.connection.connection_string">#{connectionString2nd}< /item>
                    < item key="hibernate.cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache< /item>
                    < item key="hibernate.cache.use_query_cache">true< /item>
                    < item key="relativeExpiration">30< /item>
                < /settings>
                < assemblies>
< !—map files from CUY.Modules.Test -->

                    < assembly>CUY.Modules.Test< /assembly>
                < /assemblies>
            < /factory>
        < /facility>
        < facility type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement"
            id="autotransaction" />
    < /facilities>
< /configuration>

3- use the 2nd database in CUY.Modules.Test module by : this._sessionManager.OpenSession( alias name )
Code:

public IList GetAll()
        {
            ISession session = this._sessionManager.OpenSession("seconddb");
            ICriteria crit = session.CreateCriteria(typeof(test1));
            return crit.List();
        }


martijnb
Posted: Thursday, October 14, 2010 1:42:39 AM

Rank: Administration
Groups: Administration , Member

Joined: 12/30/2004
Posts: 1,674
Points: 1,824
Location: Wageningen (NL)
Nice writeup. Thanks!
Constructor
Posted: Thursday, October 14, 2010 1:26:55 PM
Rank: Administration
Groups: Administration , Member

Joined: 10/7/2008
Posts: 505
Points: 1,515
I would like to see more of these mini tutorial types of post based around real life use senarios. Things like this also help future development direction and idea generation.
goolz
Posted: Friday, October 15, 2010 11:43:08 AM
Rank: Advanced Member
Groups: Member

Joined: 6/18/2007
Posts: 39
Points: 117
Constructor wrote:
I would like to see more of these mini tutorial types of post based around real life use senarios. Things like this also help future development direction and idea generation.


The wiki(http://wiki.cuyahoga-project.org/) site must be helpful, I think it's the time for turn around the wiki and make it useful.
goolz
Posted: Friday, October 15, 2010 11:45:15 AM
Rank: Advanced Member
Groups: Member

Joined: 6/18/2007
Posts: 39
Points: 117
martijnb wrote:
Nice writeup. Thanks!


You are welcome Martijnb.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

Yet Another Forum.net version 1.9.0 running under Cuyahoga.
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.