• Spring-Aop事物管理


    一些子模块组成一个事物,

    要么全部成功,要么全不成功

    事物四大原则:

    一致性

    隔离性

    原子性

    持久性

     

    张三给李四转账

     

     张三账户减100元 ,李四的账户多100

     

    建表:

    create  table  account(

        aid int primary key auto_increment,

        aname varchar(20),

        money double

    )

    insert  account values (null,'张三',10000),(null,'李四',10000)

     

    面试题

    四种隔离级别:

    https://www.cnblogs.com/ubuntu1/p/8999403.html

     

    七种传播方式:

    https://blog.csdn.net/weixin_41313504/article/details/78769535

     

    接口:

    public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

        @Override

        public void increateMoney(int id, double money) {

            String  sql="update account set money=money+? where aid=?";

            getJdbcTemplate().update(sql,money,id);

        }

     

        @Override

        public void decreateMoney(int id, double money) {

            String  sql="update account set money=money-? where aid=?";

            getJdbcTemplate().update(sql,money,id);

        }

    }

     

    Service:

    public class AccountServiceImpl implements AccountService {

        AccountDao dao;

     

        public AccountDao getDao() {

            return dao;

        }

     

        public void setDao(AccountDao dao) {

            this.dao = dao;

        }

     

        @Override

        public void trader(int from, int to, double money) {

            System.out.println("转账中............");

            //减钱

            dao.decreateMoney(from,money);

            System.out.println(1/0);

            //加钱

            dao.increateMoney(to,money);

     

            System.out.println("转账成功..............");

        }

    }

     

    Xml配置:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                      xmlns="http://www.springframework.org/schema/beans"

                      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-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/aop

                                                                   http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

                                                                   http://www.springframework.org/schema/tx

                                                                   http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

             <!--引入配置文件-->

             <context:property-placeholder location="classpath:c3p0.properties"/>

             <!--配置数据源-->

             <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">

                      <property name="driverClass" value="${c3p0.driverClass}"/>

                      <property name="jdbcUrl" value="${c3p0.jdbcUrl}"/>

                      <property name="user" value="${c3p0.user}"/>

                      <property name="password" value="${c3p0.password}"/>

             </bean>

     

             <bean id="acoountDao" class="com.ujiuye.dao.AccountDaoImpl">

                      <property name="dataSource" ref="ds"/>

             </bean>

             <!--配置service-->

             <bean id="accountService" class="com.ujiuye.service.AccountServiceImpl">

                      <property name="dao" ref="acoountDao"/>

             </bean>

     

     

             <!--配置spring 的事物数据源-->

             <bean id="tran" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

                      <property name="dataSource" ref="ds"/>

             </bean>

     

             <!--配置通知-->

             <tx:advice id="txAdvice" transaction-manager="tran">

                      <tx:attributes>

                               <!--isolation事物的隔离级别 READ_COMMITTED 读已提交,propagation="REQUIRED"事物的传播方式-->

     

                      <!--   REQUIRED&#45;&#45;支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

                               比如service 方法中调用另外的service的方法,如果此方法没有事物管理,重新创建一个事物

                      -->

                               <tx:method name="update*" isolation="READ_COMMITTED" propagation="REQUIRED"/>

                               <tx:method name="trade*" isolation="READ_COMMITTED" propagation="REQUIRED"/>

                               <tx:method name="save*" isolation="READ_COMMITTED" propagation="REQUIRED"/>

                      </tx:attributes>

             </tx:advice>

             <!--配置aop-->

             <aop:config>

                      <aop:pointcut id="ptt" expression="execution(* com.ujiuye.service.*ServiceImpl.*(..))"/>

                      <!--织入通知-->

                      <aop:advisor advice-ref="txAdvice" pointcut-ref="ptt"/>

             </aop:config>

     

    </beans>

     

    测试:

     @Test

        public  void test1(){

            ApplicationContext app=new ClassPathXmlApplicationContext("trans.xml");

            AccountService service= (AccountService) app.getBean("accountService");

           //为配置事物之前,

          //配置事物之后

            service.trader(1,2,100);

        }

     

    Spring事物注解的配置

    注解配置可以在方法上,也可以在类上

    加在类上表示类中所有的方法都参与事物的管理

    加在方法上代表此方法参与事物的管理

    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)

     

    xml

    <!--配置spring 的事物数据源-->

             <bean id="tran" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

                      <property name="dataSource" ref="ds"/>

             </bean>

             <!--注解扫描事物-->

             <tx:annotation-driven transaction-manager="tran"/>

     

  • 相关阅读:
    HarmonyOS(鸿蒙OS)发布,聊聊操作系统的调度
    HarmonyOS(鸿蒙OS)发布,聊聊操作系统的调度
    修改SQL Server Management Studio 默认设置从而提高开发效率
    修改SQL Server Management Studio 默认设置从而提高开发效率
    还原默认的 SQL Server Management Studio 配置
    SQL2008智能提示失效
    SQL2008智能提示失效
    1028:字符菱形
    1028:字符菱形
    1027:输出浮点数
  • 原文地址:https://www.cnblogs.com/masterhxh/p/12957669.html
Copyright © 2020-2023  润新知