Aiden Zhao Aiden Zhao - 1 month ago 11
Groovy Question

Should I close the groovy Sql in Grails service

I'm using groovy Sql in a Grails project.

class MyService{
def sessionFactory
def method(){
def sql = new Sql(sessionFactory.currentSession.connection())
def query =...
sql.rows(query,...)
sql.close()
}
}


I see a lot of examples online about groovy sql in grails. All of them are not including the
sql.close()
in their code.

I'm wondering why. Is it because Grails has already managed the session connection after a service method is executed?
If so can you give me some evidence(or resources) I can refer to?

Answer

This is not necessary. Please refer to Does groovy.sql.Sql.firstRow Closes Connection After Execution?

Take a look at the link provided in the comments of the question by dmahapatro https://github.com/apache/groovy/blob/GROOVY_2_4_X/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java#L1709