Pashec
Pashec

Reputation: 23289

Switch NHibernate connection on the fly

We have WebServer (based on Orchard CMS) and two SQL Server databases: master (read-write operations) and slave (only read operations). Databases are synchronized by replication. WebServer uses NHibernate as an ORM.

We want to separate load between these two databases by the following schema. During request processing we should have:

  1. USE slave
  2. SELECT FROM T1
  3. SELECT FROM T2
  4. USE master
  5. BEGIN TRANSACTION
  6. UPDATE T1
  7. UPDATE T2
  8. COMMIT TRANSACTION

So the idea is to switch to the master database before first change statement (UPDATE, DELETE, INSERT). Important note that existing infrastructure uses .Net TransactionScope class to organize transaction.

So how to achieve this with NHibernate?

Upvotes: 1

Views: 293

Answers (1)

Firo
Firo

Reputation: 30813

some starting points:

  • session.Disconnect(); session.Reconnect(connection) to switch connection.
  • maybe use NHibernate.shards

Upvotes: 2

Related Questions