hibernate not calling setReadOnly(true) on connection
I'm trying to set up my spring / hibernate project to work with master/slave database setup.
Transactions seem to work in spring (for example when I try to hibernateTemplate.merge() within a method annotated as @Transactional(readOnly=true) I get an exception). But hibernate does not use the slave database within methods annotated.
@Transactional(readOnly=true).
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.ReplicationDriver"/>
<property name="url" value="jdbc:mysql:replication://master:3306,slave:3306/proust"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"开发者_如何学Python>
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
Then I annotate the manager class or DAO class with:
@Transactional(readOnly = true)
... and some methods with
@Transactional(readOnly = false)
I expect some methods in the class to be executed against the master, and some against the slave, but they all get executed against the master. What could I be doing wrong?
Hibernate does not switch the JDBC connection to the R/O mode with regard to the @Transactional
annotation, it only prevents flushing of changes.
精彩评论