javascript - spring mvc hibernate Restful(并发)问题

标签 javascript multithreading spring hibernate spring-mvc

最近我在网络应用程序方面遇到了问题。我将 spring mvc Restful 应用程序与 hibernate 作为 jpa 一起使用。

客户端可以使用以下格式构建 xml 文件:

<SCCF>
    <registerSCCF>...</registerSCCF>
    ...
    <registerSCCF>...</registerSCCF>
</SCCF>

然后,Web 应用程序会将 registerSCCF 标记内的每个数据映射到一个类并将其保存在数据库中。

现在我遇到一个问题,当我使用soapui和多线程测试对其进行测试时,我总是遇到异常

[ERROR] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) org.hibernate.AssertionFailure: null id in draft.persistence.entity.dcrm.CustomersNoneSSO entry (don't flush the Session after an exception occurs)

Caused by: org.hibernate.HibernateException: Flush during cascade is dangerous

org.hibernate.SessionException: Session is closed!

这是服务层代码:

@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");

而不是使用参数中的值,应用程序可以在多线程测试中运行良好。

有 Controller 调用服务方法:

@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;
}

}

这里是道

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);
}

...
}

Controller 方法或服务方法应该有问题。仍然不知道出了什么问题。

最佳答案

你的道有缺陷。

你的 dao 是一个单例,只有一个。 Hibernate Session 对象不是线程安全的,不应该跨线程使用。

您有 1 个 dao、2 个线程,线程 1 获取 session 的实例 X1,线程 2 将其重置为实例 X2,现在它们突然共享同一个 session ,更不用说线程 1 甚至可能在 2 个不同的 session 上运行。

正如我在评论中提到的,永远不要将 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,如果实体带有注释,那么应该很容易做到)。

关于javascript - spring mvc hibernate Restful(并发)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237672/

相关文章:

javascript - "innerHTML += ..."与 "appendChild(txtNode)"

java - 在notify()之后线程不恢复执行

spring - 使用DeadLetterPublishingRecoverer处理Spring-kafka错误

java - Spring 错误: No mapping found for HTTP request with URI [/myproject/] in DispatcherServlet with name 'appServlet'

javascript - 使用 jQuery 按变量值选择数组

javascript - 合并两个 JavaScript 脚本

java - getClass().getName() 返回 null

java - Spring集成和多线程,丢失了必需的 header

javascript - 使用 jQuery 提交值

Python多处理需要更长的时间