2017-02-14 29 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