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

本文深入探讨了Spring声明式事务处理的实现机制,通过配置文件自动管理事务,确保数据库操作的一致性和可靠性。详细解释了如何配置数据源、引入目标类与切面,以及事务属性的设置,同时提供了Java代码示例。
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();
     }
}
============================


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值