Search code examples
javamysqlibatisdatabase-replicationmybatis

Using MySQL replication (Master/Slave) with MyBatis


I am just wondering how I can use a Master/Slave MySQL replication database with MyBatis. JDBC offers a com.mysql.jdbc.ReplicationDriver (see here), but I couldn't find out where I can use similar things including all the nice properties I can configure (roundRobinLoadBalance, autoReconnect,...) in MyBatis.

Currently I have configured my none-replicated database in MyBatis like this:

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://localhost:3306/database" />
            <property name="username" value="root" />
            <property name="password" value="" />
        </dataSource>
    </environment>
    <environment id="production">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://xxx:3306/database" />
            <property name="username" value="production" />
            <property name="password" value="" />
        </dataSource>
    </environment>
</environments>

Has anyone a a hint for me?

Thanks for your help.

Daniel


Solution

  • ANOTHER POSSIBLE ANSWER

    If you notice, the properties you're setting in the xml for the driver are also common properties set and passed to jdbc. So, I wouldn't be surprised if MyBatis was just taking them and passing them right into the driver. So maybe try this:

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <!-- Just use ReplicationDriver -->
                <property name="driver" value="com.mysql.jdbc.ReplicationDriver" />
                <property name="url"
                    value="jdbc:mysql://localhost:3306/database" />
                <property name="autoReconnect" value="true" />
                <property name="roundRobinLoadBalance" value="true" />
                <property name="username" value="root" />
                <property name="password" value="" />
            </dataSource>
        </environment>
        <environment id="production">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <!-- Just use ReplicationDriver -->
                <property name="driver" value="com.mysql.jdbc.ReplicationDriver" />
                <property name="url"
                    value="jdbc:mysql://xxx:3306/database" />
                <property name="autoReconnect" value="true" />
                <property name="roundRobinLoadBalance" value="true" />
                <property name="username" value="production" />
                <property name="password" value="" />
            </dataSource>
        </environment>
    </environments>