首页 > 解决方案 > 调用休眠保存导致挂起线程

问题描述

我们使用 Hibernate 和 Spring 使用 DAO 模式,其中应用程序为每个数据库调用调用一个单独的 DAO 类。我们使用 SQL Server 作为我们的数据库。

一段时间以来,我们看到对 Hibernate 的保存调用只会导致线程挂起,而我们对此一无所知。相同的代码在不同的环境中工作(单独的服务器、单独的数据库服务器但相同的版本)。

我们使用的是 Hibernate 4.3.10,自 2 年以来,应用程序没有任何变化。

可能出了什么问题?

调用保存的类的代码粘贴在下面。BusinessAlerts 只是一个 POJO 类。

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void persistBusinessAlert(BusinessAlerts businessAlert) throws DAOException {
    if (businessAlert == null) {
        return;
    }
    try {
        currentSession().save(businessAlert);
    } catch (HibernateException e) {
        throw new DAOException(e.getMessage(), e);
    }
}

我们已经确定了线程堆栈跟踪,但我无法在此处完整发布。

DB 的 Hibernate 属性是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd">

            <tx:annotation-driven transaction-manager="priceTransactionManager" />

            <bean class="mro.util.CryptoPropertyPlaceholderConfigurer">
                           <property name="location" value="common_db.properties" />
            </bean>

            <bean id="priceDataSource" destroy-method="close"
                           class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverClassName" value="${db.driver}" />
                <property name="url" value="${alert.db.url}" />
                <property name="username" value="${alert.db.username}" />
                <property name="password" value="${alert.db.password}" />
            </bean>

            <bean id="priceSessionFactory"
                           class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                <property name="dataSource" ref="priceDataSource" />
                <property name="annotatedClasses">
                    <list>
                        <value>BusinessAlerts</value>
                        <value>Executions</value>
                        <value>AlertCodes</value>
                    </list>
                </property>
                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                        <prop key="hibernate.show_sql">true</prop>
                    </props>
                </property>
            </bean>

            <bean id="priceTransactionManager"
                           class="org.springframework.orm.hibernate4.HibernateTransactionManager">
                <property name="sessionFactory" ref="priceSessionFactory" />
            </bean>
</beans>

currentSession()是一个 Hibernate 调用,当我们找到更多关于它的信息时,就会出现这种情况:

protected final org.hibernate.Session currentSession() throws org.springframework.dao.DataAccessResourceFailureException;
     0  aload_0 [this]
     1  invokevirtual org.springframework.orm.hibernate4.support.HibernateDaoSupport.getSessionFactory() : org.hibernate.SessionFactory [10]
     4  invokeinterface org.hibernate.SessionFactory.getCurrentSession() : org.hibernate.Session [11] [nargs: 1]
     9  areturn
      Line numbers:
        [pc: 0, line: 129]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: org.springframework.orm.hibernate4.support.HibernateDaoSupport

标签: springhibernatesave

解决方案


推荐阅读