我想通过使用乐观锁定来处理并发执行.我@Version在我的实体类中包含了注释.
在我的代码中,我同时运行两个线程.有时它正确执行.有时它是投掷org.eclipse.persistence.exceptions.OptimisticLockException和javax.persistence.OptimisticLockException.
public class StudentCreation implements Runnable {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("jpaApp");
Student student = null;
@Override
public void run() {
for (int i = 1; i <= 2; i++) {
try {
if (i % 2 == 0) {
update("XYZ");
} else {
update("ABC");
}
} catch (Exception e) {
System.out.println("Update exception");
}
}
}
// main method
public static void main(String ar[]) {
StudentCreation std1 = new StudentCreation();
// creating two threads …Run Code Online (Sandbox Code Playgroud) jpa locking eclipselink optimistic-locking optimistic-concurrency