主题 : hibernate和spring整合事务怎么控制 |
级别: 状元
|
4# 发表于:2012-05-14 15:52:49 IP:59.140.*.*
<h2><span style="color: #0000ff;">第一种配置方法:基于xml的事务管理</span></h2>这种方法不需要对原有的业务做任何修改,通过在xml文件中定义需要拦截方法的匹配即可完成配置,要求是,业务处理中的方法的命名要有规律,比如setxxx,xxxupdate等等。详细配置如下:
Xml代码 1.<!-- 定义事务处理类,不同的数据访问方式,事务处理类不同 比如:hibernate操作的hibernatetransactionmanager,jdbc操作的使用datasourcetransactionmanager --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"></property> </bean> <!-- 定义事务通知 --> <tx:advice id="txadvice" transaction-manager="transactionmanager"> <!-- 定义方法的过滤规则 --> <tx:attributes> <!-- 所有方法都使用事务 --> <tx:method name="*" propagation="required"/> <!-- 定义所有get开头的方法都是只读的 --> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 定义aop配置 --> <aop:config> <!-- 定义一个切入点 --> <aop:pointcut expression="execution (* com.iflysse.school.services.impl.*.*(..))" id="services"/> <!-- 对切入点和事务的通知,进行适配 --> <aop:advisor advice-ref="txadvice" pointcut-ref="services"/> </aop:config> <!-- 定义事务处理类,不同的数据访问方式,事务处理类不同 比如:hibernate操作的hibernatetransactionmanager,jdbc操作的使用datasourcetransactionmanager --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"></property> </bean> <!-- 定义事务通知 --> <tx:advice id="txadvice" transaction-manager="transactionmanager"> <!-- 定义方法的过滤规则 --> <tx:attributes> <!-- 所有方法都使用事务 --> <tx:method name="*" propagation="required"/> <!-- 定义所有get开头的方法都是只读的 --> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 定义aop配置 --> <aop:config> <!-- 定义一个切入点 --> <aop:pointcut expression="execution (* com.iflysse.school.services.impl.*.*(..))" id="services"/> <!-- 对切入点和事务的通知,进行适配 --> <aop:advisor advice-ref="txadvice" pointcut-ref="services"/> </aop:config><h2><span style="color: #0000ff;"> |
||
---|---|---|---|
1
共1页