Spring声明式事务控制原理之声明式事务的重要组件(注解实现)以及之间关系

Spring异步处理Async Spring Async 异步处理 阅读详情

实现声明式事务只需要两个注解即可完成:@EnableTransactionManagement和@Transactional。下面就探究一下这两个注解究竟做了什么。

@Transactional注解大家比较熟悉,作用在方法上用来显式声明事务,可以通过属性指定事务的隔离级别,传播行为,什么异常回滚什么异常不回滚等。

主要还是探究一下@EnableTransactionManagement注解

进入EnableTransactionManagement注解类发现首先这个类通过@Import帮Spring以注解解析的方式引入了TransactionManagementConfigurationSelector
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {

	/**
	 * Indicate whether subclass-based (CGLIB) proxies are to be created ({@code true}) as
	 * opposed to standard Java interface-based proxies ({@code false}). The default is
	 * {@code false}. <strong>Applicable only if {@link #mode()} is set to
	 * {@link AdviceMode#PROXY}</strong>.
	 * <p>Note that setting this attribute to {@code true} will affect <em>all</em>
	 * Spring-managed beans requiring proxying, not just those marked with
	 * {@code @Transactional}. For example, other beans marked with Spring's
	 * {@code @Async} annotation will be upgraded to subclass proxying at the same
	 * time. This approach has no negative impact in practice unless one is explicitly
	 * expecting one type of proxy vs another, e.g. in tests.
	 */
	boolean proxyTargetClass() default false;

	/**
	 * Indicate how transactional advice should be applied.
	 * <p><b>The default is {@link AdviceMode#PROXY}.</b>
	 * Please note that proxy mode allows for interception of calls through the proxy
	 * only. Local calls within the same class cannot get intercepted that way; an
	 * {@link Transactional} annotation on such a method within a local call will be
	 * ignored since Spring's interceptor does not even kick in for such a runtime
	 * scenario. For a more advanced mode of interception, consider switching this to
	 * {@link AdviceMode#ASPECTJ}.
	 */
	AdviceMode mode() default AdviceMode.PROXY;

	/**
	 * Indicate the ordering of the execution of the transaction advisor
	 * when multiple advices are applied at a specific joinpoint.
	 * <p>The default is {@link Ordered#LOWEST_PRECEDENCE}.
	 */
	// 默认最大int,,目的做方法最后执行的横切逻辑,将其他切面的横切逻辑也纳入,保证事务的原子性
	int order() default Ordered.LOWEST_PRECEDENCE;

}
进入TransactionManagementConfigurationSelector,这个类⼜向容器中导⼊了两个重要的组件AutoProxyRegistrar、ProxyTransactionManagementConfiguration
// AdviceModeImportSelector 实现了selectImports接口,实现了String[] selectImports(AnnotationMetadata importingClassMetadata);方法
	//此方法返回类的全限定名,返回的类将会注册到Spring容器中
public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {

	/**
	 * Returns {@link ProxyTransactionManagementConfiguration} or
	 * {@code AspectJ(Jta)TransactionManagementConfiguration} for {@code PROXY}
	 * and {@code ASPECTJ} values of {@link EnableTransactionManagement#mode()},
	 * respectively.
	 */
	@Override
	protected String[] selectImports(AdviceMode adviceMode) {
		switch (adviceMode) {
			case PROXY:
				//AutoProxyRegistrar注册生成代理类的bean后置处理器
				//ProxyTransactionManagementConfiguration注册处理事务的advisor(通知器)
				return new String[] {AutoProxyRegistrar.class.getName(),
						ProxyTransactionManagementConfiguration.class.getName()};
			case ASPECTJ:
				return new String[] {determineTransactionAspectClass()};
			default:
				return null;
		}
	}

	private String determineTransactionAspectClass() {
		return (ClassUtils.isPresent("javax.transaction.Transactional", getClass().getClassLoader()) ?
				TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME :
				TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME);
	}

}
1.进入AutoProxyRegistrar, AutoProxyRegistrar 类的 registerBeanDefinitions ⽅法中⼜注册了⼀个组件
public class AutoProxyRegistrar implements ImportBeanDefinitionRegistrar {

