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?

Marwen Trabelsi
  • 4,167
  • 8
  • 39
  • 80

1 Answers1

1

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

axtavt
  • 239,438
  • 41
  • 511
  • 482