【Spring七】JDBC编程之声明式事务处理

8 篇文章 0 订阅
Spring声明式事务处理:通过书写配置文件,Spring帮我们处理事务!
由于使用不同的数据库操作技术,开启事务的方式不一样,但是核心都是从数据源获取链接,然后开启事务。
1、spring处理事务的session和目标方法进行数据库操作用到的session必须保持一致
2、spring中事务和session是绑定在一起,因为session是由当前线程产生的

1.配置文件:
< 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"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  >
      <!-- 读取配置文件  -->
      < bean
            class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
            <property name= "locations">
                <value> classpath:jdbc.properties</value >
            </property>
      </ bean  >
      <!-- 根据配置文件,获取数据源  -->
      < bean  id="dataSource" destroy-method="close"
            class= "org.apache.commons.dbcp.BasicDataSource" >
            <property name= "driverClassName" value="${jdbc.driverClassName}" />
            <property name= "url" value ="${jdbc.url}" />
            <property name= "username" value="${jdbc.username}" />
            <property name= "password" value="${jdbc.password}" />
      </ bean  >
          
      <!--
          1、引入目标类
          2、引入切面
      -->
       <bean id="classesDao" class="cn.itheima03.spring.jdbc.transaction.ClassesDaoImpl" >
            <property name= "dataSource">
                 <ref bean= "dataSource"/>
            </property>
       </bean >
     
       <bean id="classesService" class="cn.itheima03.spring.jdbc.transaction.ClassesServiceImpl" >
            <property name= "classesDao">
                 <ref bean= "classesDao"/>
            </property>
       </bean >
     
       <!--声明一个事务管理器,需要传入数据源,传入的数据源与目标类里的数据源是同一个。  -->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
            <property name= "dataSource">
                 <ref bean= "dataSource"/>
            </property>
       </bean >
       <!--
          通知
             id 是唯一标示
             transaction-manager  事务管理器
       -->
       <tx:advice id="tx" transaction-manager="transactionManager" >
            <tx:attributes>
                 <!--
                   propagation 事务的传播属性  默认值REQUIRED
                   isolation  默认值
                   read-only
                      true  只读事务
                      false 读写事务
                -->
                 <tx:method name= "save*" read-only ="false"/>
                 <tx:method name= "update*" read-only="false" />
                 <!--
                   除了上述情况以外的 qingkuan
                -->
                 <tx:method name= "*" read-only ="true"/>
            </tx:attributes>
       </tx:advice >
     
       <aop:config >
            <aop:pointcut expression="execution(* cn.itheima03.spring.jdbc.transaction.ClassesServiceImpl.*(..))" id ="perform"/>
            <aop:advisor advice-ref="tx" pointcut-ref="perform" />
       </aop:config >
</ beans >


2.java代码:
public  class ClassesDaoImpl extends JdbcDaoSupport implements ClassesDao{

      @Override
      public  void saveClasses() {
            this.getJdbcTemplate().execute("insert into classes(cname,description) values('aq','as')");
     }
}
============================
public  class ClassesServiceImpl implements ClassesService{
      private  ClassesDao classesDao;

      public  ClassesDao getClassesDao() {
            return classesDao ;
     }

      public  void setClassesDao(ClassesDao classesDao) {
            this.classesDao = classesDao;
     }

      @Override
      public  void saveClasses() {
            // TODO Auto-generated method stub
            this.classesDao .saveClasses();
            int a = 1/0;
            this.classesDao .saveClasses();
     }
}
============================
public  class ClassesDaoTest {
      @Test
      public  void testSaveClasses(){
          ApplicationContext context =   new ClassPathXmlApplicationContext("cn/itheima03/spring/jdbc/transaction/applicationContext.xml" );
          ClassesService classesService = (ClassesService)context.getBean( "classesService"  );
          classesService.saveClasses();
     }
}
============================


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring声明式事务是一种在Spring框架中实现事务管理的方式,它基于AOP(面向切面编程)的思想,通过将事务管理的逻辑从业务代码中分离出来,使得业务代码不需要关注事务的处理,降低了业务代码的复杂度,并且提高了事务的可控性和可复用性。 Spring声明式事务的核心是TransactionInterceptor拦截器,它是一个AOP拦截器,用于拦截带有@Transactional注解的方法,并在方法执行前后自动开启和提交事务。在Spring中,可以通过XML配置或注解的方式来声明事务,例如: XML配置: <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.example.service.*.*(..))"/> </aop:config> 注解方式: @Configuration @EnableTransactionManagement public class AppConfig { @Bean public DataSource dataSource() { // create and return a DataSource } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } } @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override @Transactional(propagation = Propagation.REQUIRED) public void saveUser(User user) { userDao.save(user); } } 在上述示例中,我们可以看到声明式事务的两种方式:XML配置和注解方式。其中,<tx:advice>元素用于配置事务通知,<aop:advisor>元素用于配置切入点和通知之间的关联;@EnableTransactionManagement注解用于启用声明式事务,@Transactional注解用于标记需要进行事务管理的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值