使用ssh出现 Could not obtain transaction-synchronized Session for current thre情况

来源:互联网 发布:跑步软件修改器 编辑:程序博客网 时间:2024/06/07 22:24
这个调试是基于Hibernate4 的getSessionFactory().getCurrentSession()那个Hibernate3的HibernateTemplate().get(entityClazz, id);也适合,如果出现下面的bug

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thre

是因为我们未对Service层加上事务管理

我们应该在applicationContext.xml中配置

<!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 并注入SessionFactory的引用 --><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"p:sessionFactory-ref="sessionFactory" />


注意

<!-- 可有可无 --><tx:annotation-driven transaction-manager="transactionManager" /><!-- 配置事务增强处理Bean,指定事务管理器 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 用于配置详细的事务语义 --><tx:attributes><!-- 所有以'get'开头的方法是read-only的 --><tx:method name="get*" read-only="true" /><!-- 其他方法使用默认的事务设置 --><tx:method name="*" /></tx:attributes></tx:advice><aop:config><!-- 配置一个切入点,匹配empManager和mgrManager 两个Bean的所有方法的执行 --><aop:pointcut id="leePointcut"expression="bean(customerDoDishes) or bean(customerDoMessage) or bean(customerDoNotice) or bean(customerDoOrder) or bean(customerDoOwn) or bean(commonDoDishes)" /><!-- 指定在leePointcut切入点应用txAdvice事务增强处理 --><aop:advisor advice-ref="txAdvice" pointcut-ref="leePointcut" /></aop:config>
<

0 0
原创粉丝点击