	private final Log logger = LogFactory.getLog(getClass());

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		boolean candidateFound = false;
		Set<String> annTypes = importingClassMetadata.getAnnotationTypes();
		for (String annType : annTypes) {
			AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType);
			if (candidate == null) {
				continue;
			}
			Object mode = candidate.get("mode");
			Object proxyTargetClass = candidate.get("proxyTargetClass");
			if (mode != null && proxyTargetClass != null && AdviceMode.class == mode.getClass() &&
					Boolean.class == proxyTargetClass.getClass()) {
				candidateFound = true;
				if (mode == AdviceMode.PROXY) {
				// 此处注册了InfrastructureAdvisorAutoProxyCreator实现了SmartInstantiationAwareBeanPostProcessor是个bean的后置处理器
					AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
					if ((Boolean) proxyTargetClass) {
						AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
						return;
					}
				}
			}
		}
		if (!candidateFound && logger.isInfoEnabled()) {
			String name = getClass().getSimpleName();
			logger.info(String.format("%s was imported but no annotations were found " +
					"having both 'mode' and 'proxyTargetClass' attributes of type " +
					"AdviceMode and boolean respectively. This means that auto proxy " +
					"creator registration and configuration may not have occurred as " +
					"intended, and components may not be proxied as expected. Check to " +
					"ensure that %s has been @Import'ed on the same class where these " +
					"annotations are declared; otherwise remove the import of %s " +
					"altogether.", name, name, name));
		}
	}

}

进入registerAutoProxyCreatorIfNecessary方法发现注册的后置处理器InfrastructureAdvisorAutoProxyCreator它实现SmartInstantiationAwareBeanPostProcessor,说明这是⼀个后置处理器,⽽且跟spring AOP 开启@EnableAspectJAutoProxy 时注册的 AnnotationAwareAspectJProxyCreator实现的是同⼀个接⼝,所以说,声明式事务是 springAOP 思想的⼀种应⽤
2.下面进入ProxyTransactionManagementConfiguration,见@Configuration值此类是个配置类,主要作用是向Spring中注入BeanFactoryTransactionAttributeSourceAdvisor(事务通知器/切面)
@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {

	@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
		BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
		//  向事务通知器中注⼊ 属性解析器 transactionAttributeSource
		advisor.setTransactionAttributeSource(transactionAttributeSource());
		//  向事务增强器中注⼊ 事务拦截器 transactionInterceptor
		advisor.setAdvice(transactionInterceptor());
		if (this.enableTx != null) {
			advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
		}
		return advisor;
	}

	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public TransactionAttributeSource transactionAttributeSource() {
		return new AnnotationTransactionAttributeSource();
	}

	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public TransactionInterceptor transactionInterceptor() {
		TransactionInterceptor interceptor = new TransactionInterceptor();
		interceptor.setTransactionAttributeSource(transactionAttributeSource());
		if (this.txManager != null) {
			interceptor.setTransactionManager(this.txManager);
		}
		return interceptor;
	}

}

进入BeanFactoryTransactionAttributeSourceAdvisor,Advisor两大组成部分是1.切点PointCut。2.通知Advice(连接点+横切逻辑;在SpringAOP中用于实现MethodInterceptor的invoke方法);

BeanFactoryTransactionAttributeSourceAdvisor的两大重要组件

1.属性解析器 TransactionAttributeSource(主要作用解析某方法中是否包含@Transactional,解析Transactional注解的各个属性封装成配置类备用)
2.务拦截器 TransactionInterceptor(实现MethodInterceptor的invoke方法,封装了横切逻辑 :设置事务自动提交fasle、事务提交、事务发生异常回滚)

其中BeanFactoryTransactionAttributeSourceAdvisor的PointCut也是根据TransactionAttributeSource属性解析器来判断一个类的某个方法是否切点,从而判断一个类是否要进行事务AOP处理创建代理类

public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor {

	@Nullable
	private TransactionAttributeSource transactionAttributeSource;

	private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
		@Override
		@Nullable
		protected TransactionAttributeSource getTransactionAttributeSource() {
			return transactionAttributeSource;
		}
	};


	/**
	 * Set the transaction attribute source which is used to find transaction
	 * attributes. This should usually be identical to the source reference
	 * set on the transaction interceptor itself.
	 * @see TransactionInterceptor#setTransactionAttributeSource
	 */
	public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource) {
		this.transactionAttributeSource = transactionAttributeSource;
	}

	/**
	 * Set the {@link ClassFilter} to use for this pointcut.
	 * Default is {@link ClassFilter#TRUE}.
	 */
	public void setClassFilter(ClassFilter classFilter) {
		this.pointcut.setClassFilter(classFilter);
	}

	@Override
	public Pointcut getPointcut() {
		return this.pointcut;
	}

}

