Skip to content

Commit

Permalink
Fix: Resolved LazyInitializationException in TimeEntry retrieval. (Close
Browse files Browse the repository at this point in the history
 #13)

# Issue
We encountered a `LazyInitializationException` when trying to access the `timeSegmentList` of a `TimeEntry` object that was retrieved from the cache. This happened because the `timeSegmentList` was lazily loaded by Hibernate, and we were trying to access it after the Hibernate Session had been closed.

# Temporary Solution
We resolved this issue by forcing the initialization of the `timeSegmentList` before caching the `TimeEntry` object. We did this by calling `size()` on the `timeSegmentList` in the `getTimeEntryById` method. This ensured that the list was fetched from the database and included in the cached `TimeEntry`.

```java
timeEntry.getTimeSegmentList().size(); // This will initialize the timeSegmentList
```
> ## Note
>  This solution might have performance implications if the timeSegmentList is large, as it will always be fetched from the database even if it’s not needed. Therefore, this solution is not recommended for large data sets. We are currently exploring more efficient solutions.
  • Loading branch information
seyedali-dev committed May 20, 2024
1 parent 3c1d03b commit ec08537
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import com.seyed.ali.timeentryservice.model.domain.TimeEntry;
import com.seyed.ali.timeentryservice.model.domain.TimeSegment;
import com.seyed.ali.timeentryservice.model.dto.TimeEntryDTO;
import com.seyed.ali.timeentryservice.model.dto.response.TimeEntryResponse;
import com.seyed.ali.timeentryservice.repository.TimeEntryRepository;
import com.seyed.ali.timeentryservice.service.cache.TimeEntryCacheManager;
import com.seyed.ali.timeentryservice.service.interfaces.TimeEntryService;
import com.seyed.ali.timeentryservice.util.TimeEntryUtility;
import com.seyed.ali.timeentryservice.util.TimeParser;
import com.seyed.ali.timeentryservice.util.converter.TimeEntryConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
Expand All @@ -28,7 +26,6 @@ public class TimeEntryServiceImpl implements TimeEntryService {
private final TimeEntryRepository timeEntryRepository;
private final TimeParser timeParser;
private final TimeEntryUtility timeEntryUtility;
private final TimeEntryConverter timeEntryConverter;
private final TimeEntryCacheManager timeEntryCacheManager;

/**
Expand Down Expand Up @@ -56,15 +53,19 @@ public TimeEntry getUsersTimeEntry(String userId) {
/**
* {@inheritDoc}
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
@Cacheable(
cacheNames = TimeEntryCacheManager.TIME_ENTRY_CACHE,
key = "#timeEntryId",
unless = "#result == null"
)
public TimeEntry getTimeEntryById(String timeEntryId) {
return this.timeEntryRepository.findById(timeEntryId)
log.info("Db call.");
TimeEntry timeEntry = this.timeEntryRepository.findById(timeEntryId)
.orElseThrow(()-> new ResourceNotFoundException("Time entry with ID: '" + timeEntryId +"' was not found."));
timeEntry.getTimeSegmentList().size(); // This will initialize the timeSegmentList: otherwise we'll get hibernate's LazyLoadingException.
return timeEntry;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.seyed.ali.timeentryservice.service.cache;

import com.seyed.ali.timeentryservice.model.domain.TimeEntry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class TimeEntryCacheManager {

Expand All @@ -15,6 +17,7 @@ public class TimeEntryCacheManager {
key = "#timeEntryId"
)
public TimeEntry cacheTimeEntry(String timeEntryId, TimeEntry timeEntry) {
log.info("Caching timeEntry. TimeEntryId: {} - UserId: {}", timeEntryId, timeEntry.getUserId());
return timeEntry;
}

Expand Down

0 comments on commit ec08537

Please sign in to comment.