OpenSessionInViewInterceptor无法使用Hibernate

时间:2016-06-27 13:42:58

标签: spring hibernate spring-data hibernate-criteria

我在我的代码中使用OpenSessionInViewInterceptor 代码段:

<context:component-scan base-package="com.abc.service" />

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="openSessionInViewInterceptor" />
        </list>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/abcTest" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>


<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.abc.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<bean id="applicationUtil" class="com.abc.util.ApplicationUtil" />

<bean id="auctionDao" class="com.abc.dao.AuctionDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

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

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean name="openSessionInViewInterceptor"
    class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

其他细节我在这里没有提到。 在我的DAO中,我有一个方法

@SuppressWarnings("unchecked")
@Transactional(readOnly = false)
public List<Item> fetchCurrentlyActiveBids() throws RuntimeException {

    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(Item.class);
    criteria.add(Restrictions.eq("isActive", true));
    List<Item> itemList = criteria.list();
    //session.close();
    return itemList;
}

我可以点击我的dao并获取数据几次。然后当我使用rest client(thorugh rest end point)反复调用此方法时,我收到以下错误:

{   “statusCode”:500,   “statusDescription”:“内部服务器错误”,   “errorMessage”:“不支持嵌套事务” }`

无法理解为什么不支持嵌套事务?如果我使用OpenSessionInViewInterceptor,它应该在使用后自动关闭会话。请提出解决方案

已编辑的配置栏

0 个答案:

没有答案