现在说下事务
spring提供俩种事务管理方式
1.annotation(注解)
2.xml配置(aop技术)
贴下配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="org.gjt.mm.mysql.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="5" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
</bean>
<!-- 1.注解方式配置事务 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<!-- 2.xml方式配置事务.利用aop技术
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.test.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
-->
<bean id="personService"
class="com.test.service.impl.PersonServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
我一般采用的是注解方式,因为比较简单一点.
配置文件里声明启用注解事务之后,看下面代码
package com.test.service.impl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.test.bean.Person;
import com.test.service.PersonService;
@Transactional
public class PersonServiceImpl implements PersonService {
private SimpleJdbcTemplate simpleJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
// 两种例外
// 1.unchecked 默认回滚事务(RuntimeException)
// 2.checked 默认不回滚事务(Exception)
// 开启事务.并设置unchecked例外不回滚
@Transactional(propagation = Propagation.REQUIRED, noRollbackFor = RuntimeException.class)
public void delete(int id) {
simpleJdbcTemplate.update("delete from person where id=?",
new Object[] { id });
throw new RuntimeException("运行期例外");
}
/**
* 说下事务的传播属性
* REQUIRED (默认) 业务方法需要在事务中运行,如果方法运行的时候已经开启事务,就加入事务,否则创建新的事务
* NOT_SUPPORTED 声明方法不需要开启事务.如果已经开启事务后调用该方法.则先挂起该事务,方法执行完毕后,事务恢复
* REQUIRES_NEW 不管是否存在事务,都另行开启新的事务.如果先前已经开启事务,挂起原先事务.创建新的事务.
* 新的事务执行结束后,恢复原先事务
* MANDATORY 只能在已经存在的事务中执行,不能自行创建事务,如果在没有开启事务的环境下调用,容器抛出例外
* SUPPORTS 墙头草,有事务就在事务中执行.没事务就在没事务的环境下执行
* NEVER 绝对不能在事务中执行.如果执行.抛出例外
* NESTED 如果有事务存在,开启一个新事务嵌套在上层事务中.开启的新事务具有自己独立的回滚点.新事务的回滚不会对外部事务造成影响,
* 但是外部事务的提交会直接提交嵌套事务
* 如果没有事务存在,则按照REQUIRED属性执行,该属性只对DataSourceTransationManager事务管理器有效
*/
@Transactional(propagation = Propagation.NOT_SUPPORTED)
// 不开启事务
public Person getPerson(int personId) {
return simpleJdbcTemplate.queryForObject(
"select * from person where id=?",
ParameterizedBeanPropertyRowMapper.newInstance(Person.class),
personId);
}
@Transactional(propagation = Propagation.NEVER)
// 不能在事务环境下执行
@SuppressWarnings("unchecked")
public List<Person> getPersons() {
String sql = "select * from person";
List persons = simpleJdbcTemplate.getJdbcOperations().query(sql,
new BeanPropertyRowMapper(Person.class));
return persons;
}
@Transactional(propagation = Propagation.NEVER)
public void save(Person person) {
simpleJdbcTemplate.getJdbcOperations().update(
"insert into person(name) values(?)",
new Object[] { person.getName() },
new int[] { java.sql.Types.VARCHAR });
}
public void update(Person person) {
simpleJdbcTemplate.getJdbcOperations().update(
"update person set name=? where id=?",
new Object[] { person.getName(), person.getId() },
new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER });
}
}
大体就是这样.如果想仔细研究请自行学习..
附件还是下篇发吧..下篇介绍spring整合struts1.x
分享到:
相关推荐
spring事务与数据库操作
Spring事务管理Demo
Spring事务流程图时序图Spring事务流程图时序图Spring事务流程图时序图Spring事务流程图时序图
Spring事务操作示例(四种方式),包含完整代码和数据库文件(基于MySQL,在项目sql文件夹中),可运行,学习Spring事务详见博客:http://blog.csdn.net/daijin888888/article/details/51822257
Spring事务原理、Spring事务配置的五种方式
spring事务配置详解 spring事务配置详解
spring 事务spring 事务spring 事务spring 事务spring 事务
spring 事务传播 demo
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
spring事务源码解析
spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读 spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
此ppt中前半部分通过spring事务的60道题的测试,摸底对事务的掌握情况,后半部分,对spring中的事务属性(传播行为、隔离级别、回滚规则、事务超时、是否只读)进行说明
本文档介绍了spring事务隔离级别的使用及详解
包含spring事务管理案例的项目源码和说明文档
Synchronized锁在Spring事务管理下,导致线程不安全。
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务...
Spring事务管理中所需要的jar包是 spring-tx-3.2.0.RELEASE.jar
最详细的Spring 事务讲解 让你彻底明白