Hey everyone, we recently ran into an issue relate...
# questions
u
Hey everyone, we recently ran into an issue related to this GitHub ticket: https://github.com/apache/grails-core/issues/11376. Could someone provide more details? I’d like to better understand how Grails handles this internally.
j
Its been awhile since I have used Grails 4; but later versions of grails (because of hibernate) ditched the ability to have a session without a transaction. Effectively a new transaction is a new session.
the error usually occurred because you have a domain object that expects to call hibernate apis and there isn’t a transaction so it errors
u
I'm currently using Grails 5.3.5, and the service is annotated with
@Transactional
. I had declared a method called
getAccessToken
, and initially I noticed that the transactional context was lost when calling it. But when I renamed the method to
getAccessTokenByScope
, it started working properly and the transaction was preserved. Not sure if this behavior is related to Grails/Groovy conventions around
get*
methods — do you happen to know why this might be happening?
j
I would suggest creating a sample app to see of you observe the same behavior, but I can’t think of an obvious reason that would fail
a
After reading the comments, it seems like @Transactional at the class level still only sets default transactions on the non-Java Bean methods (the ones that aren't getters or setters), so I'm guessing your service is named AccessTokenService? And Grails is making a getAccessToken() method for you behind the scenes that you're overriding.
Copy code
@Transactional
AccessTokenService {
    AccessToken accessToken
    
    //Needs an explicit @Transactional annotation because (maybe?), by "Grails magic" there is a default getter on the service that will delegate to AccessToken.get() when the Service name and Domain Class name match(??) 
    @Transactional
    AccessToken getAccessToken() {
        ...
    }

    //Gets a transaction from the class-level @Transactional annotation, because its name doesn't match the service name or domain class name and so isn't a "bean method"
    AccessToken getAccessTokenByScope() {
        ...
    }
}
100% just guessing. I haven't tried it out, I haven't looked at what Grails actually does internally, and I'm not a contributor to the framework. ``````
j
That would make sense