錯誤信息為 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文件中要修改成以下樣子

<!-- 攔截器方式配配置事務(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
