我对neo4j数据库有问题。当我尝试插入数据时,应该只创建一个样本数据,但有时当我尝试插入数据时,它会创建双样本数据。没有任何关于第二次打电话的痕迹。这是我的Neo4j的Config
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.neo.repository")
@EnableTransactionManagement
public class Neo4jConfig extends Neo4jConfiguration {
@Override
@Bean
public SessionFactory getSessionFactory() {
// with domain entity base package(s)
return new SessionFactory("com.example.neo.model", "BOOT-INF.classes.com.example.neo.model");
}
// needed for session in view in web-applications
@Override
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}
}我就是这样调用我的函数的
@RequestMapping(value = "/initCurrency")
public ModelAndView initCurrency() {
initializationService.initCurrency();
ModelAndView model = new ModelAndView("redirect:/");
return model;
}这是initializationService函数
private String[][] currencyList = {
{ "USD", "7.5" },
{ "DKK", "1" },
{ "AFN", "1"},{ "EUR", "1"},{ "ALL", "1"},{ "DZD", "1"},{ "USD", "1"},{ "AOA", "1"},{ "XCD", "1"},
{ "ARS", "1"},{ "AMD", "1"},{ "AWG", "1"},{ "SHP", "1"},{ "AUD", "1"},{ "AZN", "1"},{ "BSD", "1"},
{ "BHD", "1"},{ "BDT", "1"},{ "BBD", "1"}
}
@Override
public void initCurrency() {
for (String[] currency : currencyList) {
Currency existCurrency = currencyService.findByName(currency[0]);
if (existCurrency == null) {
existCurrency = new Currency(currency[0], Double.valueOf(currency[1]));
currencyService.save(existCurrency);
}
}
}发布于 2016-10-21 12:04:06
避免重复的唯一可靠方法是在属性上有一个实际的单一性约束:
CREATE CONSTRAINT ON (n:Currency) ASSERT n.name IS UNIQUE;SDN 4.0-2中没有办法从模型中创建这样的约束(SDN3.x中有@Indexed或@Indexed(unique = true)注释),所以您必须独立运行查询(例如使用液位仪!)。
在并发环境中,仅进行查找以保护创建是不够的(顺序调用是可以的),因为读和写之间没有锁,这可能导致执行交错的以下场景:
由于您的结果是重复的,因此将发生两个并发调用。负载均衡器具有非常短的超时并配置为重试?激活HTTP,在Spring控制器或服务中添加一些日志,用tcpdump捕获流量,等等。一旦unicity约束激活,就可以更容易地隔离第二个调用,因为您将得到一个异常。
https://stackoverflow.com/questions/40154264
复制相似问题