最近我遇到了Web应用程序的问题。我正在使用spring mvc restful application和hibernate作为jpa。
客户端可以使用以下格式构建xml文件:
<SCCF>
<registerSCCF>...</registerSCCF>
...
<registerSCCF>...</registerSCCF>
</SCCF>
然后,Web应用程序将registerSCCF标记内的每个数据映射到一个类,并将其保存在数据库中。
现在我遇到的问题是,当我使用soapui和多线程测试测试它时,我总是得到异常
[ERROR]发生断言失败(这可能表示Hibernate中存在错误,但更可能是由于会话的不安全使用) org.hibernate.AssertionFailure:draft.persistence.entity.dcrm.CustomersNoneSSO条目中的null id(发生异常后不刷新会话)
或
引起:org.hibernate.HibernateException:级联期间刷新是危险的
或
org.hibernate.SessionException:会话已关闭!
这是服务层代码:
@Transactional("dcrm")
public boolean postSCCFService(SCCFVO sccf){
CustomersNoneSSO cns = new CustomersNoneSSO();
cns.setAppid(sccf.getAppid());
cns.setCustomer_name(sccf.getCustomer_name());
cns.setCustomer_gender(sccf.getCustomer_gender());
cns.setContact_mobile(sccf.getContact_mobile());
cns.setContact_email(sccf.getContact_email());
cns.setAddress_province(sccf.getAddress_province());
cns.setAddress_city(sccf.getAddress_city());
cns.setCustomer_address(sccf.getCustomer_address());
cns.setCustomer_occupation(sccf.getCustomer_occupation());
cns.setPurchase_brand(sccf.getPurchase_brand());
cns.setPurchase_model(sccf.getPurchase_model());
cns.setPurchase_date(sccf.getPurchase_date());
cns.setPurchase_budget(sccf.getPurchase_budget());
cns.setOwncar_selected(sccf.getOwncar_selected());
cns.setOwncar_model(sccf.getOwncar_model());
cns.setTestdrive_permission(sccf.getTestdrive_permission());
cns.setMarketing_permission(sccf.getMarketing_permission());
Timestamp t = new Timestamp(new Date().getTime());
cns.setInsert_timestamp(t);
cns.setUpdate_timestamp(t);
cnsDao.makePersistent(cns);
}
如果我将所有setter设置为静态值,如:
cns.setContact_email("test@test.test");
而不是使用参数中的值,然后应用程序与多线程测试运行良好。
控制器调用服务方法:
@RequestMapping(value = "/test",method=RequestMethod.POST)
public @ResponseBody SCCFResponseList getPostResults(@RequestBody SCCFVOList sccf){
...
for(SCCFVO sccfvo : sccf.getSCCFVOList()){
...
boolean result = sccfservice.postSCCFService(sccfvo);
...
}
...
}
public class SCCFVOList {
这是请求正文类:
@XmlElement(name="registerSCCF")
public class SCCFVOList {
private Vector<SCCFVO> SCCFVOList = null;
public Vector<SCCFVO> getSCCFVOList(){
return SCCFVOList;
}
public void setSCCFVOList(Vector<SCCFVO> SCCFVOList){
this.SCCFVOList = SCCFVOList;
}
}
这里是dao
public class CNSDao extends GenericHibernateDAO<CustomersNoneSSO, Long> {}
public abstract class GenericHibernateDAO<T, ID extends Serializable>
implements GenericDAO<T, ID> {
private Class<T> persistentClass;
private Session session;
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public GenericHibernateDAO() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
public void setSession(Session s) {
this.session = s;
}
protected Session getSession() {
session = sessionFactory.getCurrentSession();
if (session == null)
throw new IllegalStateException(
"Session has not been set on DAO before usage");
return session;
}
public Class<T> getPersistentClass() {
return persistentClass;
}
@SuppressWarnings("unchecked")
public T makePersistent(T entity) {
getSession().saveOrUpdate(entity);
return entity;
}
public void makeTransient(T entity) {
getSession().delete(entity);
}
...
}
控制器方法或服务方法应该有问题。仍然不知道出了什么问题。
答案 0 :(得分:1)
你的dao有缺陷。
你的dao是一个单身人士,只有一个。 Hibernate Session
对象不是线程安全的,不应跨线程使用。
你有1个dao,2个线程,线程1获取会话的实例X1,线程2重置它到实例X2现在突然他们共享相同的会话,更不用说线程1甚至可能在2个不同的会话上运行。
正如我在评论中提到的那样,永远不要将Session
存储在实例变量中。删除它。
public abstract class GenericHibernateDAO<T, ID extends Serializable> implements GenericDAO<T, ID> {
private Class<T> persistentClass;
private SessionFactory sessionFactory;
public GenericHibernateDAO() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
另外我建议删除它,而使用Spring Data JPA可以省去创建和维护自己的通用dao的麻烦。 (你提到你使用JPA,如果实体被注释,它应该很容易做到。)