|P is for Practical|
Subclassing DBI and creating two connectionsby BMaximus (Chaplain)
|on Dec 02, 2005 at 17:21 UTC||Need Help??|
BMaximus has asked for the
wisdom of the Perl Monks concerning the following question:
My fellow Monks, I have a delicate problem I need to solve and I need your help.
My project at work is to enable a MySQL master, slave architecture to work as one server as a read only server where the statement is routed to it only if it does nothing to the database (meaning it's only retieving data, not changing it) and the other server is a write server and I'm sure you get it, if the statement does something to the database the changes are routed through to that server and the changes made to the read only server through replication.
My question is other than subclassing DBI is there a way to do this already? The example I saw in the DBI documentation made it clear as to how to subclass DBI when creating your own connection, do, prepare and execute methods when you're only using one connection, but I'm at a loss as to how I can use two connections and have them used intelligently. I understand I need to call the SUPER::connect after I get all the needed connection information and pass back a handle and it gets messy here. I need to create two connections and have them both available. Do I bless the new class with both connections storred in a hash or do I need to do somethig else? I have an idea on how to get the new class to use the connections intelligently but that could change depending on your answer.
I'm pretty sure this has been done before and I feel like I'm reinventing the wheel here. I hope there's a simple answer to this.
Thank you for your time.