Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

transactions - spring 3.1: jdbcTemplate auto commit to 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.

So instead of

insert --> commit --> rollback.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...