graphql-jpa-query
graphql-jpa-query copied to clipboard
Non-threadsafe Entitymanager instance
Hi, we are using this library in a Spring Boot application and discovered that DefaultGraphQLJpaQueryConfiguration produces non-threadsafe EntityManager instance and we experienced possible non-threadsafe access to the session exception under load.
Specifically, in https://github.com/introproventures/graphql-jpa-query/blob/master/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java#L62 :
@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(EntityManagerFactory.class)
public GraphQLSchemaBuilder graphQLJpaSchemaBuilder(final EntityManagerFactory entityManagerFactory,
ObjectProvider<RestrictedKeysProvider> restrictedKeysProvider) {
GraphQLJpaSchemaBuilder bean = new GraphQLJpaSchemaBuilder(entityManagerFactory.createEntityManager());
restrictedKeysProvider.ifAvailable(bean::restrictedKeysProvider);
return bean;
}
Here entityManagerFactory.createEntityManager() returns Hibernate's SessionImpl which is not threadsafe.
Replacing it with Spring-provided EntityManager bean solved the issue:
@PersistenceContext
private EntityManager entityManager;