Created
February 5, 2016 13:26
-
-
Save mp911de/0f8c9cada1564b7e4dd4 to your computer and use it in GitHub Desktop.
Gist to reproduce behavior in DATAREDIS-411
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces output: