Could not obtain transaction-synchronized Session for current thread Shiro 引起的事務(wù)報錯問題...

錯誤信息為 Could not obtain transaction-synchronized Session for current thread

我是從google搜索,百度搜索,才都試出來的,希望大家不要入坑啊,我剛爬出來,下班回家啊。

以下是解決方案:
按照這些方法,總有一款適合你的,童鞋們
第一種方法
Shiro 引起的事務(wù)報錯問題
解決辦法:
在MyRealm中做如下修改:(兩種方式,任意一種即可 藥到病除。)
在Shiro框架中注入Bean時,不使用@Autowire,使用ApplicationContextRegister.getBean()方法,手動注入bean。保證該方法只有在程序完全啟動運行時,才被注入。
使用 @Autowire + @Lazy 注解,設(shè)置注入到Shiro框架的Bean延時加載(即在第一次使用的時候加載)。
建議使用第二種,直接就好。

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

service實現(xiàn)類都要加上這個注解
@Transactional
@Service

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


image.png
<!-- 攔截器方式配配置事務(wù) -->
<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>

<!-- 注解方式配置事務(wù) -->
 <tx:annotation-driven transaction-manager="transactionManager" />

還有pom.xml中要加入以下節(jié)點信息
<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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容