Spring(5): 事务管理

来源:互联网 发布:sai绘图软件手机版 编辑:程序博客网 时间:2024/06/07 07:45

1.事务的概念

一个数据库事务是一个被视为单一的工作单元的操作序列。这些操作应该要么完整地执行,要么完全不执行。

事务的4个特性:

  1. 原子性(Atomicity):事务应该当作一个单独单元的操作,这意味着整个序列操作要么是成功,要么是失败的。
  2. 一致性(Consistency):这表示数据库的引用完整性的一致性,表中唯一的主键等。
  3. 隔离性(Isolation):可能同时处理很多有相同的数据集的事务,每个事务应该与其他事务隔离,以防止数据损坏。
  4. 持久性(Durablity):一个事务一旦完成全部操作后,这个事务的结果必须是永久性的,不能因系统故障而从数据库中删除。

2.编程式的事务管理(不推荐使用)

TransactionTemplate 模板类:
org.springframework.transaction.support.TransactionTemplate

两个主要方法:

  • void setTransactionManager(PlatformTransactionManager transactionManager):设置事务管理器。
  • Object execute(TransactionCallback action):在 TransactionCallback 回调接口中定义需要以事务方式组织的数据访问逻辑。

代码:

@Servicepublic class ForumService1 {    public ForumDao forumDao;    public TransactionTemplate template;    @Autowired    public void setTemplate(TransactionTemplate template) {        this.template = template;    }    public void addForum(final Forum forum) {        template.execute(new TransactionCallbackWithoutResult() {            @Override            protected void doInTransactionWithoutResult(TransactionStatus status) {                forumDao.addForum(forum); //需要在事务环境中执行的代码            }        });    }}

3.声明式的事务管理

对代码的侵入性最小,让事务管理代码完全从业务代码中移除。

3.1 基于 TransactionProxyFactoryBean 的 XML 配置

代码:

<!-- 1.引入DAO和DataSource的配置文件 --><import resource="classpath:applicationContext-dao.xml" /><!-- 2.声明事务管理器 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     <property name="dataSource" ref="dataSource" /></bean><!-- 3.需要实施事务增强的目标业务Bean --><bean id="bbtForumTarget" class="com.chen.transaction.service.BbtForum"     p:forumDao-ref="forumDAO" /><bean id="bbtForum" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"     p:transactionManager-ref="txManager"     p:target-ref="bbtForumTarget"     <!-- 注入事务属性 -->     <property name="transactionAttributes">     <props>     <!-- prop的格式 : *PROPAGATION:事务的传播行为                      *ISOLATION:事务的隔离级别                      *readOnly:只读                      *-Exception:发生哪些异常回滚事务                      *+Exception:发生哪些异常事务不回滚 -->          <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>          <prop key="*">PROPAGATION_REQUIRED</prop>     </props></property></bean>

3.2 基于 aop/tx 命名空间的 XML 配置

代码:

<!-- 1.引入DAO和DataSource的配置文件 --><import resource="classpath:applicationContext-dao.xml" /><!-- 2.事务管理器 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     <property name="dataSource" ref="dataSource" /></bean><!-- 3.使用切点表达式定义目标方法 --><aop:config>     <aop:pointcut id="serviceMethod" expression="execution(* com.chen.transaction.*Forum.*(..))" />     <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" /></aop:config><tx:advice id="txAdvice" transaction-manager="txManager"     <tx:attributes>     <tx:method name="get*" read-only="false" />     <tx:method name="add*" rollback-for="Exception" />     <tx:method name="update*" /></tx:attributes></tx:advice>

3.3 基于注解的配置

@Transactional:业务类事务属性的配置。

代码:

@Service@Transactionalpublic class BbtForum {    public void getForum(int forumId) {        return forumDao.getForum(forumId);    }}

源代码:
https://github.com/leifchen/hello-spring
代码包:com.chen.transaction

原创粉丝点击