Reputation: 1
I got the next error using micronaut data framework
19:53:29.910 [default-nioEventLoopGroup-1-2] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Could not obtain transaction-synchronized Session for current thread
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at io.micronaut.transaction.hibernate.MicronautSessionContext.currentSession(MicronautSessionContext.java:61)
JPA configuration
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: none
repository
@Repository
public interface MyRepository extends
PageableRepository<MyEntity, Long>, JpaSpecificationExecutor<MyEntity> {
}
Service
@Slf4j
@Singleton
@AllArgsConstructor
public class MyServiceImpl implements MyService {
private final MyRepository repository;
@Override
public List<TimeSeriesEntryDto<Long>> getMyEntity(Specification<MyEntity> spec) {
final Optional<MyEntity> result =
repository.findOne(spec); //Executing this line the error pops up
According to Spring Hibernate - Could not obtain transaction-synchronized Session for current thread "You must add @Transactional into your @Repository", probably I lack the knowledge behind this recommendation, also I have not seen any example where this has been done.
Upvotes: 0
Views: 112
Reputation: 1
Adding @ReadOnly fixed the error.
import io.micronaut.transaction.annotation.ReadOnly;
@ReadOnly
public List<TimeSeriesEntryDto<Long>> getMyEntity(Specification<MyEntity> spec) {
final Optional<MyEntity> result =
repository.findOne(spec);
https://guides.micronaut.io/latest/micronaut-jpa-hibernate-gradle-java.html
All database access needs to be wrapped inside a transaction. As the method only reads data from the database, annotate it with @ReadOnly.
Upvotes: 0