这是我的服务层代码:
@Override
public String getPreview(final String id) {
final String preview = findOneById(id).getPreview();
if ( id.equals("5065b6dd-7d61-4187-834b-2a0355c0b9e8") && preview == null || "".equals(preview)) {
LOG.error("!!!!!!!!!!!!!!!!!!!!!!!!! PREVIEW IS EMPTY");
}
return preview;
}
private Entity findOneById(final UUID id) {
return metadataRepository.findById(id).orElseThrow(() -> new NotFoundException("File not found"));
}
当我将此代码与顺序访问一起使用时,它可以正常运行,并且返回预览并且没有创建日志消息。 但是当此代码接收到4个以上的并发用户时,它将返回null并显示日志消息。
我的服务标记为org.springframework.transaction.annotation.Transactional
这是我的休眠配置:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${spring.datasource.url}") final String jdbcUrl,
final JpaVendorAdapter jpaVendorAdapter,
final DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan(PERSISTENCE_PACKAGE);
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
final Map<String, Object> properties = entityManagerFactory.getJpaPropertyMap();
properties.put("connection.driver_class","org.postgresql.Driver");
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect");
properties.put("hibernate.connection.url", jdbcUrl);
properties.put("hibernate.cache.use_second_level_cache", "false");
properties.put("hibernate.cache.use_query_cache", "false");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.jdbc.batch_size", "25");
properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");
return entityManagerFactory;
}
我在哪里弄错了? 这个问题看起来类似于Spring Data concurrency