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中注释代码去掉,执行后抛出异常,数据回滚,插入失败,如下:

            

  事务管控生效。

转载于:https://www.cnblogs.com/toDjlPersonnalBlog/p/4653111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值