hibernate 不在连接上调用 setReadOnly(true)
我正在尝试设置我的 spring/hibernate 项目以使用主/从数据库设置。
事务似乎在 spring 中工作(例如,当我尝试在注释为 @Transactional(readOnly=true) 的方法中使用 hibernateTemplate.merge() 时,我得到一个异常)。但是hibernate并不在注释的方法中使用从数据库。
@Transactional(只读= 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">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
然后我用以下注释来注释管理器类或 DAO 类:
@Transactional(readOnly = true)
... 以及一些方法,
@Transactional(readOnly = false)
我希望类中的某些方法针对主机执行,一些方法针对从机执行,但它们都针对主机执行。我可能做错了什么?
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">
<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于@Transactional 注解,Hibernate 不会将 JDBC 连接切换到 R/O 模式,它仅防止更改的刷新。
Hibernate does not switch the JDBC connection to the R/O mode with regard to the
@Transactional
annotation, it only prevents flushing of changes.