PostgreSQL没有从表中释放锁

时间:2017-02-14 17:09:43

标签: java postgresql hibernate wildfly-9

我们正在将我们的应用程序数据从Oracle迁移到PostgreSQL。

环境详情:
Java 1.8
PostgreSQL 9.5企业版(XA DataSource)
Hibernate 4.3
WildFly 9.0.2

我们正在使用他们网站上提供的最新PostgreSQL驱动程序(postgresql-9.4.1212.jdbc42.jar)https://jdbc.postgresql.org/download.html

编辑:还尝试了postgres enterprise db附带的edb-jdbc17.jar驱动程序。结果还是一样。

我们还在max_prepared_connections文件中将postgresql.conf设置为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() + "|");
}

锁定数据库中的信息:

1 个答案:

答案 0 :(得分: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();
    }