SSH框架整合-使用註解方式配置事務管理
阿新 • • 發佈:2019-02-02
- 修改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>
- 為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; } }
- 測試
刪除product_表中資料,重啟tomcat,訪問頁面:
即可觀察到不斷地丟擲異常,不會插入資料。http://127.0.0.1:8080/ssh/listProduct
- MYSQL 表的型別必須是INNODB才支援事務
在Mysql中,只有當表的型別是INNODB的時候,才支援事務,所以需要把表的型別設定為INNODB,否則無法觀察到事務.
修改表的型別為INNODB的SQL:
查看錶的型別的SQLalter table product_ ENGINE = innodb;
不過有個前提,就是當前的MYSQL伺服器本身要支援INNODB,如果不支援,請看 開啟MYSQL INNODB的辦法show table status from how2java;