Hi,
I am maintaining an application that is using Hibernate 5.4.21.Final on JDK 1.8 with a REST interface. As a backend we have a MySQL db. Approx 85 GB of data.
Now we are experiencing performance problems in production. Storing/fetching data slows down. Sometimes database connections are closed and the application throws exceptions related to it.
I have worked a lot with RDBMS and JDBC but not with Hibernate. But I suspect that there inefficiencies in the fetching of data that can be very costly as data grows.
Q1: I have read that code annotations like this:
"@OneToMany(mappedBy = "productRevision", cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)"
Could affect the perfomance and could be replaced with something with @NamedEntityGraph.
@NamedEntityGraph(name="graph.productRevision.levels",attributeNodes =@NamedAttributeNode(value="levels"))
So FetchType.LAZY should not be used. Since the line:
"@OneToMany(mappedBy = "productRevision", cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)"
is removed I guess I don’t need to care about the cascade.
Then in my ProductRevisionDao I add the following:
private ProductRevision getProductRevisionById(Long revisionId) {
//Using EntityGrap to make query to avoid n+1 problem.
EntityGraph graph = entityManager.getEntityGraph(“raph.productRevision.levels”);
Map<String, Object> hints = new HashMap<>();
hints.put(“javax.persistence.fetchgraph”, graph);
return getById(ProductRevision.class, revisionId,hints);
}
Will this increase the performance in the application?
Q2: I can see that we do:
entityManager.merge(entity)
and then call flush afterwards. Like
Session session = getHibernateSession();
session.flush();
I have read that there is no need to use flush since hibernate handles it.I have read that it can make application slow. Can anyone confirm it?
Can I remove it?
I also checked the db log and can see that there is only a few insert but there is a lot of find/select.
Q3: We used Java Flight Radar to profile the application. But there was really nothing strange. No thread problem. However I thougth there was a lot of threads called:
http-nio-8080-exec-#
And when I checked with Java Mission Control I could see that they try to connect to db for some time.
I just know that this is “WorkerThreads” that are spawned for each request for REST to make it responsive. I guess these worker threads runs the queries to db.
I would appreciate if you, more experienced hibernate developers,could assist me with comments and advise.
br,
//mike