欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用spring-data-redis中的Redis事務(wù)

 更新時間:2024年07月08日 09:40:01   作者:qq_32331073  
這篇文章主要介紹了使用spring-data-redis中的Redis事務(wù),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

SessionCallback

Redis通過multi, exec, 或discard命令來提供事務(wù)支持,這些操作在RedisTemplate中同樣是可用的。

但是,RedisTemplate 默認(rèn)使用RedisCallBack接口,并不能保證使用同一連接來執(zhí)行同一事務(wù)中的所有操作(此時Transaction是無效的)。

又但是,Spring Data Redis提供了SessionCallback接口,以便在需要保證同一連接執(zhí)行多個操作時使用,比如“需要使用Redis事務(wù)時”。

我們能夠看到:

public <T> T execute(SessionCallback<T> session) {
		Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
		Assert.notNull(session, "Callback object must not be null");

		RedisConnectionFactory factory = getConnectionFactory();
		// bind connection
		RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);//第8行
		try {
			return session.execute(this);
		} finally {
			RedisConnectionUtils.unbindConnection(factory);
		}
	}

RedisTemplate.execute(SessionCallback<T> session)方法的第8行已經(jīng)做了連接綁定

使用方式如下:

//execute a transaction
List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
  public List<Object> execute(RedisOperations operations) throws DataAccessException {
    operations.multi();
    operations.opsForSet().add("key", "value1");

    // This will contain the results of all ops in the transaction
    return operations.exec();
  }
});
System.out.println("Number of items added to set: " + txResults.get(0));

在返回之前,RedisTemplate將使用它的value, hash key和hash value 序列化器來反序列化exec的所有結(jié)果。

另外一個額外的exec方法,允許您為事務(wù)結(jié)果傳遞自定義序列化器。

@Transactional支持

上面我們能夠看到,可以通過SessionCallback綁定連接,并且實現(xiàn)multi, exec,或discard,從而支持Redis事務(wù),但是這樣就顯得很復(fù)雜而且Redis操作(opsXXX.X)執(zhí)行的位置也變得有局限性(盡管不影響功能)。

然而,Spring下我們可以更加簡單,只需兩步:

  • method 添加注解**@Transactional或者Xml配置**(< tx:method />),注冊事務(wù)切點。相當(dāng)于調(diào)用了TransactionSynchronizationManager.setActualTransactionActive(true);
  • 通過 setEnableTransactionSupport(true) 顯式啟用RedisTemplate實例的事務(wù)支持默認(rèn)被禁用
/** Sample Configuration **/
@Configuration
public class RedisTxContextConfiguration {
  @Bean
  public StringRedisTemplate redisTemplate() {
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory());
    // explicitly enable transaction support
    template.setEnableTransactionSupport(true);
    return template;
  }
}

redisTemplate實例 默認(rèn)調(diào)用 execute(RedisCallback action),方法內(nèi)容如下:

public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline){
		/**
		 * 變量聲明等操作……
		 */
		try {
			if (enableTransactionSupport) {
				// only bind resources in case of potential transaction synchronization
				conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);
			} else {
				conn = RedisConnectionUtils.getConnection(factory);
			}
		/**
		 * 其他操作……
		 */
}

public static RedisConnection bindConnection(RedisConnectionFactory factory, 
			boolean enableTransactionSupport) {
		/**
		 * 不用管……
		 */
		RedisConnection conn = factory.getConnection();
		RedisConnection connectionToBind = conn;
		//redisTemplate開啟事務(wù)支持,同時transactionManager非只讀的實際事務(wù)被激活
		if (enableTransactionSupport && isActualNonReadonlyTransactionActive()) {
			connectionToBind = createConnectionProxy(conn, factory);
		}
		/**
		 * 不用管……
		 */
		return conn;
}

可以看到,enableTransactionSupport = true 將會促使當(dāng)前Thread嘗試綁定RedisConnection,僅當(dāng)也 isActualNonReadonlyTransactionActive = true,連接才會成功綁定。

連接綁定成功,同時將會觸發(fā)MULTI

一旦MULTI被調(diào)用:

  • 當(dāng)前RedisConnection將會排隊write操作;
  • 所有readonly操作,例如KEYS將會被分發(fā)給一個全新的 (非Thread綁定)的RedisConnection;
  • 命令EXECDISCARD將交由SpringAOP動態(tài)代理對象去調(diào)用:
  • 如果事務(wù)構(gòu)建過程中沒有異常拋出(默認(rèn)RuntimeException及其子類),則EXEC被調(diào)用,執(zhí)行命令隊列;
  • 否則DISCARD,清除命令隊列。

開啟事務(wù)支持后:

/** Usage Constrainsts **/
// executed on thread bound connection
template.opsForValue().set("foo", "bar");

// read operation executed on a free (not tx-aware)
connection template.keys("*");

// returns null as values set within transaction are not visible
template.opsForValue().get("foo");

上面的樣例代碼是Spring官網(wǎng)給出的,第三個顯然是WATCH命令開啟樂觀鎖后的結(jié)果。

然而至少在本人正在使用的 spring-data-redis-1.8.10.RELEASE.jar

