我得到一个
org.hibernate.exception.ConstraintViolationException: Duplicate entry X for key 'PRIMARY'
造成的
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry X for key 'PRIMARY'
我正在做线程工作(有4个线程),逻辑如下:检查数据库中是否有东西(通过主键),如果它得到它并修改对象,否则创建一个新对象。我认为问题在于,当另外两个线程使用同一个对象并且它不在数据库中时,他们都想创建它并且保存会导致违规。令我困惑的是,我已经覆盖了 hashCode 和 equals 方法,当我捕获异常并查询数据库时,JVM 中的对象和数据库中的对象是相等的。这是否意味着不应该发生异常?
更让我困惑的是,有时当我查询数据库时,我会null
返回。
我尝试创建一个锁,然后对其进行同步,这样如果一个线程正在处理具有相同 id 的对象,那么另一个线程将让第一个线程完成,但这并没有让它变得更好。
我还尝试使用以下代码重新创建场景,但我运行得很好。
public static void main(String[] args) throws Exception {
ThreadPoolExecutor executionPool = new ThreadPoolExecutor(4, 4, 9999, TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>());
final Dao dao = new Dao();
TestIdentity t1 = new TestIdentity(1, "aaa", false);
dao.saveObject(t1);
TestIdentity t2 = new TestIdentity(1, "aaa", true);
dao.saveObject(t2);
dao.saveObject(t1);
dao.saveObject(t2);
final Wrapper w1 = new Wrapper(t1);
final Wrapper w2 = new Wrapper(t2);
Runnable runnable1 = new Runnable() {
public void run() {
while(true) {
w1.work(dao);
}
}
};
Runnable runnable2 = new Runnable() {
public void run() {
while(true) {
w2.work(dao);
}
}
};
executionPool.submit(runnable1);
executionPool.submit(runnable2);
}
private static class Wrapper {
TestIdentity t;
public Wrapper(TestIdentity t) {
this.t = t;
}
public void work(Dao dao) {
TestIdentity t2 = new TestIdentity(t.getId(), t.getField(), t.isBit());
dao.saveObject(t2);
t.setBit(true);
dao.saveObject(t2);
t.setBit(false);
}
}
哪个使用类
@Entity
@Table(name="cycling_scraped.test_identity")
public class TestIdentity {
int id;
String field;
boolean bit;
public TestIdentity() {
super();
}
public TestIdentity(int id, String field, boolean bit) {
super();
this.id = id;
this.field = field;
this.bit = bit;
}
@Id
@Column(name="pk_id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="field")
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
@Column(name="bit")
public boolean isBit() {
return bit;
}
public void setBit(boolean bit) {
this.bit = bit;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (bit ? 1231 : 1237);
result = prime * result + ((field == null) ? 0 : field.hashCode());
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestIdentity other = (TestIdentity) obj;
if (bit != other.bit)
return false;
if (field == null) {
if (other.field != null)
return false;
} else if (!field.equals(other.field))
return false;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "TestIdentity [id=" + id + ", field=" + field + ", bit=" + bit
+ "]";
}
}
和表
CREATE TABLE test_database.test_identity (
pk_id INT NOT NULL PRIMARY KEY,
field VARCHAR(100),
bit BIT
);