Fix StringRedisConnection not passed to callbacks
in StringTemplate.execute() DATAREDIS-222
This commit is contained in:
@@ -157,23 +157,24 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
conn = RedisConnectionUtils.getConnection(factory);
|
||||
|
||||
boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);
|
||||
preProcessConnection(conn, existingConnection);
|
||||
|
||||
boolean pipelineStatus = conn.isPipelined();
|
||||
RedisConnection connToUse = preProcessConnection(conn, existingConnection);
|
||||
|
||||
boolean pipelineStatus = connToUse.isPipelined();
|
||||
if (pipeline && !pipelineStatus) {
|
||||
conn.openPipeline();
|
||||
connToUse.openPipeline();
|
||||
}
|
||||
|
||||
RedisConnection connToExpose = (exposeConnection ? conn : createRedisConnectionProxy(conn));
|
||||
RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
|
||||
T result = action.doInRedis(connToExpose);
|
||||
|
||||
// close pipeline
|
||||
if (pipeline && !pipelineStatus) {
|
||||
conn.closePipeline();
|
||||
connToUse.closePipeline();
|
||||
}
|
||||
|
||||
// TODO: any other connection processing?
|
||||
return postProcessResult(result, conn, existingConnection);
|
||||
return postProcessResult(result, connToUse, existingConnection);
|
||||
} finally {
|
||||
RedisConnectionUtils.releaseConnection(conn, factory);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.junit.Test;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.StringRedisConnection;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -36,18 +37,19 @@ public class SessionTest {
|
||||
@Test
|
||||
public void testSession() throws Exception {
|
||||
final RedisConnection conn = mock(RedisConnection.class);
|
||||
final StringRedisConnection stringConn = mock(StringRedisConnection.class);
|
||||
RedisConnectionFactory factory = mock(RedisConnectionFactory.class);
|
||||
|
||||
final StringRedisTemplate template = spy(new StringRedisTemplate(factory));
|
||||
when(factory.getConnection()).thenReturn(conn);
|
||||
final StringRedisTemplate template = new StringRedisTemplate(factory);
|
||||
doReturn(stringConn).when(template).preProcessConnection(eq(conn), anyBoolean());
|
||||
|
||||
template.execute(new SessionCallback<Object>() {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Object execute(RedisOperations operations) {
|
||||
checkConnection(template, conn);
|
||||
checkConnection(template, stringConn);
|
||||
template.discard();
|
||||
assertSame(template, operations);
|
||||
checkConnection(template, conn);
|
||||
checkConnection(template, stringConn);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
@@ -55,8 +57,6 @@ public class SessionTest {
|
||||
|
||||
private void checkConnection(RedisTemplate<?, ?> template, final RedisConnection expectedConnection) {
|
||||
template.execute(new RedisCallback<Object>() {
|
||||
|
||||
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
assertSame(expectedConnection, connection);
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.StringRedisConnection;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
*
|
||||
* Integration test of {@link StringRedisTemplate}
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class StringRedisTemplateTests {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
redisTemplate.execute(new RedisCallback<Object>() {
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.flushDb();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeys() throws Exception {
|
||||
redisTemplate.opsForValue().set("foo", "bar");
|
||||
assertNotNull(redisTemplate.keys("*"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testTemplateNotInitialized() throws Exception {
|
||||
RedisTemplate tpl = new RedisTemplate();
|
||||
tpl.setConnectionFactory(redisTemplate.getConnectionFactory());
|
||||
tpl.exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringTemplateExecutesWithStringConn() {
|
||||
String value = redisTemplate.execute(new RedisCallback<String>() {
|
||||
public String doInRedis(RedisConnection connection) {
|
||||
StringRedisConnection stringConn = (StringRedisConnection) connection;
|
||||
stringConn.set("test", "it");
|
||||
return stringConn.get("test");
|
||||
}
|
||||
});
|
||||
assertEquals(value,"it");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="srpConnectionFactory"
|
||||
class="org.springframework.data.redis.connection.srp.SrpConnectionFactory">
|
||||
<property name="hostName">
|
||||
<bean class="org.springframework.data.redis.SettingsUtils"
|
||||
factory-method="getHost" />
|
||||
</property>
|
||||
<property name="port">
|
||||
<bean class="org.springframework.data.redis.SettingsUtils"
|
||||
factory-method="getPort" />
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.data.redis.core.StringRedisTemplate">
|
||||
<property name="connectionFactory" ref="srpConnectionFactory" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user