spring 3.1:jdbcTemplate 自动提交为 false. [英] spring 3.1: jdbcTemplate auto commit to 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?
自动提交配置通常在连接本身上设置.Connection
由 Datasource
创建.由于 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>
这篇关于spring 3.1:jdbcTemplate 自动提交为 false.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!