2017-05-08 61 views
1

我需要創建一個進程,將查詢Web服務來提取信息,然後將數據保存在我的數據庫中。但是,因爲這個過程非常耗時,所以我想讓它在後臺運行。後臺線程拋出HibernateException - 「沒有休眠會話綁定到線程...」

目前,我有一個ProcessHandler這是由UI中的按鈕調用。該處理程序創建一個Thread,它應該在後臺運行該進程。但是,我得到HibernateException與消息No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

我已經在配置XML文件中定義的ProcessHandler(有幾個)如下(這是一個非常通用的定義):

<bean class="com.project.ProcessHandler" parent="parentHandler" /> 

裏面ProcessHandler,調用此過程中的代碼也很通用:

Thread t = new Thread(new WorkerThread(alphaManager, bravoManager, charlieManager)); 
t.start(); 

這是當前實現的WorkerThread

public class WorkerThread implements Runnable { 

    private Manager alphaManager; 
    private Manager bravoManager; 
    private Manager charlieManager; 

    public WorkerThread() { 
     this.alphaManager = null; 
     this.bravoManager = null; 
     this.charlieManager= null; 
    } 

    public WorkerThread(Manager alphaManager, Manager bravoManager, Manager charlieManager) { 
     this.alphaManager = alphaManager; 
     this.bravoManager = bravoManager; 
     this.charlieManager= charlieManager; 
    } 

    @Override 
    public void run() { 
     // code to query webservice and extract data... 

     saveToDbMethod(data); 
    } 

    @Transactional(propagation = Propagation.REQUIRED) 
    private void saveToDbMethod(String data) { 
     // code to process data... 

     alphaManager.save(entityA); 
     bravoManager.save(entityB); 
     charlieManager.save(entityC); 
    } 
} 

默認的構造函數是我嘗試將WorkerThread定義爲我的配置xml文件(之一)中的bean時的剩餘部分。

任何人都可以通過給我一些關於如何排除故障的提示來幫助我嗎?

回答

1

問題是,您手動創建線程並期望它的行爲像一個彈簧管理bean。

由於ProcessHandler是合法豆,我會做的是以下幾點:

1)創造條件,有管理的依賴和@Transactional方法seaparate服務類:

@Service 
public class Service{ 

    private Manager alphaManager; 
    private Manager bravoManager; 
    private Manager charlieManager; 

    public Service(Manager alphaManager, Manager bravoManager, Manager charlieManager) { 
     this.alphaManager = alphaManager; 
     this.bravoManager = bravoManager; 
     this.charlieManager= charlieManager; 
    } 

    @Transactional(propagation = Propagation.REQUIRED) 
    private void saveToDbMethod(String data) { 
     // code to process data... 

     alphaManager.save(entityA); 
     bravoManager.save(entityB); 
     charlieManager.save(entityC); 
    } 
} 

2)Service注入ProcessHandler

<bean class="com.project.ProcessHandler" parent="parentHandler"> 
    <property name="service" ref="service"> 
</bean> 

3)最後通過ServiceWorkerThread

public class WorkerThread implements Runnable { 

    private Service service; 

    public WorkerThread(Service service) { 
     this.service = service; 
    } 

    @Override 
    public void run() { 
     // code to query webservice and extract data... 

     service.saveToDbMethod(data); 
    } 
} 

和:

Thread t = new Thread(new WorkerThread(service)); 
t.start(); 

現在您的操作應該是事務和會話中。

+0

我應該如何在配置中定義'service'? – ohseekay

+0

我在'ProcessHandler'的定義下面定義瞭如下:'然後,我得到'org.springframework.beans.factory.NoSuchBeanDefinitionException異常:沒有定義名爲'service'的bean' – ohseekay

+0

我已經設法通過遵循此解決方案來解決我的問題,並在此之後進行了一些故障排除。我將在隨後的評論中詳細介紹我的故障排除過程。 – ohseekay

相關問題