hibernate正确使用session

Could not obtain transaction-synchronized Session for current thread 错误的缘由spring


用事物不能用opensessionexpress

必须用getcurrentsessionsession

报错的缘由是没有配置事物的切面 ,或者扫描的包错了spa


<!-- 配置事务管理器 -->hibernate

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">orm

<property name="sessionFactory" ref="sessionFactory"></property>事务

</bean>get


<!-- 配置事务通知属性 -->it

<tx:advice id="txAdvice" transaction-manager="transactionManager">io

<!-- 定义事务传播属性 -->

<tx:attributes>

<tx:method name="save*" propagation="REQUIRED" /> <!-- 支持当前事务,若是执行到save开头的任何方法时没有事务则开启一个事务 这是最多见的方式 -->

<tx:method name="update*" propagation="REQUIRED" />

<tx:method name="add*" propagation="REQUIRED" />

<tx:method name="delete*" propagation="REQUIRED" />

<tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <!-- 支持当前事务,若是当前没有事务,就以非事务方式执行。只读 -->

<tx:method name="get*" propagation="SUPPORTS" read-only="true" />

<tx:method name="*"  propagation="SUPPORTS" read-only="true" />

</tx:attributes>

</tx:advice>


<!-- 配置事务切面 -->

<aop:config>

<aop:pointcut id="txMethod" expression="execution(* org.ym.demo.service.impl.*ServiceImpl.*(..))" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="txMethod" />

</aop:config>



这玩意会帮我买开启事物

相关文章
相关标签/搜索