<p>Does a new <code>SessionFactor</code>y and <code>Session</code> object have to be created for each database? I have a data store for my application data, and a separate data store for my employee security, which is used to validate users. Do I have to create a new SessionFactory ans Session object for calls to the 2 different databases?</p>

[{'answer_id': 56749, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 2, 'selected': True, 'text': '<p>ok so this doesn\'t answer your question directly but it might offer an insight as to why you should create multiple session objects for each datastore.</p>\n\n<p><a href="http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx" rel="nofollow noreferrer">This article</a> explains how you can implement a thread safe lazy singleton for each type of Session you need so that you only have one session per datastore but it\'s shared across the entire application. So at most you\'re only ever going to have 2 session objects.</p>\n\n<p>To directly answer your question however, you will need 1 session object per database.</p>\n'}, {'answer_id': 45129618, 'author': 'Frédéric', 'author_id': 1178314, 'author_profile': 'https://Stackoverflow.com/users/1178314', 'pm_score': 0, 'selected': False, 'text': '<h1>General case</h1>\n\n<p>The general case answer is no, you need at least different sessions for the general case.</p>\n\n<p>You may use a single session factory by using the <code>OpenSession</code> overload taking an opened connection as argument, allowing you to switch database for the session requiring it.</p>\n\n<p>This has some drawbacks, like lack of connection auto-releasing after transactions, disabling of second level cache, ... Better have two session factories in my opinion, rather than supplying your own connection on session opening.</p>\n\n<h1>Database specific cases</h1>\n\n<p>Depending on the database server you use, you may be able to use a single connection string for accessing both with NHibernate. If you can use a single connection string, then you can use a single session factory and use the same session for accessing your entities split between two databases.</p>\n\n<h2>Simplest case</h2>\n\n<p>Using SQL Server, you may have your two databases on the same SQL Server. In such case, you can use a single connection string and adjust the <code>catalog</code> attribute on your <code>&lt;class&gt;</code> mappings for telling in which database the table is to be found. (<code>schema</code> can be used too, by appending a dot. It is available in NHibernate since longer, so with an old version you may only have <code>schema</code>.)</p>\n\n<p>Of course, the connection credentials must be valid for accessing both databases.</p>\n\n<h2>Other cases</h2>\n\n<p>Still using SQL Server, if the second database is on another server, you may use a linked server. You would adjust again the <code>catalog</code> attribute on classes requiring it for specifying the appropriate <code>linkedServerName.DbName</code>.</p>\n\n<p>Maybe other databases could have similar solutions.</p>\n'}]
['https://Stackoverflow.com/questions/56589', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1284/']