views:

83

answers:

1

I would like to connect NHibernate to a MySQL master-slave replication configuration, so I would like to send writes to the master, and reads to the master and slaves. Is this possible? I am also planning on having a load balancer to balance the reads. (ldirectord)

A: 

under the hood, nhibernate uses ado.net to connect to data sources. So you would need to look at how ado.net handles this situation.

But additionally, I don't think you would gain anything if you could do this.

Some background: Objects in nhibernate are tied to sessions, which are tied to session factories, which are tied to 1 connection.

Let's say you had a load balancer with ip 1 for reads. And it balances databases on ip's 2 and 3. And you hit ip 2 directly for writes.

So you have

Ip     Use
1      Balancer
2      Read / Write
3      Read  

If you read an object with a session tied to connection 1, you have to load that object and then save and flush to write on a session tied to connection 2. At that point, you have done 2 read's and 1 write. Whereas if you use one session factory then you have a read and a write (assuming the same session is alive through the read and the write, or that 2nd level caching is setup).

Tim Hoolihan
Thanks, Tim. An alternative plan would be to load balance reads on a set of slaves, and to reserve the master only for writes. The downside is that the master hardware may be under-utilized. Do you think this would work?
Jacko
because of how nhibernate has to read in order to write, I still don't think this would gain you anything. 2nd level caching reduces some, but your caches aren't shared between different session factories (connection strings), so you would still have redundant reads.
Tim Hoolihan
i think what you want here is some sort of db clustering, regardless, it has to be transparent (read 1 connection string) to nhibernate in order to have maximum gains.
Tim Hoolihan
Thanks! If all the slaves were accessible through a virtual IP load balancer, than the caching for reads would be work, I believe. However, I am now looking into MySQL cluster. Problem is, having to convince management to switch away from (horrors!) SQL Server and run on (double horrors!) Linux instead of Windows.
Jacko