1

I have a table xxx with id (id_xxx int AUTO_INCREMENT ) and name (name_xxx varchar (50)), When I insert a new row in the table I made​​:

INSERT INTO xxx VALUES ​​("name for test");

and the result (int=1) of insertion is returned, then I display in my java interface a message "succseed!", until now it's a very basic and simple operation... BUT, when I want to return the inserted id_xxx,I have to do another query to the database:

INSERT INTO xxx VALUES ​​("name for test");
//after the insert response I made:
SELECT MAX (id_xxx) FROM xxx;

and I display in my java interface "succseed $$$ is your id_xxx "....

the second version can easily cause a serious error during concurrent access to multiple users: imagine a case when a user1 makes an insert... and then H2DB interrupt operations of this user then executes the insert of user2. when user1 executes a select max (id_xxx) the H2DB return A FALSE id_xxx...

(I hope that my example is clear otherwise I will schematize this problem).

how to solve this problem?

1 Answer 1

1

You should be able to retrieve keys generated by insert query, see 5.1.4 Retrieving Automatically Generated Keys.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.