spring事务

来源:互联网 发布:王陆807使用方法知乎 编辑:程序博客网 时间:2024/06/06 08:24
事务的四个特性ACID
原子性(atomic)——事务由一个或多个行为捆绑在一起组成,就好像是一个单独的工作单元。原子性确保在事务中的所有操作要么全部发生,要不全不发生。
一致性(Consistent)——一旦一个事务结束了(不管成功与否),系统所处的状态和它的业务规则是一致的。
隔离性(Isolated)——事务应该允许许多用户操作同一个数据,一名用户的操作不会和其他用户的操作相混淆。因此事务必须是互相隔离的。
持久性(Durable)——一旦事务完成,事务的结果应该持久化。

spring事务的两种方式
编程式事务管理和声明式事务管理

一声明式事务
新建普通的java项目,并加入如下的jar

主要的配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  9. <!-- 定义扫描包 -->
  10. <context:component-scan base-package="com.rookie"></context:component-scan>
  11. <!-- 定义外部资源文件 -->
  12. <context:property-placeholder location="classpath:db.properties"/>
  13. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  14. <property name="driverClass" value="${jdbc.driver}" />
  15. <property name="jdbcUrl" value="${jdbc.url}" />
  16. <property name="user" value="${jdbc.username}" />
  17. <property name="password" value="${jdbc.password}" />
  18. </bean>
  19. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  20. <property name="dataSource" ref="dataSource"></property>
  21. </bean>
  22. <!-- 定义事务管理器 -->
  23. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  24. <property name="dataSource" ref="dataSource"></property>
  25. </bean>
  26. <!-- 添加注解事务管理 -->
  27. <tx:annotation-driven transaction-manager="transactionManager"/>
  28. </beans>
二编程式事务
新建普通的java项目,加入如下的jar

 主要的配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  11. <context:component-scan base-package="com.rookie.tx.xml"></context:component-scan>
  12. <context:property-placeholder location="classpath:db.properties"/>
  13. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  14. <property name="driverClass" value="${jdbc.driver}" />
  15. <property name="jdbcUrl" value="${jdbc.url}" />
  16. <property name="user" value="${jdbc.username}" />
  17. <property name="password" value="${jdbc.password}" />
  18. </bean>
  19. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  20. <property name="dataSource" ref="dataSource"></property>
  21. </bean>
  22. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  23. <property name="dataSource" ref="dataSource"></property>
  24. </bean>
  25. <!-- 事务的通知 -->
  26. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  27. <tx:attributes>
  28. <tx:method name="purchase"/>
  29. <tx:method name="*"/>
  30. </tx:attributes>
  31. </tx:advice>
  32. <!-- 事务切面 -->
  33. <aop:config>
  34. <aop:pointcut expression="execution(* com.rookie.tx.xml.BookShopService.*(..))" id="txPointcut"/>
  35. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  36. </aop:config>
  37. </beans>
0 0
原创粉丝点击