进入事务属性解析器 TransactionAttributeSource
AnnotationTransactionAttributeSource.class
// 事务属性解析器集合(包括Spring的解析器、jpa的解析器)
private final Set<TransactionAnnotationParser> annotationParsers;
进入Spring的事务属性解析器
public class SpringTransactionAnnotationParser implements TransactionAnnotationParser, Serializable {

	@Override
	@Nullable
	// 判断方法中是否含有@Transaction注解
	public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
		AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
				element, Transactional.class, false, false);
		if (attributes != null) {
			return parseTransactionAnnotation(attributes);
		}
		else {
			return null;
		}
	}

	public TransactionAttribute parseTransactionAnnotation(Transactional ann) {
		return parseTransactionAnnotation(AnnotationUtils.getAnnotationAttributes(ann, false, false));
	}
	// 解析@Transaction注解的各个属性封装成配置类备用
	protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
		RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();

		Propagation propagation = attributes.getEnum("propagation");
		rbta.setPropagationBehavior(propagation.value());
		Isolation isolation = attributes.getEnum("isolation");
		rbta.setIsolationLevel(isolation.value());
		rbta.setTimeout(attributes.getNumber("timeout").intValue());
		rbta.setReadOnly(attributes.getBoolean("readOnly"));
		rbta.setQualifier(attributes.getString("value"));

		List<RollbackRuleAttribute> rollbackRules = new ArrayList<>();
		for (Class<?> rbRule : attributes.getClassArray("rollbackFor")) {
			rollbackRules.add(new RollbackRuleAttribute(rbRule));
		}
		for (String rbRule : attributes.getStringArray("rollbackForClassName")) {
			rollbackRules.add(new RollbackRuleAttribute(rbRule));
		}
		for (Class<?> rbRule : attributes.getClassArray("noRollbackFor")) {
			rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
		}
		for (String rbRule : attributes.getStringArray("noRollbackForClassName")) {
			rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
		}
		rbta.setRollbackRules(rollbackRules);

		return rbta;
	}


	@Override
	public boolean equals(Object other) {
		return (this == other || other instanceof SpringTransactionAnnotationParser);
	}

	@Override
	public int hashCode() {
		return SpringTransactionAnnotationParser.class.hashCode();
	}

}

进入务拦截器 TransactionInterceptor的invoke方法
TransactionInterceptor.class
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// Work out the target class: may be {@code null}.
		// The TransactionAttributeSource should be passed the target class
		// as well as the method, which may be from an interface.
		Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);

		// Adapt to TransactionAspectSupport's invokeWithinTransaction...
		// 带有事务处理的方法执行
		return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
	}
进入TransactionAspectSupport#invokeWithinTransaction方法
TransactionAspectSupport.class
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,
			final InvocationCallback invocation) throws Throwable {

		// If the transaction attribute is null, the method is non-transactional.
		// 1。获取事务属性解析器ProxyTransactionManagementConfiguration中注入
		TransactionAttributeSource tas = getTransactionAttributeSource();
		final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
		// 2.获取事务管理器
		final PlatformTransactionManager tm = determineTransactionManager(txAttr);
		final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);

		if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
			// Standard transaction demarcation with getTransaction and commit/rollback calls.
			TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);

			Object retVal;
			try {
				// This is an around advice: Invoke the next interceptor in the chain.
				// This will normally result in a target object being invoked.
				// 3.执行原方法
				retVal = invocation.proceedWithInvocation();
			}
			catch (Throwable ex) {
				// target invocation exception
				// 4.如果目标方法剖出异常执行回滚操作,并且抛出异常
				completeTransactionAfterThrowing(txInfo, ex);
				throw ex;
			}
			finally {
				cleanupTransactionInfo(txInfo);
			}
			// 5.如果目标方法正常执行执行事务提交操作。
			commitTransactionAfterReturning(txInfo);
			return retVal;
		}

		else {
			final ThrowableHolder throwableHolder = new ThrowableHolder();

			// It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
			try {
				Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> {
					TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
					try {
						return invocation.proceedWithInvocation();
					}
					catch (Throwable ex) {
						if (txAttr.rollbackOn(ex)) {
							// A RuntimeException: will lead to a rollback.
							if (ex instanceof RuntimeException) {
								throw (RuntimeException) ex;
							}
							else {
								throw new ThrowableHolderException(ex);
							}
						}
						else {
							// A normal return value: will lead to a commit.
							throwableHolder.throwable = ex;
							return null;
						}
					}
					finally {
						cleanupTransactionInfo(txInfo);
					}
				});

				// Check result state: It might indicate a Throwable to rethrow.
				if (throwableHolder.throwable != null) {
					throw throwableHolder.throwable;
				}
				return result;
			}
			catch (ThrowableHolderException ex) {
				throw ex.getCause();
			}
			catch (TransactionSystemException ex2) {
				if (throwableHolder.throwable != null) {
					logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
					ex2.initApplicationException(throwableHolder.throwable);
				}
				throw ex2;
			}
			catch (Throwable ex2) {
				if (throwableHolder.throwable != null) {
					logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
				}
				throw ex2;
			}
		}
	}

