First some background: I'm using a modified version of Spring Security to perform Active Directory authentication and also check for possible access permissions stored in a database. This means that there is a call in a normal Groovy class to load information from the database:
if (Holders.config.loadRolesFromDatabase)
{
Set<DomainClassRole> roles = DomainClassUser.findByUsername(username)?.roles
if (roles)
authorities.addAll(roles.collect({ new SimpleGrantedAuthority('ROLE_' + it.name) }))
}
This was working great with Hibernate 4.3.6.1 and Tomcat 7.0.54, however, after upgrading both (to 4.3.10.18 and 8.0.14.1) it now produces a "HibernateException: No Session found for current thread" exception when calling the dynamic finder method. After doing some research I decided to wrap this code in a withTransaction block:
if (Holders.config.loadRolesFromDatabase)
{
DomainClassUser.withTransaction({
Set<DomainClassRole> roles = DomainClassUser.findByUsername(username)?.roles
if (roles)
authorities.addAll(roles.collect({ new SimpleGrantedAuthority('ROLE_' + it.name) }))
})
}
This fixes the error, however, I'm not really sure why this is required. My current understanding of withTransaction is that it is used to create transactions that can be rolled back in case of an exception, etc. However, I don't need to perform any rollbacks here (it's all read only calls), why would I still need a transaction to perform this call?