graphql-jpa-query icon indicating copy to clipboard operation
graphql-jpa-query copied to clipboard

Non-threadsafe Entitymanager instance

Open 256cats opened this issue 5 years ago • 0 comments

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;

256cats avatar Nov 09 '20 13:11 256cats