> For the complete documentation index, see [llms.txt](https://flamme1004.gitbook.io/flamme-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://flamme1004.gitbook.io/flamme-dev/info-1/framework/spring/springetc/mybatis/auto_increment-selectkey.md).

# auto\_Increment, selectKey

Mybatis에서 auto\_Increment selectKey를 사용했을 때 동일한 데이터가 들어가서 충돌이 나지 않을까?

```
<insert id="insertSurveyInfo" parameterType="Board">
    INSERT INTO board(boardID, title, content)
    VALUES(#{boarID}, #{title}, #{content})
    <selectKey resultType="int" keyProperty="iq" order="AFTER">
        SELECT LAST_INSERT_ID()
    </selectKey>        
</insert>
```

&#x20;SELECT LAST\_INSERT\_ID()를 쓰면 되지만 이게 쓰레드 환경에서 과연 안정할까?

{% embed url="<https://stackoverflow.com/questions/30959678/thread-safety-of-mysqls-select-last-insert-id>" %}

여기에 답변이 나와있다.&#x20;

'The ID that was generated is maintained in the server on a *per-connection basis*. This means that the value returned by the function to a given client is the first AUTO\_INCREMENT value generated for most recent statement affecting an AUTO\_INCREMENT column *by that client*. This value cannot be affected by other clients, even if they generate AUTO\_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

<https://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id>:

생성된 ID는 접속별로 서버에서 유지 관리된다. 즉, 기능에 의해 주어진 클라이언트에 반환된 값이 해당 클라이언트에 의해 AUTO\_INCREMENT 열에 영향을 미치는 가장 최근의 문장에 대해 생성된 첫 번째 AUTO\_INCREMENT 값임을 의미한다. 이 값은 다른 클라이언트가 자체 AUTO\_INGment 값을 생성하더라도 영향을 받을 수 없다. 이러한 행동은 각 클라이언트가 다른 클라이언트의 활동에 대해 염려하지 않고, 잠금이나 거래의 필요 없이 자신의 ID를 검색할 수 있도록 보장한다.

\--> 즉, ID를 생성해서 유지하는 것은 컨넥션 별로 따로 관리되며, 각각의 클라이언트가 정확한 수치를 가져오기 위해 테이블에 락을 걸거나 트랜잭션을 사용 할 필요가 없다는 것이다. 고로, 그냥 연달아 쿼리 날려주면 된다.

라 다른 클라이언트는 영향을 받지 않는다고 한다. 걱정 말고 쓰도록 하자!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://flamme1004.gitbook.io/flamme-dev/info-1/framework/spring/springetc/mybatis/auto_increment-selectkey.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
