ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--用注解配置事务,就不需要配置下面的 事务增强 和 配置切面, java方法用@Transactional注解-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--事务增强,采取什么样的事务策略-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--所有find开头的方法,timeout:设置事务超时时间,-1就是不超时,秒为单位;read-only="true":事务为只读,查询才设置,提高查询性能,默认为false-->
<tx:method name="set*" timeout="1000" propagation="SUPPORTS" read-only="true"/>
<!--其它没想到的方法用*表示,propagation="REQUIRED" 表示有事务就用当前事务,没有就自动创建一个事务-->
<!--propagation="SUPPORTS" 表示 有事务就用当前事务,没事务就不用事务,一般用于查询语句-->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置切面,就是对那一些方法增强-->
<aop:config>
<!--execution(* com.big3.service.impl.*.*(..)) 第一个*表示方法返回类型-->
<aop:pointcut id="myPoint" expression="execution(* cn.kgc.service..*.*(..))"/>
<!--advice-ref: 增强引用 pointcut-ref:切入点引用 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint"/>
</aop:config>
</beans>
java代码
@Transactional(timeout = 10,readOnly = false) //事务控制,可以写在类前面
public int setDetail(Bbs_detail bdsl) {
int countOne=detailDao.setDetail(bdsl);
//int i=1/0;
int countTwo=detailDao.setDetail(bdsl);
if(countOne>0 && countTwo>0) {
return 1;
}else {
return 0;
}
}
事务配置
最新推荐文章于 2022-11-02 14:35:33 发布