Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it safe to use LastInsertId() ? #377

Closed
mayurshivakumar opened this issue Oct 28, 2015 · 5 comments
Closed

Is it safe to use LastInsertId() ? #377

mayurshivakumar opened this issue Oct 28, 2015 · 5 comments

Comments

@mayurshivakumar
Copy link

If connection is shared between many threads . Is it safe to use LastInsertId() ? Thanks for help

@arnehormann
Copy link
Member

No, it is not - the connection pooling by database/sql makes it unsafe. You can use it in transactions, though.

@erikdubbelboer
Copy link

I think he means Result.LastInsertId() which is implemented in result.go#L16 which just uses the insertId variable set in Exec() from the result of the executed statement.

So it's perfectly safe to use concurrently because it's stored in the result and has nothing to do with the connections.

@erikdubbelboer
Copy link

What is NOT safe is executing this after your insert:

SELECT LAST_INSERT_ID()

Because that might use a different connection because of the pooling.

@arnehormann
Copy link
Member

Yeah, thanks. Exactly what @erikdubbelboer says.

@mayurshivakumar
Copy link
Author

Thanks for the input i did mean Result.LastInsertId()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants