您的位置:首页 > 编程语言 > Java开发

Spring学习之声明式事物管理

2016-03-04 13:30 525 查看
public List<Student> selectStudent() {
Student s = new Student();
s.setName("zhengbin");
s.setScore(109);
sqlSession.insert("com.zhengbin.entity.studentMapper.addStudent",s);
sqlSession.delete("com.zhengbin.entity.studentMapper.delStudent",6);
return sqlSession.selectList("com.zhengbin.entity.studentMapper.getStudent");
}


  代码运行的前提是数据库不存在id为6的数据

  1.如果不加入事物管理,则运行的结果是,第五行成功执行,向数据库插入了新数据

  2.如果加入事物管理,则运行的结果是,虽然第五行可以执行成功,但第六行不能成功执行,则第五行的操作会回滚,不会将记录写入数据库

声明式事物管理在Spring配置文件beans.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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/student" />
<property name="username" value="root" />
<property name="password" value="950906" />
</bean>

<!-- 声明事物配置 开始 -->
<!-- 配置事物管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事物的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
<!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*insert*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*select*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="pointcut"
expression="execution(* com.zhengbin.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!-- 声明事物配置 结束 -->

<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.cfg.xml" />
</bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="studentDao" class="com.zhengbin.dao.StudentDaoImpl">
<property name="sqlSession" ref="sqlSessionTemplate"></property>
</bean>
</beans>


事务的几种传播特性
1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启
2. PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3. PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4. PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5. PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6. PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常
7. PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务,

补充:

  1.在mybatis-spring-1.2.3.jar下,声明与实现:

aopalliance.jar
asm-4.2.jar
aspectjrt.jar
aspectjweaver.jar
cglib-3.1.jar
commons-logging-1.2.jar
log4j-1.2.17.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mybatis-3.3.0.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.0.6-bin.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-instrument-4.1.6.RELEASE.jar
spring-instrument-tomcat-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-jms-4.1.6.RELEASE.jar
spring-messaging-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-oxm-4.1.6.RELEASE.jar
spring-test-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
spring-webmvc-portlet-4.1.6.RELEASE.jar
spring-websocket-4.1.6.RELEASE.jar


jarlist
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: