I tried to set up multiple datasource with 2 datasources include
datasource1
which is annotated with@Primary
.datasource2
.
then query data by using findAll()
(that belong to @Primary datasource, datasource1) it work without any error.
but got the following error when I called the the findAll()
method that belong to datasource2
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.example.linewebhookjava.entity.datasource2.Projects2.itemUpdates: could not initialize proxy - no Session]
I have tried change @ElementCollection(fetch = FetchType.LAZY)
to @ElementCollection(fetch = FetchType.EAGER)
in Projects2
entity.
or
place this spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
into application.properties
the error was gone.
But the point is why it is fail on datasource2 (secondary datasource).
Conclusion, calling findAll()
that belong to datasource
with @Primary
work fine , but didn’t work on findAll()
of the secondary datasource.
Additionally, I have tried swapping @Primary
annotation with another datasource
.
(Here’s my code with sql and docker-compose.yml
https://github.com/annopud/line-webhook-java/tree/no_session)
anno is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.