zoukankan      html  css  js  c++  java
  • Spring学习5_通过XML进行事务配置

      Spring的AOP的一个重要应用就是用来进行事务管控,本例通过结合Hibernate的事务管控,用一个简单Demo来模拟其实现:

    具体如下:

     1、Spring配置

    <?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:aop="http://www.springframework.org/schema/aop"
        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/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
             http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
       
       
       <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/hibernate"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
       </bean>  
        
       <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource"/>
            <property name="mappingResources">
            <list>
                <value>student.hbm.xml</value>
            </list>
            </property>
            <property name="hibernateProperties">
                <value>
                  hibernate.dialect=org.hibernate.dialect.MySQLDialect
                  hibernate.show_sql=true
                </value>
            </property>
       </bean>
             
       <bean id = "dao" class="com.dao.UserDaoImpl"> 
    <!--            <property name="sessionFactory" ref="mySessionFactory"></property> -->
           <property name="txManager" ref="txManager"></property>
       </bean>
       
       <bean id="userService" class="com.service.UserService">
                <property name="dao" ref="dao"></property>
       </bean> 
      
       <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="mySessionFactory"></property>
        </bean>
        
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <!-- other methods use the default transaction settings (see below) -->
                <tx:method name="*" rollback-for="Throwable" />
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <aop:pointcut id="userServiceOperation" expression="execution(* com.service..*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="userServiceOperation"/>
        </aop:config>
    </beans>

    2、Dao层实现如下

    package com.dao;
    
    import org.hibernate.Session;
    import org.springframework.orm.hibernate4.HibernateTransactionManager;
    
    import com.vo.Student;
    
    public class UserDaoImpl implements UserDao {
        private HibernateTransactionManager txManager;
    
        @Override
        public void save(Student student) {
            Session session = txManager.getSessionFactory().getCurrentSession();
            session.save(student);
            System.out.println("excute save;");
        }
    
        public void setTxManager(HibernateTransactionManager txManager) {
            this.txManager = txManager;
        }
    
        @Override
        public void delete() {
            System.out.println("excute delete;");
    
        }
    
    }

    3、Service层

    package com.service;
    
    import com.dao.UserDao;
    import com.vo.Student;
    
    public class UserService {
        
        private UserDao dao;
    
        public void setDao(UserDao dao) {
            this.dao = dao;
        }
        
        public void save() throws Exception{
            Student student = new Student(3, "test3", 30);
            dao.save(student);
            Student student2 = new Student(4, "test4", 30);
            dao.save(student2);
            //throw new Exception();
        }
    
    }

     测试1:当上述Service中异常被注释掉时,执行后数据库正常插入

                

     测试2:将数据库中数据清除,同时将Service中注释代码去掉,执行后抛出异常,数据回滚,插入失败,如下:

                

      事务管控生效。

  • 相关阅读:
    mouse_event模拟鼠标滚轮
    润乾报表配置技术路线
    建筑 物件 开心背单词 读句子,单词,字母,看图例, 翻译,看动画
    文字过渡动画,曲线过渡动画,,使用这个插件assign shape keys
    运动锻炼 开心背单词 读句子,单词,字母,看图例, 翻译,看动画,学英语,轻松背单词,简单背单词
    blender293 内置插件 精度绘画控件,PDT学习003,pdt tangents 切线
    日常用品 背单词 读句子 看图片 读单词 读字母 翻译, 看动画 学英语
    blender293 内置插件 精度绘画控件,PDT学习 precision drawing tools
    乔布斯 背单词 02 读句子 单词 字母 翻译,看动画 学英语 名言 我菜顾我在,我菜故我在,blender加python
    狐狸 和 乌鸦 英语 朗读句子 背单词
  • 原文地址:https://www.cnblogs.com/toDjlPersonnalBlog/p/4653111.html
Copyright © 2011-2022 走看看