1

我有以下问题。在我的检票口应用程序中,我想使用 @Transactional 注释。我所有的 DAO 都加载了 @Component 注释;

@Component
@Transactional(propagation = Propagation.SUPPORTS)
public class AccountHibernateDataAccessHelper implements
    AccountDataAccessHelper
{
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void saveOrUpdate(T dataObject)
{
    try
    {
        Session session = getSessionProvider().getSession();
        session.saveOrUpdate(dataObject);
    }
    catch (HibernateException e)
    {
        String message = "Failed to batch save or update " + dataObject;
        log.error(message, e);
    }
}
@SuppressWarnings("unchecked")
public <R extends T> List<R> list(Class<R> class1)
{
    Asserts.assertNotNull("class1", class1);
    try
    {
        return createCriteria(class1).setCacheable(true)
                .setCacheRegion(getQueryCacheRegion()).list();
    } catch (HibernateException e)
    {
        String message = "Failed to get a list of" + class1;
        log.error(message, e);
        throw new DataAccessException(message, e);
    } catch (RuntimeException e)
    {
        log.error(e.toString(), e);
        throw new DataAccessException(e);
    }
}

}

我的 AccountDataAccesshelper 实现了以下接口

public interface DataAccessHelper<T>
{
public Serializable save(T dataObject);

public void saveOrUpdate(T dataObject);

public void update(T dataObject);

public void delete(T dataObject);

public void evict(T dataObject);

}

接下来是我的 appllicationcontext.xml 设置。

<context:component-scan base-package="nl.response.webapp" />
<context:annotation-config />

    <!-- hibernate session factory -->
<bean id="sessionFactory" class="nl.response.responseframework.app.ResponseAnnotationSessionFactoryBean">
    <property name="dataSource" ref="mainDataSource" />
    <property name="hibernateProperties">
        <map>
            <entry key="hibernate.session_factory_name" value="wtxwebapp" />
            <entry key="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory" />
            <entry key="hibernate.cache.query_cache_factory"
                value="nl.response.responseframework.service.InvalidatableQueryCacheFactory" />
            <entry key="hibernate.cache.region.factory_class"
                value="nl.response.wtxwebapp.core.hibernate.WtxWebAppEhCacheRegionFactory" />
            <entry key="hibernate.cache.region_prefix" value="hibernate-" />
            <entry key="hibernate.generate_statistics" value="false" />
            <entry key="hibernate.current_session_context_class" value="thread" />
            <entry key="hibernate.cache.use_structured_entries" value="true" />
            <entry key="hibernate.cache.use_query_cache" value="true" />
            <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        </map>
    </property>
</bean>

   <bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
        <property name="driverClass" ref="hibernate.connection.driver_class" />
        <property name="jdbcUrl" ref="hibernate.connection.url" />
        <property name="username" ref="hibernate.connection.username" />
        <property name="password" ref="hibernate.connection.password" />
        <property name="poolName" ref="hibernate.connection.username"/>
        <property name="idleConnectionTestPeriodInMinutes" value="60" />
        <property name="idleMaxAgeInMinutes" value="240" />
        <property name="maxConnectionsPerPartition" value="50" />
        <property name="minConnectionsPerPartition" value="5" />
        <property name="partitionCount" value="2" />
        <property name="poolAvailabilityThreshold" value="10" />
        <property name="acquireIncrement" value="4" />
        <property name="statementsCacheSize" value="50" />
        <property name="releaseHelperThreads" value="3" />
        <property name="serviceOrder" value="LIFO" />
    </bean>

<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="mainDataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

会话提供者提供如下会话

public Session getSession()
{
    getSessionFactory().openSession();
}

当我使用上述会话方法时,没有提交任何内容,并且由于连接没有正确关闭,所以我收到一个错误。

当我使用 getSessionFactory.getCurrentSession() 时,出现以下错误

如果没有活动事务,createCriteria 无效

我错过了什么?我已经尝试让这个工作好几天了,我没有选择:S

4

1 回答 1

1

Session每次打电话都会打开一个新的getSession()

试试这个:

public Session getSession() {
    SessionFactoryUtils.getSession(getSessionFactory(), true);
}

这将获得 Spring 管理的休眠会话,按需创建,而不是每次都打开一个新会话。

请注意,如果您不使用OpenInViewSessionFilter或类似的东西,您仍然会为每个查询创建一个新会话,除非您在事务中。但这一次,Spring 会自动为您关闭它们。

于 2012-02-08T18:36:35.910 回答