Mybatis事务管理-jdbc事务

一、mybatis.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="true">

	<!-- 配置数据源 -->
	<bean id="dataSourceMybatisMysql" class="org.logicalcobwebs.proxool.ProxoolDataSource">
		<property name="driver">
			<value>${driver.mysql}</value>
		</property>
		<property name="driverUrl">
			<value>${url.mysql}</value>
		</property>
		<property name="user" value="${user.mysql}" />
		<property name="password" value="${pwd.mysql}" />
		<property name="alias" value="dataSourceMybatisMysqlAlis" />
		<property name="prototypeCount" value="0" />
		<property name="maximumConnectionCount" value="1000" />
		<property name="minimumConnectionCount" value="10" />
		<property name="simultaneousBuildThrottle" value="1000" />
		<property name="houseKeepingTestSql" value="select CURRENT_DATE" />
	</bean>

	<!-- 配置会话工厂 -->
	<bean id="sessionFactoryMybatis" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSourceMybatisMysql" />
		<property name="typeAliasesPackage" value="cn.*.mybatis.domain" />
		<!-- 分页 -->
		<!-- 自定义分页处理 -->
		<property name="plugins">
			<list>
				<bean class="cn.hard.common.mybatis.plugin.OffsetLimitInterceptor">
					<property name="dialectClass" value="cn.hard.common.jdbc.dialect.MysqlDialect" />
				</bean>
			</list>
		</property>
	</bean>
	
	<!-- 扫描DAO/Mapper -->
	<bean id="mapperScan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn..*.mybatis.mypersist" />
	</bean>
	
	<!-- 事务管理 -->
	<bean id="transactionManagerJdbc" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSourceMybatisMysql" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManagerJdbc" />
</beans>

注:如果有其他数据连接池使用DataSourceTrasactionManager来处理事务,则事务处理不会成功(如下图)。因此,这种方式只能一个数据连接池使用。(不知道怎么回事,如果有知道的请给我留个言,不胜感激。)

jdbc.xml:同一个项目同时存在两个事务管理,且都是引用DataSourceTransactionManager,则事务管理不会成功。可注释其中一个事务管理。

....

<bean id="jdbcTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource">
		<ref local="dataSourceJdbcMysql" />
	</property>
</bean>
<tx:annotation-driven transaction-manager="jdbcTxManager" />

....

<tx:annotation-driven>一共有四个属性如下:

mode:指定Spring事务管理框架创建通知bean的方式。可用的值有proxy和aspectj。前者是默认值,表示通知对象是个JDK代理;后者表示Spring AOP会使用AspectJ创建代理。

proxy-target-class:如果为true,Spring将创建子类来代理业务类;如果为false,则使用基于接口的代理。(如果使用子类代理,需要在类路径中添加CGLib.jar类库)。

order:如果业务类除事务切面外,还需要织入其他的切面,通过该属性可以控制事务切面在目标连接点的织入顺序。

transaction-manager:指定到现有的PlatformTransaction Manager bean的引用,通知会使用该引用。

二、DAO/Mapper:略。(我们的事务管理放在service接口处)

三、事务管理的接口类及方法:

接口类:

public interface IQuestionService {
	
	@Transactional(rollbackFor = Exception.class,isolation=Isolation.SERIALIZABLE)
	public abstract void saveOrUpdate() throws Exception;
	
}

说明:1、rollbackFor:回滚条件;2、isolation:事务隔离级别;3、SERIALIZABLE:禁止脏读、不可重复读和幻读。

实现类:

@Service("questionService")
public class QuestionServiceImpl implements IQuestionService {
	
	@Override
	public void saveOrUpdate() throws Exception{
		
		//执行第一个sql,代码略
		boolean result1=true;
		if(!result1){
			//抛出异常,则将不再执行第二个sql
			throw new Exception("操作失败!");
		}
		
		//执行第二个sql,代码略
		boolean result2=true;
		if(!result2){
			//抛出异常,则第一个sql将回滚
			throw new Exception("操作失败!");
		}
	}
}


*************以下内容转自http://my.oschina.net/guanzhenxing/blog/214228***********

四、@Transactional的属性

属性名

类型

说明

isolation

枚举org.springframework.transaction.annotation.Isolation的值

事务隔离级别

noRollbackFor

Class<? extends Throwable>[]

一组异常类,遇到时不回滚。默认为{}

noRollbackForClassName

Stirng[]

一组异常类名,遇到时不回滚,默认为{}

propagation

枚举org.springframework.transaction.annotation.Propagation的值

事务传播行为

readOnly

boolean

事务读写性

rollbackFor

Class<? extends Throwable>[]

一组异常类,遇到时回滚

rollbackForClassName

Stirng[]

一组异常类名,遇到时回滚

timeout

int

超时时间,以秒为单位

value

String

可选的限定描述符,指定使用的事务管理器

五、@Transactional标注的位置:

1、@Transactional注解可以标注在类和方法上,也可以标注在定义的接口和接口方法上。

2、如果我们在接口上标注@Transactional注解,会留下这样的隐患:因为注解不能被继承,所以业务接口中标注的@Transactional注解不会被业务实现类继承。所以可能会出现不启动事务的情况。所以,Spring建议我们将@Transaction注解在实现类上。

3、在方法上的@Transactional注解会覆盖掉类上的@Transactional。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值