简体   繁体   English

Spring Jdbc 模板 setAutocommit(false)

[英]Spring Jdbc template setAutocommit(false)

I want insert multiple rows in db table.我想在 db 表中插入多行。 I am using SpringJdbc .我正在使用SpringJdbc How can i manage transaction in SpringJdbc connection.我如何在 SpringJdbc 连接中管理事务。 My code is:我的代码是:

@Override
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public int addUserRelationshipMapping(final ArrayList<UserDO> userDOs, final long userId) throws UserDataException {
    final JdbcTemplate jd = this.getJdbctemplate();
    try {
        jd.getDataSource().getConnection().setAutoCommit(false);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    int totalAdded = 0;
    try {
        int[] isAdded = jd.batchUpdate(ADD_USER_RELATION_MAPPING, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                final long userRelationId = jd.queryForObject(USER_RELATION_KEY, Long.class);
                UserDO userDO = userDOs.get(i);
                    ps.setLong(1, userRelationId);
                    ps.setLong(2, userId);
                    ps.setLong(3, userDO.getprimaryUserId());
                    ps.setInt(4, 1);
                    ps.setInt(5, 0);
                    jd.getDataSource().getConnection().commit();
            }

            @Override
            public int getBatchSize() {
                return userDOs.size();
            }
        });
        totalAdded = isAdded.length;
    } catch (DuplicateKeyException dExp) {
        log.info("error for duplicate key exception ",dExp);
        log.error(dExp);

    } catch (DataAccessException dExp) {
        throw new UserDataException("error while adding user relation for userId is" + userId, dExp);
    }
    return totalAdded;
}

In this code userRelationId return always old values not updated table values.在此代码中,userRelationId 始终返回旧值而不是更新的表值。 So will use database connection commit.所以将使用数据库连接提交。

SOF question: Java MYSQL/JDBC query is returning stale data from cached Connection SOF 问题: Java MYSQL/JDBC 查询正在从缓存的连接返回陈旧数据

I got error message:我收到错误消息:

Caused by: java.sql.SQLException: Can't call commit when autocommit=true引起:java.sql.SQLException: Can't call commit when autocommit=true

So i need help for this.所以我需要帮助。 Advance thanks.提前谢谢。

See the example of how to set auto commit false请参阅如何将自动提交设置为 false 的示例

<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>

ref: spring 3.1: jdbcTemplate auto commit to false.参考: spring 3.1:jdbcTemplate 自动提交为 false。

You can also try the annotation based connection transaction management ?您也可以尝试基于注释的连接事务管理?
How can I config to turn off autocommit in Spring + JDBC? 如何配置在 Spring + JDBC 中关闭自动提交?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM