spring4+hibernate4事务处理

在使用Spring和Hibernate进行事务处理时遇到'HibernateException: Could not obtain transaction-synchronized Session for current thread'错误。错误出现在job中调用Service时,尽管配置了事务处理。尝试更新了springContext.xml配置,包括添加<tx:annotation-driven>标签,但问题仍未解决。最终通过在Service实现类上添加@Transactional注解成功解决了该事务问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近总是遇到

HibernateException: Could not obtain transaction-synchronized Session for current thread错误

业务场景:在job中执行Service中的方法(事务是在xml中配置,针对所有service的方法的)


我的springContext.xml配置如下:

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
            <tx:method name="select*" propagation="NOT_SUPPORTED" read-only="true" />
            <tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception.class" />
        </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="true">
        <aop:pointcut id="txPointCut" expression="execution(* net.kkkk.*.*.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />

    </aop:config>

web.xml中配置:

<filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>

    </filter>

<filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

后来百度后,结合大牛的文章,springContext.xml做出如下调整:

<tx:annotation-driven proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
            <tx:method name="select*" propagation="NOT_SUPPORTED" read-only="true" />
            <tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception.class" />
        </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="true">
        <aop:pointcut id="txPointCut" expression="execution(* net.kkkk.*.*.service.impl.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />

    </aop:config>

发现还是不行,依然报上面的错误!

后来在对应的Service的实现类中,增加@Transactional注解,解决上面的报错!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值