• 声明式事务管理


         目前使用的比较多的一般有两种事务管理。一种是@Transactional,另一种是在配置文件中做相关的规则事务说明。

         这里呢,主要是讲@Transactional声明式事务管理。我直接从上次的applicationContext.xml文件中开始讲。如下:

    <?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:p="http://www.springframework.org/schema/p"  
            xmlns:aop="http://www.springframework.org/schema/aop"   
            xmlns:context="http://www.springframework.org/schema/context"  
            xmlns:jee="http://www.springframework.org/schema/jee"  
            xmlns:tx="http://www.springframework.org/schema/tx"  
            xsi:schemaLocation="    
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
        
        <!-- (本案例不用到,只是用了一个全盘扫描,以上内容只是为了让大家了解它) -->
        
        <context:component-scan base-package="news.."/>
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
    
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
            <!-- 每300秒检查所有连接池中的空闲连接 -->
            <property name="idleConnectionTestPeriod" value="300"/>
            <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
            <property name="maxIdleTime" value="900"/>
            <!-- 最大连接数 -->
            <property name="maxPoolSize" value="2"/>
        </bean>
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.connection.autocommit">false</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            <!-- <property name="packagesToScan">
                <list>
                    这里value值添实体类所在的包
                    <value>news.entity</value>
                </list>
            </property> -->
            <property name="mappingResources">
                <list>
                    <value>news/entity/News.hbm.xml</value>
                </list>
            </property>
        </bean>
        
        <bean id="transactionManager" 
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
             <!--  创建事务管理器, 管理sessionFactory(因为所有的session都是从sessionFactory获取的) -->
             <property name="sessionFactory" ref="sessionFactory" />
         </bean>
         <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

     然后是serviceImpl文件

    package news.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import news.dao.Newsdao;
    import news.entity.News;
    @Service
    @Scope("prototype")
    public class NewsServiceImpl implements NewsService {
        @Autowired
        private Newsdao nd;
        @Transactional(readOnly=true)
        public List<News> showAll(){
            List<News> listAllNews=nd.showAll();
            return listAllNews;
        }
        @Transactional(propagation=Propagation.REQUIRED)
        public String delSingleNews(Integer id){
            String deleteList=nd.delSingleNews(id);
            return deleteList;
        }
    }

     在我看来,声明式事物管理的使用要看是什么项目而定。

     比如说,大型的项目一般就会使用声明式事物管理。

     因为大型的项目的所定义的类定义的方法有很多,如果是用注解式会很容易出问题,比如遗漏了,忘记了,有一处改动,需要跟着修改的地方就很多。

     如果是用声明式事务管理只需要改配置文件中所对应的方法就可以了,其他的地方完全不用管。

    以上就是我对声明式事务管理的理解,如果有不对或者遗漏的地方,欢迎指出来。

  • 相关阅读:
    (转载)Linux进程基础
    C语言字符串
    DNS域名解析服务
    Linux-SSH远程管理
    Linux文件系统深入了解
    Linux进程和计划任务管理
    Linux账户与权限管理
    MySQL实现读写分离
    SQL数据库常用函数
    MySQL进阶查询(二)
  • 原文地址:https://www.cnblogs.com/t555523/p/5992809.html
Copyright © 2020-2023  润新知