Using MySQL replication (Master/Slave) with MyBatis

≡放荡痞女 提交于 2019-12-11 09:06:46

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/5834046/using-mysql-replication-master-slave-with-mybatis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!