如何将一个只读休眠会话转换为在事务中写入(主/从DB) [英] how to convert a read only hibernate session to write during a transaction (Master/Slave DB)

查看:212
本文介绍了如何将一个只读休眠会话转换为在事务中写入(主/从DB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用spring + hibernate进行MySql复制,我有一个快速问题;

打开的事务处于只读模式,即指向从属数据库。
如果我想在交易过程中保存/更新/删除任何东西,将其转换为写入模式的最佳方式是什么?



我不想打开一个写模式的事务,因为大部分时间我都想要读东西。



我需要覆盖复制驱动程序/休眠模板这个东西吗?

解决方案

我们以只读模式打开事务,然后将其转换为写模式,因为只读连接不会像使用从DB一样成为问题。

我们重写 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);
}
};

在写入之前的应用程序逻辑中,我们检查

连接处于写入模式,写入。

else如果连接是只读的,那么首先在写入模式下进行写入操作,然后再次将其重新设置为只读。

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?

解决方案

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

这篇关于如何将一个只读休眠会话转换为在事务中写入(主/从DB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