spring 3.1: jdbcTemplate auto commit to false.

百般思念 提交于 2019-11-27 07:24:48

问题


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.

So instead of

insert --> commit --> rollback.

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


回答1:


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?

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.

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>



回答2:


I tried to catch your question, and there are some ideas to solve them. The way set autocommit to false in spring jdbctemplate:

  1. Set defaultAutoCommit property of DataSource to false.
  2. You can commit or rollback by writing codes as below:

    jdbcTemplate.getDataSource().getConnection.commit();
    jdbcTemplate.getDataSource().getConnection.rollback();
    


来源:https://stackoverflow.com/questions/10746377/spring-3-1-jdbctemplate-auto-commit-to-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!