av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring事務執行流程及如何創建事務

瀏覽:4日期:2023-07-18 18:05:30

接上節內容,Spring事務執行原理通過創建一個BeanFactoryTransactionAttributeSourceAdvisor,并把TransactionInterceptor注入進去,而TransactionInterceptor實現了Advice接口。而Spring Aop在Spring中會把Advisor中的Advice轉換成攔截器鏈,然后調用。

執行流程 獲取對應事務屬性,也就是獲取@Transactional注解上的屬性 獲取TransactionManager,常用的如DataSourceTransactionManager事務管理 在目標方法執行前獲取事務信息并創建事務 回調執行下一個調用鏈 一旦出現異常,嘗試異常處理,回滾事務 提交事務 具體分析

獲取對應事務屬性,具體代碼執行流程如下:

final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);

protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) { // Don’t allow no-public methods as required. //1. allowPublicMethodsOnly()返回true,只能是公共方法 if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) { return null; } // Ignore CGLIB subclasses - introspect the actual user class. Class<?> userClass = ClassUtils.getUserClass(targetClass); // The method may be on an interface, but we need attributes from the target class. // If the target class is null, the method will be unchanged. //method代表接口中的方法、specificMethod代表實現類的方法 Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass); // If we are dealing with method with generic parameters, find the original method. //處理泛型 specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod); // First try is the method in the target class. //查看方法中是否存在事務 TransactionAttribute txAttr = findTransactionAttribute(specificMethod); if (txAttr != null) { return txAttr; } // Second try is the transaction attribute on the target class. //查看方法所在類是否存在事務聲明 txAttr = findTransactionAttribute(specificMethod.getDeclaringClass()); if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { return txAttr; } //如果存在接口,則在接口中查找 if (specificMethod != method) { // Fallback is to look at the original method. //查找接口方法 txAttr = findTransactionAttribute(method); if (txAttr != null) { return txAttr; } // Last fallback is the class of the original method. //到接口類中尋找 txAttr = findTransactionAttribute(method.getDeclaringClass()); if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { return txAttr; } } return null;}

getTransactionAttributeSource()獲得的對象是在ProxyTransactionManagementConfiguration創建bean時注入的AnnotationTransactionAttributeSource對象。 AnnotationTransactionAttributeSource中getTransactionAttributeSource方法主要邏輯交給了computeTransactionAttribute方法,所以我們直接看computeTransactionAttribute代碼實現。

computeTransactionAttribute方法執行的邏輯是:

判斷是不是只運行公共方法,在AnnotationTransactionAttributeSource構造方法中傳入true。若方法不是公共方法,則返回null。 得到具體的方法,method方法可能是接口方法或者泛型方法。 查看方法上是否存在事務 查看方法所在類上是否存在事務 查看接口的方法是否存在事務,查看接口上是否存在事務。

所以如果一個方法上用了@Transactional,類上和接口上也用了,以方法上的為主,其次才是類,最后才到接口。

獲取TransactionManager,具體代碼執行流程如下:

protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) { // Do not attempt to lookup tx manager if no tx attributes are set if (txAttr == null || this.beanFactory == null) { return getTransactionManager(); } String qualifier = txAttr.getQualifier(); if (StringUtils.hasText(qualifier)) { return determineQualifiedTransactionManager(qualifier); } else if (StringUtils.hasText(this.transactionManagerBeanName)) { return determineQualifiedTransactionManager(this.transactionManagerBeanName); } else { //常用的會走到這里 PlatformTransactionManager defaultTransactionManager = getTransactionManager(); if (defaultTransactionManager == null) { defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); if (defaultTransactionManager == null) { //從beanFactory獲取PlatformTransactionManager類型的bean defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); this.transactionManagerCache.putIfAbsent( DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); } } return defaultTransactionManager; }}

@Beanpublic PlatformTransactionManager txManager() { return new DataSourceTransactionManager(dataSource());}創建事務主要兩部分: 獲取事務狀態 構建事務信息 獲取事務狀態

代碼如下:

@Override public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { //1.獲取事務 Object transaction = doGetTransaction(); // Cache debug flag to avoid repeated checks. boolean debugEnabled = logger.isDebugEnabled(); if (definition == null) { // Use defaults if no transaction definition given. definition = new DefaultTransactionDefinition(); } //判斷當前線程是否存在事務,判斷依據為當前線程記錄連接不為空且連接中的(connectionHolder)中的transactionActive屬性不為空 if (isExistingTransaction(transaction)) { // Existing transaction found -> check propagation behavior to find out how to behave. return handleExistingTransaction(definition, transaction, debugEnabled); } // Check definition settings for new transaction. //事務超時設置驗證 if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) { throw new InvalidTimeoutException('Invalid transaction timeout', definition.getTimeout()); } // No existing transaction found -> check propagation behavior to find out how to proceed. //如果當前線程不存在事務,但是@Transactional卻聲明事務為PROPAGATION_MANDATORY拋出異常 if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) { throw new IllegalTransactionStateException( 'No existing transaction found for transaction marked with propagation ’mandatory’'); } //如果當前線程不存在事務,PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED都得創建事務 else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED || definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW || definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) { //空掛起 SuspendedResourcesHolder suspendedResources = suspend(null); if (debugEnabled) { logger.debug('Creating new transaction with name [' + definition.getName() + ']: ' + definition); } try { //默認返回true boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); //構建事務狀態 DefaultTransactionStatus status = newTransactionStatus( definition, transaction, true, newSynchronization, debugEnabled, suspendedResources); //構造transaction、包括設置connectionHolder、隔離級別、timeout //如果是新事務,綁定到當前線程 doBegin(transaction, definition); //新事務同步設置,針對當前線程 prepareSynchronization(status, definition); return status; } catch (RuntimeException ex) { resume(null, suspendedResources); throw ex; } catch (Error err) { resume(null, suspendedResources); throw err; } } else { // Create 'empty' transaction: no actual transaction, but potentially synchronization. if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) { logger.warn('Custom isolation level specified but no actual transaction initiated; ' + 'isolation level will effectively be ignored: ' + definition); } //聲明事務是PROPAGATION_SUPPORTS boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS); return prepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null); }}構建事務信息 獲取事務,創建對應的事務實例,這里使用的是DataSourceTransactionManager中的doGetTransaction方法,創建基于JDBC的事務實例,如果當前線程中存在關于dataSoruce的連接,那么直接使用。這里有一個對保存點的設置,是否開啟允許保存點取決于是否設置了允許嵌入式事務。DataSourceTransactionManager默認是開啟的。 如果當先線程存在事務,則轉向嵌套的事務處理。是否存在事務在DataSourceTransactionManager的isExistingTransaction方法中 事務超時設置驗證 事務PropagationBehavior屬性的設置驗證 構建DefaultTransactionStatus。 完善transaction,包括設置connectionHolder、隔離級別、timeout,如果是新事務,綁定到當前線程 將事務信息記錄在當前線程中

以上就是Spring事務執行流程及如何創建事務的詳細內容,更多關于Spring事務執行流程及如何創建的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美精品一区二区三区一线天视频 | 精品在线一区 | 日韩精品在线观看一区二区 | 99久久夜色精品国产亚洲96 | 成人国内精品久久久久一区 | 91精品久久久久久久久 | 亚洲传媒在线 | 99精品网站 | 日本不卡一区二区三区在线观看 | 久久tv在线观看 | 成人精品视频在线观看 | 久久不卡视频 | 日本午夜精品一区二区三区 | 国产91精品久久久久久久网曝门 | 日韩欧美三级 | 亚洲一区二区三区免费在线观看 | 91精品国产91久久久久久 | 亚洲欧美一区二区三区在线 | 北条麻妃一区二区三区在线视频 | 日韩精品久久久久久 | 国产免费一区二区三区最新6 | 免费在线观看av网址 | 成人a视频在线观看 | 久久9热 | 粉嫩高清一区二区三区 | 天天碰日日操 | 日本三级在线 | 亚洲视频区 | 一区二区三区小视频 | 欧美高清性xxxxhdvideosex | 美女爽到呻吟久久久久 | 欧美色综合一区二区三区 | 天天夜天天操 | 在线欧美a| 亚洲精品在线观看视频 | 日韩电影在线 | 国产一区二 | 国产三级日本三级 | 久久综合九九 | 久久精品电影 | 国产精品高清一区二区三区 |