Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created February 5, 2016 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mp911de/0f8c9cada1564b7e4dd4 to your computer and use it in GitHub Desktop.
Save mp911de/0f8c9cada1564b7e4dd4 to your computer and use it in GitHub Desktop.
Gist to reproduce behavior in DATAREDIS-411
/*
* Copyright 2016 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 com.example;
import javax.sql.DataSource;
import java.sql.SQLException;
import org.h2.Driver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Mark Paluch
*/
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
public @Bean RedisConnectionFactory connectionFactory() {
JedisConnectionFactory f = new JedisConnectionFactory();
f.setUsePool(false);
return f;
}
@Bean
public PlatformTransactionManager transactionManager() throws SQLException {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() throws SQLException {
return new SimpleDriverDataSource(new Driver(), "jdbc:h2:mem:test");
}
}
/*
* Copyright 2016 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 com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Mark Paluch
*/
@Component
public class RedisService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void setKeyValue(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public String readKeyValueWithoutTransaction(String key) {
return redisTemplate.opsForValue().get(key);
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public String readKeyValueWithTransaction(String key) {
return redisTemplate.opsForValue().get(key);
}
}
/*
* Copyright 2016 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 com.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringDataRedisPlaygroundApplication.class)
public class SpringDataRedisPlaygroundApplicationTests {
@Autowired
private RedisService redisService;
@Test
public void contextLoads() {
// set value in a transaction here
redisService.setKeyValue("akey", "avalue");
// value is read ok here
String val = redisService.readKeyValueWithoutTransaction("akey");
System.out.println(val);
// value is null here
val = redisService.readKeyValueWithTransaction("akey");
System.out.println(val);
}
}
@mp911de
Copy link
Author

mp911de commented Feb 5, 2016

Produces output:

avalue
null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment