mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.7k stars 12.82k forks source link

MySQL Replication Driver Support #559

Open DigitalSmile opened 8 years ago

DigitalSmile commented 8 years ago

Is there any way to get ReplicationDriver for MySQL (http://dev.mysql.com/doc/connectors/en/connector-j-master-slave-replication-connection.html) work out of the box with MyBatis?

I believe the main issue is in the configuration of DataSource and the statement executed. If the DataSource issue is clear for me (simply override method getConnection() to create new one and set the readOnly flag to true), the statement part is not. May be an option to the statement or environment configuration should be added to control which statements should be executed on slaves?

emacarron commented 8 years ago

It it is an statement configuration option then it can to the xml or to the @Options parameter. (this will require a change in mybatis for sure, but looks easy).

But I wonder it this should be an statement property or a SqlSession method...

BTW, if you are using MyBatis with Spring, Spring sets the readOnly flag got from the Transaction to the Connection, so you may already have a solution.

DigitalSmile commented 8 years ago

I have had a discussion with my team and It seems the statement-based is the one we should stick to. Session-based could be tricky, because several statements (not only SELECT's) could be used inside.

We are using xml to provide MyBatis with SQL statements and, from our point of view, the most helpful would be parameter for select section, something like:

<select id="getEventIdsByUserRoles" parameterType="map" resultType="int" readOnly="true">

BTW, if you are using MyBatis with Spring, Spring sets the readOnly flag got from the Transaction to the Connection, so you may already have a solution.

We are not using Spring, so this is not our case...

Sorry for the late reply. Take care.

DigitalSmile commented 8 years ago

Just a quick think about this feature - It seems, if we are in transaction this flag should be ignored, because there could be any statements, not only SELECT's. Also statements inside transaction could rely on each other - in that case running SELECT on slave could return unpredicted results for other statements. Anyway, the transaction, consisting of several SELECT's is pointless...

kazuki43zoo commented 7 years ago

I've removed milestone.

@emacarron @harawata Please set an appropriate milestone.