我使用mysql主/从复制(写主和读从)与ReplicationDriver.My连接URL如下:
"jdbc:mysql:replication://master:3306,slave1:3307,slave2:3308/sampledb?allowMasterDownConnections=true"
我使用Spring Spring MyBatis模块。
我将我的交易标记为只读如下:
@Override
@Transactional(rollbackFor=Exception.class,readOnly=true)
public Sample getSample(SampleKey sampleKey) throws SampleException {
//Call MyBastis based DAO with "select" queries.
}
但是当我看到事务/db日志时,它显示即使对于“只读”事务,ReplicationDriver也会首先命中master。请注意“获取连接”和“释放JDBC连接”行。
为什么会发生这种情况?
1)无论是只读查询,JDBC驱动程序是否仍然“ping”master检查其是否活动,然后转到从库进行实际查询?
2)如果只读=true,Spring不设置基础Connection对象的只读(true)吗?
2014-03-19 12:32:28,280 DEBUG [http-8080-2] [AbstractPlatformTransactionManager.java:365] - Creating new transaction with name [com.rakuten.gep.foo.businesslogic.impl.SampleBusinessLogicImpl.getSample]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '',-java.lang.Exception
2014-03-19 12:32:28,390 DEBUG [http-8080-2] [DataSourceTransactionManager.java:204] - Acquired Connection [jdbc:mysql://master:3306/, UserName=root@10.174.10.72, MySQL Connector Java] for JDBC transaction
CACHED DAO
Trying to retrive from the Cache
2014-03-19 12:32:31,334 DEBUG [http-8080-2] [Slf4jImpl.java:47] - ooo Using Connection [jdbc:mysql://slave1:3307/, UserName=root@10.174.10.72, MySQL Connector Java]
2014-03-19 12:32:31,334 DEBUG [http-8080-2] [Slf4jImpl.java:47] - ==> Preparing: select tbl.item_id, tbl.item_name, tbl.create_time, tbl.update_time from sample_tbl tbl where tbl.item_id=?
2014-03-19 12:32:31,335 DEBUG [http-8080-2] [Slf4jImpl.java:47] - ==> Parameters: 79bc3c80-af0a-11e3-a8e4-b8e8560f9d02(String)
Adding SampleTbl id to cache : 79bc3c80-af0a-11e3-a8e4-b8e8560f9d02
2014-03-19 12:32:31,340 DEBUG [http-8080-2] [AbstractPlatformTransactionManager.java:752] - Initiating transaction commit
2014-03-19 12:32:31,342 DEBUG [http-8080-2] [DataSourceTransactionManager.java:264] - Committing JDBC transaction on Connection [jdbc:mysql://slave1:3307/, UserName=root@10.174.10.72, MySQL Connector Java]
2014-03-19 12:32:31,382 DEBUG [http-8080-2] [DataSourceTransactionManager.java:322] - Releasing JDBC Connection [jdbc:mysql://master:3306/, UserName=root@10.174.10.72, MySQL Connector Java] after transaction
我的连接设置是:
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/sample"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
username="root"
password="root"
driverClassName="com.mysql.jdbc.ReplicationDriver"
url="jdbc:mysql:replication://master:3306,slave1:3307,slave2:3308/sampledb?allowMasterDownConnections=true"
connectionCachingEnabled="true"
connectionCacheProperties="{MaxStatementsLimit=10}"
removeAbandoned="true"
removeAbandonedTimeout="600"
logAbandoned="true"
timeBetweenEvictionRunsMillis="1000"
minEvictableIdleTimeMillis="1000"
testOnBorrow="false"
testOnReturn="false"
validationQuery="select null"
testWhileIdle="true"
maxActive="10"
maxIdle="3"
maxWait="1000"
defaultAutoCommit="false"/>
</Context>
您所看到的是MySQL JDBC驱动程序正在管理与物理服务器(无论是主服务器还是从服务器)的连接这一事实的副作用。连接池和Spring事务管理器都不知道数据库连接正在与多台服务器通信的事实。一切看起来都正常工作,但我将解释为什么看起来主连接正在被使用。
希望这能有所帮助。如果你需要更详细的信息,请告诉我。