Could not obtain transaction-synchronized Session for current thread Shiro 引起的事务报错问题......

错误信息为 Could not obtain transaction-synchronized Session for current thread

我是从google搜索,百度搜索,才都试出来的,希望大家不要入坑啊,我刚爬出来,下班回家啊。

以下是解决方案:
按照这些方法,总有一款适合你的,童鞋们
第一种方法
Shiro 引起的事务报错问题
解决办法:
在MyRealm中做如下修改:(两种方式,任意一种即可 药到病除。)
在Shiro框架中注入Bean时,不使用@Autowire,使用ApplicationContextRegister.getBean()方法,手动注入bean。保证该方法只有在程序完全启动运行时,才被注入。
使用 @Autowire + @Lazy 注解,设置注入到Shiro框架的Bean延时加载(即在第一次使用的时候加载)。
建议使用第二种,直接就好。

第二种修改方法
这种方法可能不太管用,大家还可以使用
private Session getCurrentSession() {
Session session = this.sessionFactory.openSession();
return session;
}
改成openSession()就可以了

service实现类都要加上这个注解
@Transactional
@Service

然后applicationContext.xml文件中要修改成以下样子


image.png
<!-- 拦截器方式配配置事务 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="create*" propagation="REQUIRED"/>
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="import*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="updateRN*" propagation="REQUIRES_NEW" />
        <tx:method name="delete*" propagation="REQUIRED" no-rollback-for="NotImplementedException"/>
        <tx:method name="get*" read-only="true" propagation="REQUIRED" />
        <tx:method name="find*" read-only="true" propagation="REQUIRED" />
        <tx:method name="query*" read-only="true" propagation="REQUIRED" />
        <tx:method name="count*" read-only="true" propagation="REQUIRED" />
        <tx:method name="isNS*" read-only="true" propagation="NOT_SUPPORTED" />
        <tx:method name="*" read-only="true" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceMethods"
                  expression="execution(* com.tian.service.*.*(..))" />
    <aop:advisor advice-ref="txadvice" pointcut-ref="serviceMethods" />
</aop:config>

<!-- 注解方式配置事务 -->
 <tx:annotation-driven transaction-manager="transactionManager" />

还有pom.xml中要加入以下节点信息
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.0</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.14</version>
</dependency>
就完美解决了
postMan测试OK


image.png
原创文章 36 获赞 11 访问量 348

猜你喜欢

转载自blog.csdn.net/wutian842929/article/details/106138105
今日推荐