【自学java笔记#第四十九天#】spring day04 spring中的JdbcTemplate及事务控制

一、Spring中JdbcTemplate的基本用法

 

 二、Spring中的声明式事务控制

可以改进的地方:

spring中有一个内置的事务管理器,叫做:PlatformTransactionManager(它的实现类封装了回滚和提交),以及一个内置的数据源:DriverManagerDataSource。

所以编写代码的时候,可以使用这两个类的对象,进行操作。

三、编程式事务控制

 

 虽然编程式事务控制也能达到目的,但是这么配置的弊端也是很明显的,即重复性代码太多,不符合AOP思想。

所以在实际的开发工作中,主流的还是声明式事务控制。

四、学习过程中遇到的bug汇总

1、Pointcut is not well-formed: expecting ')' at character position 16

错误的写法:

<aop:before method="printLog" pointcut="excution(public void com.lulu.service.impl.AccountServiceImpl.saveAccount())"></aop:before>

正确的写法:

<aop:before method="printLog" pointcut="execution(public void com.lulu.service.impl.AccountServiceImpl.saveAccount())"></aop:before>

bug原因:execution少写了一个e

2、No qualifying bean of type 'com.lulu.service.impl.AccountServiceImpl' available

错误的写法:

IAccountService aService=(IAccountService)aContext.getBean(AccountServiceImpl.class);

正确的写法:

IAccountService aService=(IAccountService)aContext.getBean("accountService");

bug原因:getBean方法里写的是bean对象的id,而不是字节码文件

3、'TRUE' 不是 'boolean' 的有效值。

错误的写法:

<tx:method name="find*" propagation="SUPPORTS" read-only="TRUE"></tx:method>

正确的写法:

<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>

bug原因:read-only属性只有两个默认值,一个是false,一个是true,且区分大小写。

4、声明式事务不支持回滚:(数据库引擎有问题)。

也就是说在程序有异常的情况下,即使配置好了事务控制,但还是无法实现回滚。这样就产生了数据的不一致问题。

在确定配置没有任何错误的前提下,考虑是数据库的引擎问题。

mysql数据库修改引擎方法参考:

https://blog.csdn.net/qiuzhi__ke/article/details/80278003

https://www.cnblogs.com/kerrycode/p/6999550.html

5、Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'transactionTemplate' of bean class [com.lulu.service.impl.AccountServiceImpl]: Bean property 'transactionTemplate' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

错误的写法:

public void setTemplate(TransactionTemplate transactionTemplate) {
            this.transactionTemplate = transactionTemplate;
        }

正确的写法:

public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
            this.transactionTemplate = transactionTemplate;
        }

bug原因:set方法的名称没有写对,所以spring无法正确注入对象。

posted @ 2020-05-21 15:29  一只奶鹿  阅读(196)  评论(0)    收藏  举报