本文主要讲了声明式事务的重要组件以及之间关系,下篇文章会讲述各个组件在事务实现SpringAOP中的作用体现

Spring @Async 注解的使用以及原理(二) 在上一篇中《Spring @Async 注解的使用以及原理(一)》简单介绍了@Async的使用,本篇简单分析一下它的源码以及实现异步的机制。 我们用spring-context-5.0.5.RELEASE.jar包,先看一下@Async的源码: package org.springframework.scheduling.annotation; import j... 阅读详情

相关推荐

Spring的声明式事务详解:注解驱动下的高效事务管理

Spring声明式事务管理详解 本文全面介绍了Spring框架中的声明式事务管理机制。首先对比了声明式事务与编程式事务的差异,突出声明式事务的低侵入性、配置灵活性和开发效率优势。重点解析了@Transactional注解的使用方法及其关键属性,包括事务传播行为(propagation)、隔离级别(isolation)、回滚规则(rollbackFor/noRollbackFor)、超时设置(timeout)和只读事务(readOnly)。深入剖析了声明式事务基于AOP的实现原理,涉及代理创建、切面织入等核心

weixin_42593797的博客 1135

美团一面:@Transactional 原理和常见的坑?

Hollis的新书限时折扣中,一本深入讲解Java基础的干货笔记!下面是 DB 数据和 DB 操作接口:uidunameusex1张三女2陈恒男3楼仔男//提供的接口 publicinterfaceUserDao{ //select*fromuser_testwhereuid="#{uid}" publicMyUserselec...

HollisChuang's Blog 546

深入理解Spring(六)

事务是开发中必不可少的东西,使用JDBC开发时,我们使用connection对事务进行控制,使用MyBatis时,我们使用SqlSession对事务进行控制,缺点显而易见,当我们切换数据库访问技术时,事务控制的方式总会变化,Spring就将这些技术基础上,提供了统一的控制事务的接口。Spring的事务分为:编程式事务和声明式事务控制。

三日沐水 1450

5 个关于Spring @Transactional的坑

https://codete.com/blog/5-common-spring-transactional-pitfalls/ The article is a part of JPA & Spring pitfalls series. Below you can find the actual list of all articles of the series: Spring...

lppl010_的专栏 1948

@Transactional 声明式事务失效场景总结以及底层原理剖析

在Spring框架中,是实现声明式事务管理的核心注解,但在实际开发中,很多时候我们会遇到事务的情况。以下是,分门别类进行说明,适合进阶开发者理解和规避。

weixin_46072507的博客 1224

使用电荷放大器处理压电加速度计输出

电阻器 R p模拟传感器的绝缘电阻,为产生的电荷创建泄漏路径。在此图中,顶部波形显示传感器产生的电荷,而底部波形显示电荷放大器的输出。这需要具有大输入阻抗的放大级,以防止产生的电荷通过与传感元件并联的放大器的输入阻抗泄漏。换句话说,电荷放大器补偿传感器产生的电荷 q p ,反馈电容器 C F中具有相反极性的等量电荷。因此,任何与传感器并联的绝缘电阻,如电缆的绝缘电阻或传感器的漏电电阻Rp,都不能流过电流。漂移是指电荷放大器输出在一段时间内发生的变化,而不是由被测物理参数的变化(我们讨论的加速度)引起的。

weixin_45260069的博客 3911

Spring循环引用-@Async注解启动报错,而@Transactional则不会

Spring循环引用-@Async注解启动报错,而@Transactional则不会

liangbo7的博客 1202

iOS面试题大全(附带答案)

