如何在事务期间将只读休眠会话转换为写入(主/从数据库)
使用 spring+hibernate 进行 MySql 复制,我有一个简单的问题;
打开的事务处于只读模式,即指向从属数据库。 如果我想在事务期间保存/更新/删除任何内容,将其转换为写入模式的最佳方法是什么?
我不想打开写入模式事务,因为大多数时候我想读取内容。
我是否需要覆盖这个东西的复制驱动程序/休眠模板?
Working with MySql replication with spring+hibernate, I have a quick question;
The transactions that are opened are in read-only mode i.e pointing to slave DB.
What is the best way to convert it to write mode if I want to save/update/delete any thing during that transaction?
I do not want to open a write mode transaction as most of the time I want read stuff.
Do I need to overide the replication Driver/Hibernate template for this thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们以只读模式打开事务,然后将其转换为写入模式,因为只读连接不会像辅助数据库那样成为问题。
我们重写 HibernateTemplate 类并创建方法以使会话处于写入模式
在写入之前的应用程序逻辑中,我们检查
连接处于写入模式,然后只需写入即可。
否则,如果连接是只读的,则首先将其设置为写入模式,执行写入操作,然后再次将其恢复为只读
We open transactions in read only mode and then convert it to write mode as read only connections will not be an issue as it is with salve DB.
We override the HibernateTemplate class and create methods to make session in write mode
In application logic before write we check
Connection is in write mode then simply write.
else if connection is readonly then first make it in write mode, do write operation and again make it back to readonly