本文介绍了mybatis-插入交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于插入的mybatis映射器代码:

mybatis mapper code for insert:

<insert id="insert" parameterType="Shop" useGeneratedKeys="true">
    insert into shop(email, pswd, nickname, mobile, city, create_date, status) values (#{email}, #{pswd}, #{nickname}, #{mobile}, #{city}, #{createDate}, #{status})
    <selectKey keyProperty="id" order="AFTER" resultType="long">
        select currval('shop_id_seq')
    </selectKey>
</insert>

数据库是PostgreSQL 9.3.

The database is postgresql 9.3.

我的怀疑是:没有显式事务,当我使用select currval('shop_id_seq')从序列中检索ID时,如果其他线程也在执行插入操作,是否有可能得到错误的值?

My doubt is: without explicity transaction, when I retrieve the id from sequence with select currval('shop_id_seq'), is it possible to get the wrong value if other threads are also doing insert?

我认为不会,因为currval()函数在当前会话而不是全局会话的上下文中运行,但是我不确定.

I thought it won't, because currval() function runs in context of current session, not global session, but I am not that sure.

推荐答案

根据 PostgreSQL:文档:9.3:序列操作函数 ,序列函数currval:

这样您将获得正确的值.换句话说,序列是非交易性的.每个会话都会获得一个不同的序列值.序列中的更改无法撤消.

So you will get the correct value. In another way, the sequences are non-transactional. Each session gets a distinct sequence value. The changes in the sequence can not be undone.

这篇关于mybatis-插入交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:27