mysqldatabase-replicationmysql-connectorconnector-j

Mysql JDBC master/slave for seperated read write operations


I've started to check mysql connector j's replication paradigm and see that we can seperate read and write operations on master and slave databases.

I've checked below page and get some clues on the operation but still need to know how does mysql-jdbc understands which server is master and which servers are slaves ? ( might be a silly one, sorry for this )

http://www.dragishak.com/?p=307


Solution

  • The ReplicationDriver or NonRegisteringReplicationDriver decides the first url as master and the rest considered as slaves

    The point you should of take into consideration is : If you are using ReplicationDriver or NonRegisteringReplicationDriver you need to give at least two hosts contains the same db instance. Otherwise you will get an SQLException telling : "Must specify at least one slave host to connect to for master/slave replication load-balancing functionality".

    One more point : You don't actually need to create an instance of NonRegisteringReplicationDriver. Because ReplicationDriver is also using it. You can check it by let your application throw and Exception. What you will see is; the DB connection was tried by NonRegisteringReplicationDriver.connect(..) method.

    Edit(!) : You actually don't need to create non of spesific driver for your system. What you need to know is what are you doing and the correct connection url. Because the Driver class itself checks the url against replication pattern and loadbalance pattern. Then it triggers the required driver instance.