2017-02-14 36 views
0

我們正在將我們的應用程序數據從Oracle遷移到PostgreSQL。PostgreSQL未釋放表中的鎖

環境細節:
的Java 1.8
的PostgreSQL 9.5企業版(XA數據源)
的Hibernate 4.3
WildFly 9.0.2

我們正在利用最新的PostgreSQL驅動(PostgreSQL的,1212年4月9日。 (https://jdbc.postgresql.org/download.html

編輯:也試過edb-jdbc17.ja r驅動程序,它附帶postgres企業數據庫。還是一樣的結果。

我們還在postgresql.conf文件中將max_prepared_connections設置爲100。

下面給出的方法是採用一個對象並使用hibernate啓動事務,然後提交事務。方法不會引發任何錯誤或異常。但是在數據庫中,對象沒有得到保存,應用程序正在獲取導致死鎖的表上的鎖。 相同的代碼與Oracle完美結合。從數據庫

public void createObject(Object obj) throws CSTransactionException { 
    Session s = null; 
    Transaction t = null; 
    try { 

     try { 
      obj = performEncrytionDecryption(obj, true); 
     } catch (EncryptionException e) { 
      throw new CSObjectNotFoundException(e); 
     } 


     try{ 
      obj = ObjectUpdater.trimObjectsStringFieldValues(obj); 
     }catch(Exception e){ 
      throw new CSObjectNotFoundException(e); 
     } 



     s = HibernateSessionFactoryHelper.getAuditSession(sf); 
     t = s.beginTransaction(); 
     s.save(obj); 
     t.commit(); 
     s.flush(); 
     auditLog.info("Creating the " + obj.getClass().getName().substring(obj.getClass().getName().lastIndexOf(".")+1) + " Object ");   
    } 
catch (PropertyValueException pve) 
    { 
     try { 
      t.rollback(); 
     } catch (Exception ex3) { 
      if (log.isDebugEnabled()) 
       log.debug("Authorization|||createObject|Failure|Error in Rolling Back Transaction|" + ex3.getMessage()); 
     } 
     if (log.isDebugEnabled()) 
      log 
        .debug("Authorization|||createObject|Failure|Error in Rolling Back Transaction|" + pve.getMessage()); 
     throw new CSTransactionException(
       "An error occured in creating the " + StringUtilities.getClassName(obj.getClass().getName()) + ".\n" + " A null value was passed for a required attribute " + pve.getMessage().substring(pve.getMessage().indexOf(":")), pve); 
    } 
    catch (ConstraintViolationException cve) 
    { 
     try { 
      t.rollback(); 
     } catch (Exception ex3) { 
      if (log.isDebugEnabled()) 
       log.debug("Authorization|||createObject|Failure|Error in Rolling Back Transaction|" + ex3.getMessage()); 
     } 
     if (log.isDebugEnabled()) 
      log 
        .debug("Authorization|||createObject|Failure|Error in Rolling Back Transaction|" + cve.getMessage()); 
     throw new CSTransactionException(
       "An error occured in creating the " + StringUtilities.getClassName(obj.getClass().getName()) + ".\n" + " Duplicate entry was found in the database for the entered data" , cve); 
    }  
    catch (Exception ex) { 
     log.error(ex); 
     try { 
      t.rollback(); 
     } catch (Exception ex3) { 
      if (log.isDebugEnabled()) 
       log 
         .debug("Authorization|||createObject|Failure|Error in Rolling Back Transaction|" 
           + ex3.getMessage()); 
     } 
     if (log.isDebugEnabled()) 
      log 
        .debug("Authorization|||createObject|Failure|Error in creating the " 
          + obj.getClass().getName() 
          + "|" 
          + ex.getMessage()); 
     throw new CSTransactionException(
       "An error occured in creating the " 
         + StringUtilities.getClassName(obj.getClass() 
           .getName()) + "\n" + ex.getMessage(), ex); 
    } finally { 
     try { 

      s.close(); 
     } catch (Exception ex2) { 
      if (log.isDebugEnabled()) 
       log 
         .debug("Authorization|||createObject|Failure|Error in Closing Session |" 
           + ex2.getMessage()); 
     } 
    } 
    if (log.isDebugEnabled()) 
     log 
       .debug("Authorization|||createObject|Success|Successful in creating the " 
         + obj.getClass().getName() + "|"); 
} 

鎖的信息:

+0

我不是Hibernate專家,我知道關於Postgres的一些消息 - Postgres對交易結束時持有鎖定。您的代碼不會結束交易。這是非常關鍵的問題。 –

+0

t.commit();將保存並結束交易。 – Maverick

+0

尋找postgres - 記錄所有語句或等待語句 - 也許休眠不會。表pg_stat_activity或log_min_duration_statement選項可以幫助您。 –

回答

0

你必須在提交之後(最好在finally塊)關閉會話:

s = HibernateSessionFactoryHelper.getAuditSession(sf); 
t = s.beginTransaction(); 
    try { 
      s.save(obj); 
      session.flush(); 
      session.clear(); 
      t.commit(); 
      auditLog.info("Creating the " + obj.getClass().getName().substring(obj.getClass().getName().lastIndexOf(".")+1) + " Object ");   
     }   
    }catch (Exception e) { 
     t.rollBack(); 
    }finally{ 
     s.close(); 
    } 
+0

感謝您的回覆Maciej。我正在關閉會議終於封鎖。我只是粘貼了完整的方法定義。 – Maverick

+0

@Leozeo嘗試在't.commit()'之前創建'session.flush()',如Maciej示例中所聲明 – rvit34

+0

在事務提交之前嘗試刷新會話,但它不起作用。 – Maverick