SSH系列教材 (六)- 使用注解方式配置事务管理

步骤1:修改applicationContext.xml
步骤2:为ProductServiceImpl 添加注解
步骤3:测试
步骤4:MYSQL 表的类型必须是INNODB才支持事务

步骤 1 : 修改applicationContext.xml

在SSH 通过XML 配置事务管理 的基础上,修改applicationContext.xml文件.
1. 去掉<tx:advice和 <aop:config 配置
2. 保留<bean id="transactionManager" ...>
3. 添加<tx:annotation-driven transaction-manager="transactionManager"/> 用于进行注解扫描

<?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"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

   http://www.springframework.org/schema/beans 

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

   http://www.springframework.org/schema/aop 

   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

   http://www.springframework.org/schema/tx 

   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/context      

   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  

    <bean name="productActionBean" class="com.how2java.action.ProductAction">

        <property name="productService" ref="productServiceImpl" />

    </bean>

     

    <bean name="productServiceImpl" class="com.how2java.service.impl.ProductServiceImpl">

        <property name="productDAO" ref="productDAOImpl" />

    </bean>

    <bean name="productDAOImpl" class="com.how2java.dao.impl.ProductDAOImpl">

        <property name="sessionFactory" ref="sf" />

    </bean>

    <bean name="sf"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource" ref="ds" />

        <property name="mappingResources">

            <list>

                <value>com/how2java/pojo/Product.hbm.xml</value>

            </list>

        </property>

  

        <property name="schemaUpdate">  

            <value>true</value>  

        </property>  

                 

        <property name="hibernateProperties">

            <value>

                hibernate.dialect=org.hibernate.dialect.MySQLDialect

                hibernate.show_sql=true

                hbm2ddl.auto=update

            </value>

        </property>

    </bean>    

         

    <bean name="ds"

        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>

        <property name="username" value="root" />

        <property name="password" value="admin" />

    </bean>

     

    <!-- 配置事务管理器(声明式的事务) --> 

    <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">  

        <property name="sessionFactory" ref="sf"></property>  

    </bean>  

    <tx:annotation-driven transaction-manager="transactionManager"/>  

</beans>

步骤 2 : 为ProductServiceImpl 添加注解

在ProductServiceImpl 的list方法前加一句:

@Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception")


表示这个方法要进行事务管理。
没了~

package com.how2java.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

import com.how2java.dao.ProductDAO;

import com.how2java.pojo.Product;

import com.how2java.service.ProductService;

public class ProductServiceImpl implements ProductService {

    ProductDAO productDAO;

     

    @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception")

     

    public List<Product> list() {

        List<Product> products= productDAO.list();

        if(products.isEmpty()){

            for (int i = 0; i < 5; i++) {

                if(i==2)

                    throw new RuntimeException();

                Product p = new Product();

                p.setName("product " + i);

                productDAO.add(p);

                products.add(p);

                 

            }

        }

        return products;

    }

    public ProductDAO getProductDAO() {

        return productDAO;

    }

    public void setProductDAO(ProductDAO productDAO) {

        this.productDAO = productDAO;

    }

}

步骤 3 : 测试

删除product_表中数据,重启tomcat,访问页面:

http://127.0.0.1:8080/ssh/listProduct


即可观察到不断地抛出异常,不会插入数据。

测试

步骤 4 : MYSQL 表的类型必须是INNODB才支持事务

在Mysql中,只有当表的类型是INNODB的时候,才支持事务,所以需要把表的类型设置为INNODB,否则无法观察到事务.

修改表的类型为INNODB的SQL:

alter table product_ ENGINE  = innodb;



查看表的类型的SQL

show table status from how2java; 



不过有个前提,就是当前的MYSQL服务器本身要支持INNODB,如果不支持,请看 开启MYSQL INNODB的办法


更多内容,点击了解: https://how2j.cn/k/ssh/ssh-transaction-annotation/1077.html

原文地址:https://www.cnblogs.com/Lanht/p/12789348.html