<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
	<version>1.8.10.RELEASE</version>
</dependency>

WATCH命令并沒有被使用,親測第三種效果并不存在(你可以根據(jù)自己的依賴版本嘗試一下),此處亮出代碼。

org.springframework.data.redis.core.RedisConnectionUtils.potentiallyRegisterTransactionSynchronisation

private static void potentiallyRegisterTransactionSynchronisation(RedisConnectionHolder connHolder,
			final RedisConnectionFactory factory) {

		if (isActualNonReadonlyTransactionActive()) {

			if (!connHolder.isTransactionSyncronisationActive()) {
				connHolder.setTransactionSyncronisationActive(true);

				RedisConnection conn = connHolder.getConnection();
				conn.multi();//在此之前conn.watch()未被調(diào)用

				TransactionSynchronizationManager.registerSynchronization(new RedisTransactionSynchronizer(connHolder, conn,
						factory));
			}
		}
	}

聲明兩個RedisTemplate實例

兩個RedisTemplate實例?

  • 支持事務(wù):commands要么統(tǒng)一執(zhí)行,要么都被清除,維護(hù)數(shù)據(jù)完整性;
  • 不支持事務(wù),command立即執(zhí)行,即時返回執(zhí)行結(jié)果并且更高效;
/** Sample Configuration **/
@Configuration
public class RedisTxContextConfiguration {
  @Bean
  public StringRedisTemplate redisTransactionTemplate() {
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory());
    // explicitly enable transaction support
    template.setEnableTransactionSupport(true);
    return template;
  }
 @Bean
  public StringRedisTemplate redisTemplate() {
    StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory());
    return template;
  }
}

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot面試必問之啟動流程知識點詳解

    Spring?Boot面試必問之啟動流程知識點詳解

    SpringBoot是Spring開源組織下的子項目,是Spring組件一站式解決方案,主要是簡化了使用Spring的難度,簡省了繁重的配置,提供了各種啟動器,開發(fā)者能快速上手,這篇文章主要給大家介紹了關(guān)于Spring?Boot面試必問之啟動流程知識點的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • SpringBoot如何訪問html和js等靜態(tài)資源配置

    SpringBoot如何訪問html和js等靜態(tài)資源配置

    這篇文章主要介紹了SpringBoot如何訪問html和js等靜態(tài)資源配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot-controller的使用詳解

    springboot-controller的使用詳解

    本篇文章主要介紹了springboot-controller的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Eureka源碼閱讀之環(huán)境搭建及工程結(jié)構(gòu)

    Eureka源碼閱讀之環(huán)境搭建及工程結(jié)構(gòu)

    這篇文章主要為大家介紹了Eureka源碼閱讀之環(huán)境搭建的工程結(jié)構(gòu)及調(diào)試需知詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>
    2022-10-10
  • Tomcat 多端口 多應(yīng)用

    Tomcat 多端口 多應(yīng)用

    當(dāng)你使用Tomcat作為Web Server的時候,是不是會想過這樣的一個問題:如何利用Tomcat建立兩個Web Server,同時偵聽來自不同端口的請求呢?要實現(xiàn)這一點是很簡單的。以下假設(shè)Tomcat安裝在D:/Tomcat5.0目錄下。
    2008-02-02
  • SpringBoot2 參數(shù)管理實踐之入?yún)⒊鰠⑴c校驗的方式

    SpringBoot2 參數(shù)管理實踐之入?yún)⒊鰠⑴c校驗的方式

    這篇文章主要介紹了SpringBoot2 參數(shù)管理實踐,入?yún)⒊鰠⑴c校驗,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-06-06
  • Java通過JNI調(diào)用C++動態(tài)庫的完整流程詳解

    Java通過JNI調(diào)用C++動態(tài)庫的完整流程詳解

    JNI(Java Native Interface),是實現(xiàn)Java/Kotlin與C/C++語言之間交互的橋梁,本文主要為大家介紹了Java通過JNI調(diào)用C++動態(tài)庫的完整流程,需要的可以參考下
    2025-04-04
  • spring boot切面execution表達(dá)式添加多個包路徑問題及解決方案

    spring boot切面execution表達(dá)式添加多個包路徑問題及解決方案

    在Spring Boot中,如果你想為多個包中的方法創(chuàng)建一個切面,你可以在@Pointcut注解中使用||操作符來指定多個包,下面給大家分享spring boot切面execution表達(dá)式添加多個包路徑問題及解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • 簡單易懂的Java Map數(shù)據(jù)添加指南

    簡單易懂的Java Map數(shù)據(jù)添加指南

    Java提供了多種方法來往Map中添加數(shù)據(jù),開發(fā)者可以根據(jù)具體需求選擇合適的方法,需要的朋友可以參考下
    2023-11-11
  • Java代理的幾種實現(xiàn)方式總結(jié)

    Java代理的幾種實現(xiàn)方式總結(jié)

    本文將通過例子說明java代理的幾種實現(xiàn)方式,并比較它們之間的差異,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的參考價值,需要的朋友可以參考下
    2023-12-12

最新評論