Posts

Showing posts from October, 2017

Spring application events lifecycle and earlyApplicationEvents

Not long ago I learned the hard way the way spring handles earlyApplication events when we started having problems in one of our projectsBasically we were using a MongoDB database as a cache layer, so when the application would startup we would pull most of the data from our RDBMS do some processing and denormalisation then fill the MongoDB with data The application had been running mostly fine for some time but we saw some memory issues as the data kept growing within the application and I suspected a Memory leak issue So I decided to do a java heap dump to try to see what was happening, and I found the culprit; way down the piping of spring the org.springframework.context.support.AbstractApplicationContext class had a private property earlyApplicationEvents that was keeping references to more than 3 million instances of BasicDBObject that were not Garbage collected even though they were no longer in scope and were already written to the databaseAfter scouring the internet a fellow …

Working with stateless session in hibernate and jpa

When doing heavy read-only operations in with the database it can be a good idea to take advantage of using Hibernate's Stateless SessionAs it name indicates this session does not keep state of the entities that it retrieves from the database and so it bypasses the L1 and L2 cacheThis can have significant performance impact on heavy DBA read operations (since the state of the entities is not bet tracked) @Component @Scope("prototype") public class SomeJpaService { public static final int SCROLL_STEP = 1000; @PersistenceContext private EntityManager entityManager; public void doInStatelessSession(){ LOGGER.info("ABOUT to perform Heavy read operation on DB"); StatelessSession statelessSession = entityManager.unwrap(Session.class).getSessionFactory().openStatelessSession(); try (ScrollableResults r = statelessSession .createQuery("SELECT c FROM Order c") .setReadOnly(true)…