spring事务
来源:互联网 发布:王陆807使用方法知乎 编辑:程序博客网 时间:2024/06/06 08:24
事务的四个特性ACID
原子性(atomic)——事务由一个或多个行为捆绑在一起组成,就好像是一个单独的工作单元。原子性确保在事务中的所有操作要么全部发生,要不全不发生。
一致性(Consistent)——一旦一个事务结束了(不管成功与否),系统所处的状态和它的业务规则是一致的。
隔离性(Isolated)——事务应该允许许多用户操作同一个数据,一名用户的操作不会和其他用户的操作相混淆。因此事务必须是互相隔离的。
持久性(Durable)——一旦事务完成,事务的结果应该持久化。
spring事务的两种方式
编程式事务管理和声明式事务管理
一声明式事务
新建普通的java项目,并加入如下的jar
主要的配置文件
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 定义扫描包 -->
<context:component-scan base-package="com.rookie"></context:component-scan>
<!-- 定义外部资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 添加注解事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
二编程式事务
新建普通的java项目,加入如下的jar
主要的配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.rookie.tx.xml"></context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="purchase"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 事务切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.rookie.tx.xml.BookShopService.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
0 0
- 事务:spring事务支持
- 事务:spring事务支持
- spring 事务,事务特性
- Spring事务
- Spring 事务
- Spring事务
- spring事务
- Spring事务
- spring 事务
- spring 事务
- spring事务
- Spring 事务
- spring事务
- Spring 事务
- spring 事务
- Spring事务
- Spring 事务
- spring事务
- Structs2的拦截器与过滤器区别
- 记录下这一时刻
- 欢迎使用CSDN-markdown编辑器
- 玩转安卓 Android系统文件夹结构解析(绝对有用)
- 重定向和转发的区别比较
- spring事务
- Mybatis3.4.0不支持mybatis-spring1.2.5及以下版本
- 17 - 01 - 22 计算机网络(12)(国际控制消息(报文)协议)
- obs+nginx搭建流媒体
- 归并排序
- 软导涉及英文缩写
- java.lang.NoClassDefFoundError: org/apache/ibatis/cursor/Cursor
- 持续集成环境选择:Jenkins VS gitlab-ci
- 王学岗单例模式的优化————双重检查、枚举、内部类、容器管理