Skip to content

Could not initialize Hazelcast with a dependency on a JPA repository #15359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JorgenRingen opened this issue Nov 30, 2018 · 10 comments
Open
Labels
type: bug A general bug
Milestone

Comments

@JorgenRingen
Copy link

JorgenRingen commented Nov 30, 2018

Issue after upgrading from version 2.0.4 to 2.0.5+.

Startup fails with:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'cacheManager': Requested bean is currently in creation: Is there an unresolvable circular reference?

I'm instantiating a HazelcastInstance bean manually and instantiation relies upon a org.springframework.data.jpa.repository.JpaRepository bean. This causes a circular reference on the cacheManager bean during initialisation.

@Bean public FooMapStore fooMapStore(FooRepository fooRepository) {}
@Bean public Config config(FooMapStore fooMapStore) {}
@Bean public HazelcastInstance hazelcastInstance(Config config) { }

I've debugged the dependency graph and it looks like this:
entityManagerFactory -> cacheManager -> JCacheCacheConfiguration -> hazelcastPropertiesCustomizer -> hazelcastInstance -> config -> fooMapStore -> fooRepository -> (inner bean)#hash -> entityManagerFactory -> cacheManager

Sample application: https://github.com/JorgenRingen/spring-boot-error-cachemanager-currently-in-creation

Any suggestions for workarounds?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 30, 2018
@snicoll snicoll self-assigned this Nov 30, 2018
@snicoll
Copy link
Member

snicoll commented Dec 3, 2018

@JorgenRingen the contract with Spring Boot always has been that the cache infrastructure is available before the JPA entity manager kicks in. The main reasoning behind that is that if you don't do this and you use second level cache, Hibernate will attempt to initialize a cache manager and create a duplicate.

Several things:

I don't see how we could support both use case and having the cache infrastructure initialized by the time hibernate starts (if you're using that) makes sense to me. Can't you defer the registration of the MapStore rather than doing it from the get go?

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Dec 3, 2018
@JorgenRingen
Copy link
Author

JorgenRingen commented Dec 3, 2018

Okey, I see we should've avoided the cycle in the first place.

Are there any preferable way of deferring the registration? As a first draft I'm doing a "lazy-lookup" from the ApplicationContext on the first call on FooRepository from FooMapStore, but it feels a bit awkward :-)

public class FooMapStore implements MapStore<Long, Foo> {

    @Autowired 
    private ApplicationContext applicationContext;
    private FooRepository fooRepository;

    @Override 
    public void store(Long key, Foo value) {
        fooRepository().findAll();
    }

    ...

    private FooRepository fooRepository() {
        if (this.fooRepository == null) {
            this.fooRepository = applicationContext.getBean(FooRepository.class);
        }
        return fooRepository;
    }

@snicoll
Copy link
Member

snicoll commented Dec 3, 2018

Thanks for the feedback. That's not really what I have in mind. I was more thinking about retrieving the HazelcastInstance later and alter its configuration to register an extra store. Is that a use case that Hazelcast supports?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 3, 2018
@snicoll snicoll added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Dec 3, 2018
@JorgenRingen
Copy link
Author

JorgenRingen commented Dec 4, 2018

I thought about registering the MapStore in a BeanPostProcessor, but according to com.hazelcast.config.Config [Config instances] should not be modified after they are used to create HazelcastInstance.

The creation of the HazelcastInstance bean instantiates it (return Hazelcast.getOrCreateHazelcastInstance(config);) so a BeanPostProcessor is too late. I could also add FooRepository to FooMapStore in a BeanPostProcessor but I think that just adds another layer of confusion compared to looking it up from the ApplicationContext.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 4, 2018
@snicoll snicoll changed the title Hazelcast + spring-boot-data-jpa: cacheManager initialisation issue after upgrade from 2.0.4 -> 2.0.5 Could not initialize Hazelcast with a dependency on a JPA repository Dec 4, 2018
@snicoll
Copy link
Member

snicoll commented Dec 4, 2018

@JorgenRingen that's not what this issue indicates. I've tried and got another Hazelcast problem that I don't really understand:

Caused by: com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable serializer for class com.example.cachemanagercurrentlyincreationerror.FooMapStore
	at com.hazelcast.internal.serialization.impl.AbstractSerializationService.serializerFor(AbstractSerializationService.java:491) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.serialization.impl.AbstractSerializationService.writeObject(AbstractSerializationService.java:254) ~[hazelcast-3.9.4.jar:3.9.4]
	... 44 common frames omitted

I've submitted a PR to your repro project.

@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Dec 4, 2018
@JorgenRingen
Copy link
Author

Interesting. This error only occurs when the mapstore is added in the postconstruct. As far as I understand hazelcast does some kind of broadcasting of the config if the config is updated after initialisation and thats what causing the serialization issue. From stacktrace:

Caused by: com.hazelcast.nio.serialization.HazelcastSerializationException: Failed to serialize 'com.hazelcast.config.MapConfig'
	at com.hazelcast.internal.serialization.impl.SerializationUtil.handleSerializeException(SerializationUtil.java:75) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toBytes(AbstractSerializationService.java:161) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toBytes(AbstractSerializationService.java:137) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toData(AbstractSerializationService.java:122) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toData(AbstractSerializationService.java:110) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.dynamicconfig.ClusterWideConfigurationService.cloneConfig(ClusterWideConfigurationService.java:227) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.dynamicconfig.ClusterWideConfigurationService.broadcastConfigAsync(ClusterWideConfigurationService.java:219) ~[hazelcast-3.9.4.jar:3.9.4]
	at com.hazelcast.internal.dynamicconfig.ClusterWideConfigurationService.broadcastConfig(ClusterWideConfigurationService.java:199) ~[hazelcast-3.9.4.jar:3.9.4]

Notice "broadcastConfigurationAsync".

I'll try to experiment with the deferred registration in the application where the problem originally occurred a bit later. My example-app is over-simplified with regards to hazelcast in order to show the circular reference problem. Might have to do some additional configuration somewhere in order to do updates of the hazelcast-config after hazelcast initialization.

@snicoll snicoll removed the for: team-attention An issue we'd like other members of the team to review label Dec 21, 2018
@wilkinsona wilkinsona added type: bug A general bug and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Mar 20, 2019
@wilkinsona wilkinsona added this to the 2.2.x milestone Mar 20, 2019
@snicoll snicoll modified the milestones: 2.2.x, 2.x Oct 15, 2019
@lukass77

This comment has been minimized.

@snicoll

This comment has been minimized.

@nkavian
Copy link

nkavian commented Apr 17, 2021

I have a similar issue. I want to initialize the Hazelcast Config with one setting from my database, which then I'm hit with the cyclical issue.

@snicoll snicoll removed their assignment Sep 14, 2023
@StefanPopa02
Copy link

Any updates on this? I'm facing a similar issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

8 participants