如何获得一个spring JdbcTemplate来read_uncommitted? [英] How can I get a spring JdbcTemplate to read_uncommitted?

查看:228
本文介绍了如何获得一个spring JdbcTemplate来read_uncommitted?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不能使用声明性的 @Transactional 方法,因为应用程序有多个JDBC数据源,我不想厌倦细节,但是足以说DAO方法传递正确的数据源来执行逻辑。所有JDBC数据源都具有相同的模式,当我暴露ERP系统的休息服务时,它们是分开的。

Firstly, I can't use the declarative @Transactional approach as the application has multiple JDBC data-sources, I don't want to bore with the details, but suffice it to say the DAO method is passed the correct data-source to perform the logic. All JDBC data sources have the same schema, they're separated as I'm exposing rest services for an ERP system.

由于这个遗留系统,有很多长期锁定的记录,我无法控制,所以我想要脏读。

Due to this legacy system there are a lot of long lived locked records which I do not have control over, so I want dirty reads.

使用JDBC我会执行以下操作:

Using JDBC I would perform the following:

private Customer getCustomer(DataSource ds, String id) {
    Customer c = null;
    PreparedStatement stmt = null;
    Connection con = null;
    try {
        con = ds.getConnection();
        con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        stmt = con.prepareStatement(SELECT_CUSTOMER);
        stmt.setString(1, id);
        ResultSet res = stmt.executeQuery();
        c = buildCustomer(res);
    } catch (SQLException ex) {
        // log errors
    } finally {
        // Close resources
    }
    return c;
}

好的,很多锅炉,我知道。所以我尝试了 JdbcTemplate 因为我正在使用spring。

Okay, lots' of boiler-plate, I know. So I've tried out JdbcTemplate since I'm using spring.

使用JdbcTemplate

Use JdbcTemplate

private Customer getCustomer(JdbcTemplate t, String id) {
    return t.queryForObject(SELECT_CUSTOMER, new CustomerRowMapper(), id);
}

更好,但它仍然使用默认事务隔离。我需要以某种方式改变这一点。所以我考虑使用 TransactionTemplate

Much nicer, but it's still using default transaction isolation. I need to somehow change this. So I thought about using a TransactionTemplate.

private Customer getCustomer(final TransactionTemplate tt,
                             final JdbcTemplate t,
                             final String id) {
    return tt.execute(new TransactionCallback<Customer>() {
        @Override
        public Customer doInTransaction(TransactionStatus ts) {
            return t.queryForObject(SELECT_CUSTOMER, new CustomerRowMapper(), id);
        }
    });
}

但是如何在此处设置事务隔离?我无法在回调的任何地方找到它或 TransactionTemplate 来执行此操作。

But how do I set the transaction isolation here? I can't find it anywhere on the callback or the TransactionTemplate to do this.

我正在阅读Spring在Action,第三版中解释了我已经完成的事情,尽管关于事务的章节继续使用带注释的声明性事务,但是如上所述我不能使用它,因为我的DAO需要在运行时确定哪个数据源根据提供的参数使用,在我的例子中是国家代码。

I'm reading Spring in Action, Third Edition which explains as far as I've done, though the chapter on transactions continues on to using declarative transactions with annotations, but as mentioned I can't use this as my DAO needs to determine at runtime which data-source to used based on provided arguments, in my case a country code.

任何帮助将不胜感激。

推荐答案

我目前通过直接使用 DataSourceTransactionManager 解决了这个问题,虽然看起来好像我没有节省太多锅炉板正如我最初的希望。不要误会我的意思,它更干净,虽然我仍然忍不住觉得必须有一个更简单的方法。我不需要读取事务,我只想设置隔离。

I've currently solved this by using the DataSourceTransactionManager directly, though it seems like I'm not saving as much boiler-plate as I first hoped. Don't get me wrong, it's cleaner, though I still can't help but feel there must be a simpler way. I don't need a transaction for the read, I just want to set the isolation.

private Customer getCustomer(final DataSourceTransactionManager txMan,
                             final JdbcTemplate t,
                             final String id) {
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);

    TransactionStatus status = txMan.getTransaction(def);
    Customer c = null;
    try {
        c = t.queryForObject(SELECT_CUSTOMER, new CustomerRowMapper(), id);
    } catch (Exception ex) {
        txMan.rollback(status);
        throw ex;
    }
    txMan.commit(status);
    return c;
}

我仍然会暂时保留这一个,因为我真的相信必须有更好的方法。

I'm still going to keep this one unanswered for a while as I truly believe there must be a better way.

参考 Spring 3.1.x文档 - 第11章 - 事务管理

这篇关于如何获得一个spring JdbcTemplate来read_uncommitted?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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