简体   繁体   中英

Is selectKey thread safe in MyBatis?

I have an insert statement as follows that runs on an Oracle 11g DB:

<insert id="insertSelective" parameterType="myObj">
    <selectKey keyProperty="seq_id" order="AFTER" resultType="Long">
        SELECT mySeq.currval FROM dual
    </selectKey>        
    INSERT INTO myTable
    <include refid="myValues"/>
</insert>

If multiple threads are running in different sessions, is it possible for one thread to get the sequence value incremented by other sessions?

In other words, does MyBatis let/block another INSERT INTO statement in between the INSERT INTO and <selectKey> statements located in a certain <insert> tag?

There is a similar question dealing with PostgreSQL here . But, since Oracle Sequences are shared by all sessions, I cannot trust the DB to give me the last inserted value in the current session.

The short answer is mybatis doesn't do any implicit locking in this situation.

Even if mybatis would make execution of both statements (select and insert) atomic as you want it to be this wouldn't help much as the second concurrent transaction will still see the same value. You need to take care about concurrency yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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