春天3.1:jdbcTemplate自动提交为false. [英] spring 3.1: jdbcTemplate auto commit to false.

查看:641
本文介绍了春天3.1:jdbcTemplate自动提交为false.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们是在Spring jdbctemplate中将autocommit设置为false的一种方法.

Hi Is their a way to set autocommit to false in spring jdbctemplate.

我想在事务结束时提交查询,而不是事务(其中是回滚选项).

The thing is instead of transaction (where their is rollback option), I want to have query committed at end of transaction.

所以不是

插入->提交->回滚.

insert --> commit --> rollback.

我要插入->失败->(无提交).

I want insert --> fail --> (no commit).

推荐答案

我不理解您的全部问题,但是我可以回答第一部分:在spring jdbctemplate中是否可以将autocommit设置为false?

I did not understand your whole question, but I can answer the first part: Is there a way to set autocommit to false in spring jdbctemplate?

通常在连接本身上设置自动提交配置. ConnectionDatasource创建.由于JdbcTemplate在请求到数据源的连接中没有选项来手动禁用自动提交,因此实现此目的的方法是使用Datasource,该默认创建的autocommit设置为false的连接.

The autocommit configuration is normally set on the connection itself. The Connection is created by the Datasource. As the JdbcTemplate does not have an option to manually disable auto commit in the connections it requests to the Datasource, the way to achieve this is using a Datasource that creates connections with autocommit set to false by default.

使用apache commons BasicDataSource的此示例配置可实现以下目标:

This example configuration using apache commons BasicDataSource achieves that:

<bean id="database" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
        <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            ...
            <property name="defaultAutoCommit" value="false" />
            ...
        </bean>
    </property>
</bean>

这篇关于春天3.1:jdbcTemplate自动提交为false.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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