如何在事务期间将只读休眠会话转换为写入(主/从数据库)

发布于 2024-12-23 16:24:41 字数 188 浏览 5 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-12-30 16:24:41

我们以只读模式打开事务,然后将其转换为写入模式,因为只读连接不会像辅助数据库那样成为问题。

我们重写 HibernateTemplate 类并创建方法以使会话处于写入模式

 public final void writeEnabled(){
    getSession().doWork(jdbcWorkWriteEnabled);
}

public final void writeDisabled(boolean flush){
    if(flush)
        flush();
    getSession().doWork(jdbcWorkWriteDisabled);
}

public static final void writeEnabled(Session session){
    session.doWork(jdbcWorkWriteEnabled);
}

public static final void writeDisabled(boolean flush,Session session){
    if(flush)
        session.flush();
    session.doWork(jdbcWorkWriteDisabled);
}

final static Work jdbcWorkWriteEnabled = new Work(){
    public void execute(Connection connection) throws SQLException {
        connection.setReadOnly(false);
    }
};

final static Work jdbcWorkWriteDisabled = new Work(){
    public void execute(Connection connection) throws SQLException {
        connection.setReadOnly(true);
    }
};

在写入之前的应用程序逻辑中,我们检查
连接处于写入模式,然后只需写入即可。
否则,如果连接是只读的,则首先将其设置为写入模式,执行写入操作,然后再次将其恢复为只读

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

 public final void writeEnabled(){
    getSession().doWork(jdbcWorkWriteEnabled);
}

public final void writeDisabled(boolean flush){
    if(flush)
        flush();
    getSession().doWork(jdbcWorkWriteDisabled);
}

public static final void writeEnabled(Session session){
    session.doWork(jdbcWorkWriteEnabled);
}

public static final void writeDisabled(boolean flush,Session session){
    if(flush)
        session.flush();
    session.doWork(jdbcWorkWriteDisabled);
}

final static Work jdbcWorkWriteEnabled = new Work(){
    public void execute(Connection connection) throws SQLException {
        connection.setReadOnly(false);
    }
};

final static Work jdbcWorkWriteDisabled = new Work(){
    public void execute(Connection connection) throws SQLException {
        connection.setReadOnly(true);
    }
};

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文