我正在研究 EJB 3.0,其中实体 bean 由 JPA 管理。我的问题是,如果两个或多个用户将尝试使用相同的表单同时插入同一个表,JPA 将如何处理这种情况。
1 回答
It will manage it just fine, by using database transactions. If two threads try to create the same row (i.e. with the same primary key) at the same time, one will succeed, and the other will get an exception from the database, which will cause a rollback of its transaction. That means that all the other inserts, updates and deletes made in the same transaction will also be rollbacked, or cancelled if you prefer, leaving the database in a coherent state. That's the A in ACID.
If two threads insert two different rows at the same time in the same table, then the database will handle that just fine, and both rows will be inserted.