<tx:method >的属性

这篇博客详细介绍了<tx:method>标签的属性,包括Propagation、Isolation、readOnly、timeout以及它们的作用。Propagation属性的7种传播行为分别是什么,如何影响事务执行。Isolation属性的5个隔离级别及其在防止并发问题中的作用。readOnly属性对于提高只读事务执行效率的意义。timeout属性在事务超时控制中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<tx:method >的属性

属性                                      类型                     默认值                     说明

Propagation                         Propagation枚举  REQUIRED             事务传播属性

Isolation                               isolation枚举        DEFAULT(所用数据库默认级别)   事务隔离级别

readOnly                              boolean               false                        是否采用优化的只读事务

timeout                                 int                        -1                            超时(秒)

rollbackFor                           Class[]                 {}                            需要回滚的异常类

rollbackForClassName         String[]                {}                            需要回滚的异常类名

noRollbackFor                      Class[]                 {}                            不需要回滚的异常类

noRollbackForClassName   String[]                 {}                            不需要回滚的异常类名

propagation属性的7个传播行为

1. REQUIRED:指定当前方法必需在事务环境中运行,如果当前有事务环境就加入当前正在执行的事务环境,如果当前没有事务,就新建一个事务。这是默认值。

2. SUPPORTS:指定当前方法加入当前事务环境,如果当前没有事务,就以非事务方式执行。

3. MANDATORY:指定当前方法必须加入当前事务环境,如果当前没有事务,就抛出异常。

4. REQUIRES_NEW:指定当前方法总是会为自己发起一个新的事务,如果发现当前方法已运行在一个事务中,则原有事务被挂起,我自己创建一个属于自己的事务,直我自己这个方法commit 结束,原先的事务才会恢复执行。

5. NOT_SUPPORTED:指定当前方法以非事务方式执行操作,如果当前存在事务,就把当前事务挂起,等我以非事务的状态运行完,再继续原来的事务。

6. NEVER:指定当前方法绝对不能在事务范围内执行,如果方法在某个事务范围内执行,容器就抛异常,只有没关联到事务,才正常执行。

7. NESTED:指定当前方法执行时,如果已经有一个事务存在,则运行在这个嵌套的事务中.如果当前环境没有运行的事务,就新建一个事务,并与父事务相互独立,这个事务拥有多个可以回滚的保证点。就是指我自己内部事务回滚不会对外部事务造成影响,只对 DataSourceTransactionManager事务管理器起效。

isolation属性有这些值: 

1.DEFAULT:采用数据库默认隔离级别

2. SERIALIZABLE:最严格的级别,事务串行执行,资源消耗最大;

3. REPEATABLE_READ:保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。避免了“脏读取”和“不可重复读取”的情况,但是带来了更多的性能损失。

4. READ_COMMITTED:大多数主流数据库的默认事务等级,保证了一个事务不会读到另一个并行事务已修改但未提交的数据,避免了“脏读取”。该级别适用于大多数系统。

5. READ_UNCOMMITTED:保证了读取过程中不会读取到非法数据。隔离级别在于处理多事务的并发问题。

readOnly:表示对应的事务应该被最优化为只读事务。

如果值为true就会告诉Spring这个方法里面没有insert、update或者delete,你只需要提供只读的数据库Connection就行了,这种执行效率会比read-write的Connection高,所以这是一个最优化提示。在一些情况下,一些事务策略能够起到显着的最优化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。

timeout:指定事务超时为几秒。

一般不会使用这个属性。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的解释。

<?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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 对基本包进行注解扫描,开启注解 --> <context:component-scan base-package="com.esms"> <!-- 只对controller进行扫描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- Spring的配置文件,这里主要配置和业务逻辑有关的 --> <!--=================== 数据源,事务控制,xxx ================ --> <!-- 加载数据库配置文件dbconfig.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 加载数据库 --> <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${datasource.connection.driver_class}"/> <property name="jdbcUrl" value="${datasource.connection.url}"/> <property name="user" value="${datasource.connection.username}"/> <property name="password" value="${datasource.connection.password}"/> <property name="minPoolSize" value="${datasource.connection.minPoolSize}"/> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="${datasource.connection.maxPoolSize}"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="${datasource.connection.maxIdleTime}"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="${datasource.connection.acquireIncrement}"/> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="${datasource.connection.maxStatements}"/> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="${datasource.connection.maxStatementsPerConnection}"/> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="${datasource.connection.initialPoolSize}"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="${datasource.connection.idleConnectionTestPeriod}"/> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="${datasource.connection.acquireRetryAttempts}"/> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false --> <property name="breakAfterAcquireFailure" value="${datasource.connection.breakAfterAcquireFailure}"/> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout" value="${datasource.connection.testConnectionOnCheckout}"/> <property name="checkoutTimeout" value="${datasource.connection.checkoutTimeout}"/> <property name="testConnectionOnCheckin" value="${datasource.connection.testConnectionOnCheckin}"/> <property name="automaticTestTable" value="${datasource.connection.automaticTestTable}"/> <property name="acquireRetryDelay" value="${datasource.connection.acquireRetryDelay}"/> <!--自动超时回收Connection--> <property name="unreturnedConnectionTimeout" value="${datasource.connection.unreturnedConnectionTimeout}"/> <!--超时自动断开--> <property name="maxIdleTimeExcessConnections" value="${datasource.connection.maxIdleTimeExcessConnections}"/> <property name="maxConnectionAge" value="${datasource.connection.maxConnectionAge}"/> </bean> <!--================== 配置和MyBatis的整合=============== --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定mybatis全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="pooledDataSource"></property> <!-- 指定mybatis,mapper文件的位置 --> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> </bean> <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描所有dao接口的实现,加入到ioc容器中 --> <property name="basePackage" value="com.esms.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置一个可以执行批量的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!-- ===============事务控制的配置 ================ --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--控制住数据源 --> <property name="dataSource" ref="pooledDataSource"></property> </bean> <!--配置事务增强,事务如何切入 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="SUPPORTS"/> <tx:method name="list*" read-only="true" propagation="SUPPORTS"/> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="query*" read-only="true" propagation="SUPPORTS"/> <tx:method name="count*" read-only="true" propagation="SUPPORTS"/> <tx:method name="update*" read-only="false" propagation="REQUIRED"/> <tx:method name="insert*" read-only="false" propagation="REQUIRED"/> <tx:method name="save*" read-only="false" propagation="REQUIRED"/> <tx:method name="delete*" read-only="false" propagation="REQUIRED"/> <tx:method name="remove*" read-only="false" propagation="REQUIRED"/> <tx:method name="*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:aspectj-autoproxy proxy-target-class="true"/> <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --> <aop:config> <!-- 切入点表达式 --> <!-- 需要修改包的扫描 --> <aop:pointcut expression="execution(* com.esms.service.impl.*.*(..))" id="txPoint" /> <!-- 配置事务增强 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" /> </aop:config> <bean class="com.esms.exception.CustomExceptionResolver"/> </beans>
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值