mybatis-插入交易 [英] mybatis - insert transaction

查看:77
本文介绍了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.

推荐答案

根据

返回nextval当前会话中为此序列最近获取的值. (如果在此会话中从未为该序列调用nextval,则会报告错误.)由于此查询返回的是会话本地值,因此它给出了可预测的答案自从当前会话以来,其他会话是否已执行nextval.

Return the value most recently obtained by nextval for this sequence in the current session. (An error is reported if nextval has never been called for this sequence in this session.) Because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed nextval since the current session did.

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

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-插入交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