简体   繁体   中英

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 aa hint for me?

Thanks for your help.

Daniel

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM