首页 > 其他分享 >学习笔记——Spring声明式事务管理属性(隔离级别、事务超时、事务只读、事务回滚);Spring5新特性、新注解&整合log4j2;Spring5整合Junit5

学习笔记——Spring声明式事务管理属性(隔离级别、事务超时、事务只读、事务回滚);Spring5新特性、新注解&整合log4j2;Spring5整合Junit5

时间:2023-01-19 10:35:19浏览次数:36  
标签:事务管理 事务 回滚 整合 注解 Spring5

2023-01-19

Spring声明式事务管理属性

一、隔离级别

1、概念:一个事务与其他事务之间的隔离等级(1,2,4,8)。

2、隔离级别:

(1)读未提交(1):READ UNCOMMTTED

存在问题:脏读(读取到了未提交数据)

(2)读已提交(2):READ COMMTTED

存在问题:可能出现不可重复读

(3)可重复读(4):REPEATABLE READ

存在问题:可能出现幻读

(4)串行化(8):SERIALIZABLE

二、事务超时

1、设置事务超时时间,到达指定时间后会强制事务回滚

2、类型:int,单位:秒

3、默认值:-1(未设置强制回滚)

三、事务只读(readonly)

1、一般事务方法中只有查询操作时,才将事务设置为只读

2、默认值:false

四、事务回滚

1、rollbackFor:设置回滚的异常Class

2、noRollbackFor:设置不回滚异常Class

五、基于XML方式,配置声明式事务管理

六、Spring5新特性、新注解&整合log4j2

1、添加新注解

@Nullable作用

①位置:可以书写在方法&属性上面&参数前面。

②作用:表示当前方法或属性可以为空,当前属性为空时,消除空指针异常。

2、Spring5整合Log4j2

(1)导入jar包

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.11.2</version>
    <scope>test</scope>
</dependency>

(2)编写配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序:OFF>FATAL>ERROR>WARN>INFO>DEBUG>TRACE>ALL-->
<!--        configuration后面的status用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,可以看到log4j2内部各种详细输出-->
<configuration status="INFO">
<!--    先定义所有的appender-->
    <appenders>
<!--        输出日志信息到控制台-->
        <console name="Console" target="SYSTEM_OUT">
<!--            控制日志输出的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -%msg%n"></PatternLayout>
        </console>
    </appenders>
<!--    然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
<!--    root:用于指定项目的根日志,如果没有单独指定Logger,则会使用root作为默认的日志输出-->
    <loggers>
        <root level="DEBUG">
            <appender-ref ref="Console"></appender-ref>
        </root>
    </loggers>

</configuration>

七、Spring5整合Junit5

1、导入jar包(注:将Junit4的jar包删除)

 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.2</version>
            <scope>test</scope>
        </dependency>

2、使用注解整合即可

(1)整合方式一

@ContextConfiguration(locations = "classpath:applicationContext_transactionmanager.xml")
@ExtendWith(SpringExtension.class)

(2)整合方式二

 @SpringJUnitConfig(locations = "classpath:applicationContext_transactionmanager.xml")

 

标签:事务管理,事务,回滚,整合,注解,Spring5
From: https://www.cnblogs.com/isDaHua/p/17061005.html

相关文章