0
    template.setEnableTransactionSupport(true);
    template.multi();
    template.opsForValue().set("mykey", "Hello World");
    List<String> dataList = template.opsForList().range("mylist", 0, -1);
    template.exec();

Hi guys. I have a list called "mylist' in my redis and its size is 50.

But when I run this code, I can't get what I want.

The field "dataList" is null, however, "mykey" with the value "Hello World" have persisted in my redis.

So how can I get my list data in a spring-data-redis transaction? many thanks.

Song Yang
  • 131
  • 1
  • 5

1 Answers1

1

The transaction suppport in SD-Redis helps participating in ongoing transactions and allows automatic commit (exec) / rollback (discard), so it helps wrapping commands into thread bound multi exec blocks using the same connection.
More generally redis transactions and the commands within a transaction get queued on server side and return a list of results on exec.

template.multi();

// queue set command
template.opsForValue().set("mykey", "Hello World"); 

// queue range command
List<String> dataList = template.opsForList().range("mylist", 0, -1);

// execute queued commands
// result[0] = OK
// result[1] = {"item-1", "item-2", "item-", ...}
List<Object> result = template.exec();                                   
Christoph Strobl
  • 6,491
  • 25
  • 33