C语言相关面试题 1.static有什么用途? 答案:在C语言中,static主要定义全局静态变量,定义局部静态变量,定义静态函数。 static 属于静态变量,使用它修饰的变量生命周期是整个源程序。 @1.在函数体内的 static 变量的作用范围为该函数体,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值; @2.在模块内的 static 全局变量可以被模块内所有函数访问,但不能被模块外其它函数访问; @3.在模块内的 static 函数只被这一模块内的其它函数调用,这个函数的使用范围被限

IOSNF的博客 2434

Spring事务实现的方式及底层原理

spring事务的实现方式 spring框架提供了两种事务实现方式:编程式事务、声明式事务 编程式事务:在代码中进行事务控制。优点:精度高。缺点:代码耦合度高 声明式事务:通过@Transactional注解实现事务控制 spring事务的底层原理 事务的操作本来应该由数据库进行控制,但是为了方便用户进行业务逻辑的控制,spring对事务功能进行了扩展实现。一般我们很少使用编程式事务,更多的是使用@Transactional注解实现。当使用了@Transactional注解后事务的自动功能就会关闭,由sp.

qq_34886456的博客 1万+

SPSS_Process—简单&平行&链式中介模型

本篇文章介绍采用SPSS的插件Process对简单中介、链式中介、平行中介模型进行检验的操作方法及结果解读。

m0_54401011的博客 4万+

ROS环境下UR机械臂与Realsense相机手眼标定实战指南

本文提供了在ROS环境下,对UR机械臂与Realsense相机进行手眼标定的完整实战指南。详细介绍了从环境搭建、软件包安装、硬件配置到分步数据采集与计算的完整流程,并分享了常见的避坑技巧与精度优化方法,旨在帮助开发者高效、准确地完成标定,为后续的机器人视觉应用奠定坚实基础。

sunny的博客 656

【Spring系列-08】声明式事务实现原理

Spring 声明式事务实现原理深度剖析 摘要:本文深入分析了Spring声明式事务的核心实现原理,重点探讨了事务抽象设计思想和传播行为机制。Spring通过PlatformTransactionManager接口实现了事务管理的抽象,支持多种事务管理器实现(如JDBC、JPA、JTA等),并通过TransactionDefinition定义事务属性。文章详细解析了7种事务传播行为在源码中的实现逻辑,包括PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW等,并展示了转

专注后端技术架构,AI技术分享 2126

PT:pt write_change to innovus 脚本

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?

m0_61544122的博客 1543

ssm框架-spring-spring声明式事务

事务是数据库操作的最小工作单元,作为单个逻辑工作单元执行的一系列操作。这些操作必须作为一个整体来执行,即要么全部成功,要么全部失败。事务的这一特性保证了数据的一致性和可靠性,特别是在多用户共享资源的环境下。在没有spring框架的情况下,需要我们进行编程式事务,即自己手动控制事务的回滚和提交但是操作繁杂,所以我们使用了spring框架的声明式事务自动控制事务的各种流程,那么如何添加声明式事务呢?

2302_77493813的博客 2114

【模型实战】MiMo-VL-7B讲解与实战

维度成绩MMMU70.6(图像理解最难测试之一)VideoMME70.8(视频理解标杆)59.4(超越78B参数模型)56.1(超过专用GUI模型)Elo评分开源VLM排名第一(7B~72B)社区热度过去一个月下载超1,600次(RL+SFT)📌一句话概括MiMo-VL 是目前性能最强、推理最稳、体验最佳、生态最全的7B级开源视觉语言模型。

m0_47867638的博客 1216

PCL DBSCAN密度聚类【2026最新版】

点云密度聚类的PCL代码实现,与matlab自带的密度聚类算法进行对比验证,结果一致。3万多个点能再2秒左右运行出结果,算法效率尚可。

点云侠的博客 5690

【Spring】-编程式事务和声明式事务

PlatformTransactionManager 事务管理器TransactionDefinition 事务属性优点:与业务逻辑分离:声明式事务管理将事务管理逻辑从业务代码中分离出来,使得业务逻辑更清晰,降低了代码的耦合性。配置简单:通过注解或XML配置,可以简单地定义事务的传播行为、隔离级别等属性,而无需在每个业务方法中编写重复的事务管理代码。易于维护:由于事务管理逻辑集中在配置中,易于维护和修改,提高了代码的可读性和可维护性。提高一致性。

一起加油吧~ 4703

