我在spring环境下,启动容器并向数据库一张表插了几条记录.主键是采用自增长策略.
但是我采用多线程方式出现了异常.我先贴下代码
public class TestApp extends Thread{
private static DBService dbservice;
private static TableSystemBeans tbs = new TableSystemBeans(111,"showname",null,null);
static{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
dbservice = (DBService) app.getBean("dbserviceimpl");
}
public static void main(String[] args) {
for(int i=0;i<5;i++){
Thread t = new TestApp();
t.start();
}
}
@Override
public void run(){
for(int i=0;i<3;i++){
System.out.println(this.currentThread().getName()+"_mysname");
dbservice.saveObject(tbs);
}
}
}
异常提示detached entity passed to persist:
在网上查看原因是:是一个游离的对象要被持久化(save)时,其ID既要ORM框架为它生成ID值,而此实体的ID却已然有值。
反应在代码里面的问题就是每次保存的都是同一个对象,在第一次保存后就有了ID值,再次插入就会有
identifier of an instance of xxx.xxx was altered from ...异常
如果将tbs对象在每次保存前new出来就不会报错.测试通过.