2017-02-14 5 views
0

Nous migrons nos données d'application d'Oracle vers PostgreSQL.PostgreSQL ne libère pas les verrous de la table

Environnement Détails:
Java 1.8
PostgreSQL 9.5 Enterprise Edition (XA DataSource)
Hibernate 4.3
wildfly 9.0.2

Nous utilisons le dernier pilote PostgreSQL (postgresql-09/04/1212. jdbc42.jar) disponible sur leur site Web (https://jdbc.postgresql.org/download.html)

Modifier: Également essayé edb-jdbc17.ja pilote r qui vient avec postgres enterprise db. Toujours le même résultat. Nous avons également mis max_prepared_connections à 100 dans postgresql.conf fichier.

La méthode ci-dessous prend un objet et utilise la transaction de démarrage d'hibernate, puis valide la transaction. La méthode ne génère aucune erreur ou exception. Mais dans la base de données, l'objet n'est pas enregistré et l'application acquiert des verrous sur la table, ce qui entraîne un blocage. Le même code fonctionne parfaitement avec 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() + "|"); 
} 

informations Locks base de données:

+0

Je ne suis pas Hibernate expert, j'en connais quelques uns sur Postgres - Postgres détient le verrou à la fin de la transaction. Votre code ne met pas fin à une transaction. C'est un problème assez important. –

+0

t.commit(); va enregistrer et mettre fin à la transaction. – Maverick

+0

regardez dans postgres - consignez toutes les instructions ou les instructions en attente - peut-être que hiberner ne le fait pas. l'option table pg_stat_activity ou log_min_duration_statement peut vous aider. –

Répondre

0

Vous devez fermer la session après la validation (idéalement dans le bloc 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

Merci pour la réponse Maciej. Je suis en train de terminer la séance en bloc. Je viens de coller la définition complète de la méthode. – Maverick

+0

@Leozeo Essayez de faire 'session.flush()' avant 't.commit()' comme déclaré dans Maciej exemple – rvit34

+0

Essayé session de vidage avant la validation de la transaction, mais cela n'a pas fonctionné. – Maverick