Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SpringMVC进行线程和休眠_Java_Multithreading_Spring_Hibernate - Fatal编程技术网

Java 使用SpringMVC进行线程和休眠

Java 使用SpringMVC进行线程和休眠,java,multithreading,spring,hibernate,Java,Multithreading,Spring,Hibernate,我目前正在开发一个web应用程序,它基本上是一个针对不同供应商的投资组合网站 我在做一个线程,它复制了一个供应商的详细信息,并将其与一个新供应商进行比较,非常简单 线程本来可以正常工作,但当选择特定的目录对象(此目录对象包含Velocity模板)时,执行将停止,并且不会执行任何操作。再次调用线程只会挂起整个应用程序 这是我的密码 public class CopySiteThread extends Thread { public CopySiteThread(ComponentDTO

我目前正在开发一个web应用程序,它基本上是一个针对不同供应商的投资组合网站

我在做一个线程,它复制了一个供应商的详细信息,并将其与一个新供应商进行比较,非常简单

线程本来可以正常工作,但当选择特定的
目录
对象(此目录对象包含
Velocity
模板)时,执行将停止,并且不会执行任何操作。再次调用线程只会挂起整个应用程序

这是我的密码

public class CopySiteThread extends Thread {

    public CopySiteThread(ComponentDTO componentDTO, long vendorid, int admin_id) {
        /**Application specific business logic not exposed **/
    }

    public void run() {

        /** Application based Business Logic Not Exposed **/

        //Copy Catalog first
        List<Catalog> catalog = catalogDAO.getCatalog(vendorid);
        System.out.println(catalog);
        List<Catalog> newCat = new ArrayList<Catalog>();
        HashMap<String, Integer> catIdMapList = new HashMap<String, Integer>();

        Iterator<Catalog> catIterator = catalog.iterator();

        while (catIterator.hasNext()) {

            Catalog cat = catIterator.next();
            System.out.println(cat);
            int catId = catalogDAO.addTemplate(admin_id, cat.getHtml(), cat.getName(), cat.getNickname(), cat.getTemplategroup(), vendor.getVendorid());            
            catIdMapList.put(cat.getName(), catId);

            cat = null;
        }
    }
}
经过一定次数的迭代后,它会卡在第行
Catalog cat=catIterator.next()

这个问题很奇怪,因为我已经开发了很多这样的应用程序,没有任何问题


感谢您的帮助。

实际问题在于
CatalogDAO
中的
addCatalog
方法

    Session session = sf.openSession();
    Transaction tx = null;
    Integer templateID = null;

    Date date = new Date();

    try {
        tx = session.beginTransaction();
        Catalog catalog = new Catalog();
        //Business Logic
        templateID = (Integer) session.save(catalog);
    } catch (HibernateException ex) {
        if (tx != null) tx.rolback();
    } finally {
        session.close();
    }

    return templateID;

通过添加
finally
子句并关闭所有会话来修复此问题。

如果您想回答自己的问题,请提供一些解决方案示例代码片段,而不仅仅是对您所做工作的描述
    Session session = sf.openSession();
    Transaction tx = null;
    Integer templateID = null;

    Date date = new Date();

    try {
        tx = session.beginTransaction();
        Catalog catalog = new Catalog();
        //Business Logic
        templateID = (Integer) session.save(catalog);
    } catch (HibernateException ex) {
        if (tx != null) tx.rolback();
    } finally {
        session.close();
    }

    return templateID;