2016-01-22 106 views
0

我目前正在开发一个web应用程序,它基本上是一个不同的供应商的投资组合网站。线程和Hibernate Spring MVC

我工作的这副本厂商的细节,并将其对新厂商,非常简单的一个线程。

线程旨在正常工作,但在选择特定的Catalog对象(此目录对象包含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; 
     } 
    } 
} 

而线程被调用像这样。因为我已经开发了许多应用程序像这样没有任何问题

CopySiteThread thread = new CopySiteThread(componentDTO, baseVendor, admin_id); 
thread.start(); 

一定次数的迭代后,被卡住就行Catalog cat = catIterator.next();

这个问题很奇怪。

任何帮助表示赞赏。

回答

0

实际的问题是在addCatalog方法CatalogDAO

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条款并关闭所有会话固定。

+0

如果您想回答您自己的问题,请提供一些解决方案示例代码片段,而不仅仅是描述您所做的事情 – nKognito