Spring事务
2015-08-31 17:18
721 查看
一、Spring的xml头配置需要增加tx命名空间
二、配置注解事务
1、XML配置
其中<tx:annotation-driven transaction-manager="transactionManager" />告诉Spring检查上下文中所有的Bean并查找使用@Transactional注解的Bean,
不管是方法级别的还是类级别的。需要将数据源配置在这个标签中。
在方法上标注@Transactional就会在方法前后加事务,类上的话会在类的所有方法上前后加上事务。
1)Spring的xml配置(适用于JDBC、Mybatis)
2)Spring的xml配置(适用于Hibernate)
2、代码中注解配置
在需要的方法或者类上表注上@Transactional
类上标注说明这个类的方法都是支持事务的,方法上加的话说明这个方法是支持事务的。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 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.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName">
二、配置注解事务
1、XML配置
其中<tx:annotation-driven transaction-manager="transactionManager" />告诉Spring检查上下文中所有的Bean并查找使用@Transactional注解的Bean,
不管是方法级别的还是类级别的。需要将数据源配置在这个标签中。
在方法上标注@Transactional就会在方法前后加事务,类上的话会在类的所有方法上前后加上事务。
1)Spring的xml配置(适用于JDBC、Mybatis)
<!-- 注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dynamicDataSource"/> </bean>
2)Spring的xml配置(适用于Hibernate)
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/>
2、代码中注解配置
在需要的方法或者类上表注上@Transactional
类上标注说明这个类的方法都是支持事务的,方法上加的话说明这个方法是支持事务的。
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- SQL Server误区30日谈 第1天 正在运行的事务在服务器故障转移后继续执行
- 浅析SQL Server中包含事务的存储过程
- Mysql中的事务是什么如何使用
- MySql的事务使用与示例详解
- C#分布式事务的超时处理实例分析
- SQL Server的事务操作隔离模式介绍
- oracle 合并查询 事务 sql函数小知识学习
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- sql不常用函数总结以及事务,增加,删除触发器
- struts2 spring整合fieldError问题
- mysql的XA事务恢复过程详解
- 在Mysql存储过程中使用事务实例
- mysql存储过程事务管理简析
- php+mysql事务rollback&commit示例
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)