简体   繁体   中英

concurrency with h2 database

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?

您应该能够检索由insert查询生成的密钥 ,请参阅5.1.4检索自动生成的密钥

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