-->
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.  [ 4 posts ] 
Author Message
 Post subject: Multiple Database
PostPosted: Mon Jun 11, 2007 6:29 am 
Newbie

Joined: Mon Jun 11, 2007 6:08 am
Posts: 3
Hi All,

I am a newbie to NHibernate; I am currently doing a prototype with Spring.Net 1.1 M1 +NHibernate and the requirement is to get more than one customer use the site with different databases. I would like to reuse the HBMs and only want to change the connection string based on the user login.

I am not sure how I can do this.

USECASE:

1. Client logs in to the system with the unique site code
2. Based on the site code connect to the database using the connect string in the master database.

Is there an easy way to pass the connect string to the Hibernate Session to connect to the right database for the user. How can this be done?

I would like to reuse the same hbms as all the database (client specific) will have the same tables. Thats client A connects to Database A having tables CUSTOMER, PRODUCT and client B connects to Database B having tables CUSTOMER, PRODUCT (same table spec) till client N.

I am able to get the prototype working with standard configuration with the SessionFactory to a default settings and one connection string. I would like to change the connection string per request depending on the client.

Can someone guide me on how to achieve this? Is there any working sample that I can refer to? I am stuck with this for the past whole week unable to move any further.

Can someone help! Thanks in advance.


Top
 Profile  
 
 Post subject: Multiple database connections
PostPosted: Fri Jun 22, 2007 3:31 pm 
Newbie

Joined: Fri Jun 22, 2007 3:26 pm
Posts: 1
Here is raw sample of the solution that I have come up with for my project

public static ISession GetCurrentSession(string database)
{
string strConn = @"Server=SQLServer;initial catalog={0};User Id=sa;Password=sa"; // can get this from session or something else

HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;

if (currentSession == null)
{
IDbConnection dbConn = new System.Data.SqlClient.SqlConnection(
string.Format(strConn, database));

dbConn.Open();
currentSession = sessionFactory.OpenSession(dbConn);
context.Items[CurrentSessionKey] = currentSession;
}

return currentSession;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 25, 2007 9:44 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 12:35 pm
Posts: 24
Use a connection provider instead. Far easier and it still allows NHibernate to control the connection which is far better as well.

Code:
using System;
using System.Data;
using NHibernate.Connection;

namespace Fake.Data {
    public class TestConnectionProvider : ConnectionProvider {

        public override IDbConnection GetConnection() {
            string dbName = (HttpContext.Current.Items["DB_NAME"] ?? Settings.CommonDatabaseName);
            IDbConnection connection = base.Driver.CreateConnection();
            connection.ConnectionString = String.Format(base.ConnectionString, dbName);
            connection.Open();
            return connection;
        }
    }
}



Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 9:32 am 
Newbie

Joined: Mon Jul 02, 2007 9:28 am
Posts: 2
Location: Norway
What if you want to save seperate configuration settings for each database, like the SQL dialect.

I want this to work with best possible performance and for a web application. I have the same usecase as sunraider (original post).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.