美文网首页Java
Java Spring-声明式事务

Java Spring-声明式事务

作者: 一亩三分甜 | 来源:发表于2020-03-24 01:56 被阅读0次

    Spring-声明式事务

        <!-- 配置事务管理器 -->
        <bean id = "transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        >
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 启用事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
    @Service("bookShopService")
    public class BookShopServiceImpl implements BookShopService{
        @Autowired
        private BookShopDao bookShopDao;
        //添加事务注解
        @Transactional
        @Override
        public void purchase(String username, String isbn) {
            //1.获取书的单价
            int price = bookShopDao.findBookPriceByIsbn(isbn);
            //2.更新书的库存
            bookShopDao.updateBookStock(isbn);
            //3.更新用户的余额
            bookShopDao.updateUserAccount(username,price);
        }
    }
    
    public interface BookShopDao {
    
        //根据书号获取书的单价
        public int findBookPriceByIsbn(String isbn);
    
        //更新书的库存,使书号对应的库存-1
        public void updateBookStock(String isbn);
    
        //更新用户的账户余额:使username的balance - price
        public void updateUserAccount(String username,int price);
    }
    
    @Repository("bookShopDao")
    public class BookShopDaoImpl implements BookShopDao {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Override
        public int findBookPriceByIsbn(String isbn) {
            String sql = "select price from book where isbn = ?";
            return jdbcTemplate.queryForObject(sql,Integer.class,isbn);
        }
    
        @Override
        public void updateBookStock(String isbn) {
            //检查书的库存是否足够,若不够,则跑出异常
            String sql2 = "select stock from book_stock where isbn = ?";
            int stock = jdbcTemplate.queryForObject(sql2,Integer.class,isbn);
            if (stock == 0){
                throw new BookStockException("库存不足!");
            }
            String sql = "update book_stock set stock = stock - 1 where isbn = ?";
            jdbcTemplate.update(sql,isbn);
        }
    
        @Override
        public void updateUserAccount(String username, int price) {
            //验证余额是否足够,若不足,则抛出异常
            String sql2 = "select balance from account where username = ?";
            int balance = jdbcTemplate.queryForObject(sql2,Integer.class,username);
            if (balance < price){
                throw new UserAccountException("余额不足!");
            }
            String sql = "update account set balance = balance - ? where username = ?";
            jdbcTemplate.update(sql,price,username);
        }
    }
    
    public interface BookShopService {
        public void purchase(String username,String isbn);
    }
    
    public class BookStockException extends RuntimeException {
        private static final long serialVersionUID = 6437368922780528700L;
    
        public BookStockException() {
        }
    
        public BookStockException(String message) {
            super(message);
        }
    
        public BookStockException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public BookStockException(Throwable cause) {
            super(cause);
        }
    
        public BookStockException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }
    
    public class SpringTransactionTest {
    
        private ApplicationContext ctx = null;
        private BookShopDao bookShopDao = null;
        private BookShopService bookShopService = null;
        {
            ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            bookShopDao = ctx.getBean(BookShopDao.class);
            bookShopService = ctx.getBean(BookShopService.class);
        }
    
        @Test
        public void testBookShopService(){
            bookShopService.purchase("AA","1001");
        }
        @Test
        public void testBookShopDaoUpdateUserAccount(){
           bookShopDao.updateUserAccount("AA",100);
        }
        @Test
        public void testBookShopDaoUpdateBookStock(){
            bookShopDao.updateBookStock("1001");
        }
        @Test
        public void testBookShopDaoFindPriceByIsbn(){
            System.out.println(bookShopDao.findBookPriceByIsbn("1001"));
        }
    }
    
    public class UserAccountException extends RuntimeException{
    
        private static final long serialVersionUID = 6719377715174138681L;
    
        public UserAccountException() {
        }
    
        public UserAccountException(String message) {
            super(message);
        }
    
        public UserAccountException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public UserAccountException(Throwable cause) {
            super(cause);
        }
    
        public UserAccountException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }
    
    //输出
    com.cloud.spring.tx.UserAccountException: 余额不足!
    
        at com.cloud.spring.tx.BookShopDaoImpl.updateUserAccount(BookShopDaoImpl.java:37)
        at com.cloud.spring.tx.BookShopServiceImpl.purchase(BookShopServiceImpl.java:20)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
        at com.sun.proxy.$Proxy16.purchase(Unknown Source)
        at com.cloud.spring.tx.SpringTransactionTest.testBookShopService(SpringTransactionTest.java:20)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    
    书的单价.png
    书的库存.png
    账户余额.png

    相关文章

      网友评论

        本文标题:Java Spring-声明式事务

        本文链接:https://www.haomeiwen.com/subject/ndjnyhtx.html