2016-01-22 104 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