【K8S系列】Kubernetes 中 Service 的流量不均匀问题【已解决】

在 Kubernetes 中,Service 是一种抽象,用于定义一组 Pod 的访问策略。当某些 Pod 接收的流量过多,而其他 Pod 的流量较少时,可能会导致负载不均衡。这种情况不仅影响性能,还可能导致某些 Pod 过载,影响应用的可用性。本文将详细分析此问题的原因及其解决方案。

颜淡慕潇 8746

SpringBoot中@Transactional事务控制实现原理及事务无效问题排查

1.spring事务管理简述 两种事务管理方式: 编码式事务管理:将事务控制代码编写在业务代码之中。 声明式事务管理:基于AOP(面向切面编程),事务管理与业务逻辑解耦。声明式事务管理的两种实现: 在配置文件(xml)中配置。 基于@Transactional注解。 2.SpringBoot中使用@Transactional注解 2.1.开启事务注解 在项目主类上,加上注解@Enable...

韩超的博客 (hanchao5272) 1万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Spring之事务原理篇

通过几个简单的面试题,带你学习Spring事务的原理。

个人博客,欢迎收藏。 1908

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

spring是如何实现声明式事务的

前言 有好长一段时间没写文章了,之前一直在找工作,如今工作换好了,可以继续写文章了,今天我们来讲讲spring的声明式事务。 开始 说到声明式事务,我们现在回顾一下事务这个概念,什么是事务呢,事务指的是逻辑上的⼀组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功。从而确保了数据的准确与安全。事务有着四大特性(ACID),分别是 原子性(Atomicity)原⼦性是指事务是⼀个不可分割的⼯作单位,事务中的操作要么都发⽣,要么都不发⽣。 ⼀致性(Consistency)事务必须使数据库从⼀个

wangjimmy1994的博客 2242

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

Spring声明式事务源码分析

它实现了SmartInstantiationAwareBeanPostProcessor,说明这是⼀个后置处理器,⽽且跟spring AOP 开启@EnableAspectJAutoProxy 时注册的 AnnotationAwareAspectJProxyCreator实现的是同⼀个接⼝,所以说,声明式事务是 springAOP 思想的⼀种应⽤。:在业务代码中添加事务控制代码,这样的事务控制机制就叫做编程式事务。:通过xml或者注解配置的⽅式达到事务控制的⽬的,叫做声明式事务。上述组件如何关联起来的?

他叫阿来 1449

Spring的事务管理 - 声明式事务

基于Spring AOP实现。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。编程式事务管理使用可实现更细粒度的事务控制。声明式事务管理不需要入侵代码,通过就可以进行事务操作,更快捷而且简单且大部分业务都可以满足,推荐使用。其实不管是编程式事务还是声明式事务,最终调用的底层核心代码是一致的。

埃泽漫笔 1562

【Spring】声明式事务 spring-tx

核心点掌握目标spring框架理解spring家族和spring framework框架spring核心功能组件管理、ioc容器、ioc/di , 三种配置方式spring aopaop和aop框架和代理技术、基于注解的aop配置spring tx声明式和编程式事务、动态事务管理器、事务注解、属性。

Orion 895

Spring声明式事务控制

1)事务开始时,通过AOP机制,生成一个代理的connection对象,并将其放入DataSource实例的某个与DataSourceTransactionManager相关的容器中。在接下来的整个事务中,客户端与数据库的交互都使用该connection。 2)事务结束时,回滚在第一步中得到的代理的connection中的数据库命令,然后关闭该代理connection对象。 具体分析: 声明式事务很⽅便,尤其纯注解模式,仅仅⼏个注解就能控制事务了? @Enable...

qq_38658642的博客 1336

科普文:spring声明式事务@Transactional原理与实现

在使用事务的时候需要添加@EnableTransactionManagement注解来开启事务,Spring事务底层是通过AOP来实现的,所以启用事务后,同样会向容器中注入一个代理对象创建器,AOP使用的是AnnotationAwareAspectJAutoProxyCreator,事务使用的是InfrastructureAdvisorAutoProxyCreator。

为无为,事无事,味无味。 1875
上一篇: advisor和aspect有什么区别,为什么在代理类生成之前会将全部的aspect和通用拦截器(MethodInterceptor)适配成advisor?
下一篇: Spring声明式事务控制原理之声明式事务的重要组件在AOP中的应用
躺平程序猿
博客等级 码龄8年 4870粉丝 71原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺平程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址