问题描述
我正在使用Apache Geode 1.13.0
数据类很简单,它包含1个ArrayList和1个HashMap
数据已正确存储在geode上,但随后repository.findById()返回了错误的数组和地图
repository.findAll()效果很好
数据是
id | someList | someMap
-- | --------- | ---------------------------------
1 | [1,2,3,4] | {"1":"a","2":"b","3":"c","4":"d"}
2 | [2,4] | {"4":"d","3":"c"}
3 | [3,4] | {"4":"d","3":"c"}
4 | null | {"4":"d"}
5 | null | null
在findById检索时
Class1{id=1,someList=[3,4],someMap={4=d}}
Class1{id=2,someMap={4=d}}
Class1{id=3,someMap={4=d}}
Class1{id=4,someList=null,someMap={4=d}}
Class1{id=5,someMap=null}
逻辑位于MyRunner.java
中可能是什么问题?
这是课程
....
@Region("TestRegion")
public class Class1 implements PdxSerializable {
@Id
Integer id;
ArrayList<Integer> someList;
HashMap<Integer,String> someMap;
public Class1() {}
public Class1(Integer id,ArrayList<Integer> someList,HashMap<Integer,String> someMap) {
this.id = id;
this.someList = someList;
this.someMap = someMap;
}
@Override
public String toString() {
String ret;
ret = "Class1";
ret += "{id=";
if (id == null) { ret += "null"; } else { ret += id.toString(); }
ret += ",someList=";
if (someList == null) { ret += "null"; } else { ret += someList.toString(); }
ret += ",someMap=";
if (someMap == null) { ret += "null"; } else { ret += someMap.toString(); }
ret += "}";
return ret;
}
@Override
public void toData(PdxWriter out) throws PdxFieldAlreadyExistsException,PdxSerializationException {
out.writeInt("id",id);
out.writeObject("someList",someList);
out.writeObject("someMap",someMap);
}
@Override
public void fromData(PdxReader in) throws PdxFieldTypeMismatchException,PdxSerializationException {
id = in.readInt("id");
someList = (ArrayList<Integer>)in.readObject("someList");
someMap = (HashMap<Integer,String>)in.readObject("someMap");
}
}
存储库
@Repository
public interface GeodeRepository extends CrudRepository<Class1,Integer> {
}
geode配置类
.....
@EnableEntityDefinedRegions(basePackageClasses = Class1.class,clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY)
@EnableGemfireRepositories(basePackageClasses = GeodeRepository.class)
@Configuration
class GeodeConfiguration {
}
主要
....
@SpringBootApplication
class SpringbootCmdTest {
public static void main(String[] args) {
SpringApplication.run(SpringbootCmdTest.class,args);
}
}
MyRunner类
....
@Component
public class MyRunner implements CommandLineRunner {
@Autowired
private GeodeRepository repository;
@Override
public void run(String... args) throws Exception {
//repository.deleteAll(); // doesn't work for partitioned regions as of 2020-11-02 https://jira.spring.io/browse/DATAGEODE-265
repository.deleteAll(repository.findAll());
ArrayList<Integer> l = new ArrayList<>();
HashMap<Integer,String> m = new HashMap<>();
Class1 obj;
Optional<Class1> o;
l.clear(); l.add(1); l.add(2); l.add(3); l.add(4);
m.clear(); m.put(1,"a"); m.put(2,"b"); m.put(3,"c"); m.put(4,"d");
obj = new Class1(1,l,m);
repository.save(obj);
l.clear(); l.add(2); l.add(3); l.add(4);
m.clear(); m.put(2,"d");
obj = new Class1(2,m);
repository.save(obj);
l.clear(); l.add(3); l.add(4);
m.clear(); m.put(3,"d");
obj = new Class1(3,m);
repository.save(obj);
m.clear(); m.put(4,"d");
obj = new Class1(4,null,m);
repository.save(obj);
obj = new Class1(5,null);
repository.save(obj);
System.out.println("\n-- findAll().foreach works -------------------------------");
repository.findAll().forEach((item) ->System.out.println(item.toString()));
System.out.println("\n-- optional directly to string. issue with the array and map. displays the last entry --");
System.out.println(repository.findById(1).toString());
System.out.println(repository.findById(2).toString());
System.out.println(repository.findById(3).toString());
System.out.println(repository.findById(4).toString());
System.out.println(repository.findById(5).toString());
System.out.println("\n-- first convert the optional to object. issue with the array and map. displays the last entry --");
o = repository.findById(1);
o.ifPresent(ob -> System.out.println(ob.toString()));
o = repository.findById(2);
o.ifPresent(ob -> System.out.println(ob.toString()));
o = repository.findById(3);
o.ifPresent(ob -> System.out.println(ob.toString()));
o = repository.findById(4);
o.ifPresent(ob -> System.out.println(ob.toString()));
o = repository.findById(5);
o.ifPresent(ob -> System.out.println(ob.toString()));
System.out.println("\n-- findAllById().foreach does not work either -------------------------------");
ArrayList<Integer> il = new ArrayList<>();
il.add(1); il.add(2); il.add(3); il.add(4); il.add(5);
repository.findAllById(il).forEach((item) ->System.out.println(item.toString()));
System.out.println("\n---------------------------------");
}
}
application.properties
spring.profiles.active = dev
logging.level.org.springframework.boot = INFO
logging.level.org.springframework.transaction = TRACE
logging.level.owa = DEBUG
logging.file.name = Test.log
spring.main.banner-mode=off
spring.profiles.include=common
spring.data.gemfire.pool.locators = localhost[2001]
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>org.Test</groupId>
<artifactId>SpringbootCmdTest</artifactId>
<version>20.10.0</version>
<name>SpringbootCmdTest</name>
<description>Springboot Cmd Line For Testing</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.geode</groupId>
<artifactId>spring-geode-starter</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.geode</groupId>
<artifactId>geode-core</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.8.1</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>compileTests</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
gfsh查询
gfsh>query --query="select * from /TestRegion"
Result : true
Limit : 100
Rows : 5
id | someList | someMap
-- | --------- | ---------------------------------
1 | [1,"3":"c"}
4 | null | {"4":"d"}
5 | null | null
输出
-- findAll().foreach works -------------------------------
Class1{id=1,someList=[1,someMap={1=a,2=b,3=c,4=d}}
Class1{id=2,someList=[2,someMap={4=d,3=c}}
Class1{id=3,3=c}}
Class1{id=4,someMap=null}
-- optional directly to string. issue with the array and map. displays the last entry --
Optional[Class1{id=1,someMap={4=d}}]
Optional[Class1{id=2,someMap={4=d}}]
Optional[Class1{id=3,someMap={4=d}}]
Optional[Class1{id=4,someMap={4=d}}]
Optional[Class1{id=5,someMap=null}]
-- first convert the optional to object. issue with the array and map. displays the last entry --
Class1{id=1,someMap=null}
-- findAllById().foreach does not work either -------------------------------
Class1{id=1,someMap=null}
解决方法
注意:此行为不是Spring独有的,可以单独使用Apache Geode复制。
由于将clientRegionShortcut
设置为ClientRegionShortcut.CACHING_PROXY
,因此您的数据既存储在本地服务器上,又存储在客户端上。通过id访问数据时,将首先检查本地区域,如果找到该条目,则将其返回;如果未找到该条目,它将为此向服务器呼出。
之所以只看到列表和地图的最新值,是因为该区域通过引用保留了m
和l
,因此当您重用它们并更新它们时内容,存储在本地区域中的值也反映了更改,即使不保存也是如此。您会看到findAll()
和查询的正确值,因为它们直接委托给服务器(由于它是单独的机器/进程,因此不按引用保存值),而不是本地。
您可以通过以下几种方式获得预期的行为:
选项1.将ClientRegionShortcut.CACHING_PROXY
更改为ClientRegionShortcut.PROXY
,这样就不会在本地存储值,而是每次都会从服务器检索这些值。
选项2。每次您要添加条目而不是重复使用相同的对象时,都可以创建新的ArrayList
和HashMap
。例如,将l.clear()
和m.clear()
替换为l = new ArrayList<>()
和m = new HashMap<>()
。