ask-dev
ask-dev

Reputation: 636

Groovy SQL DataSource Connection handling, withTransaction

I've been quite confused at groovy SQL documentation as far as the connection handling is concerned. I need to understand the best practices or how to actually handle the underlying connection properly.

Let's say I have an object like this.

SqL sql = new Sql(dataSource);
sql.withTransaction {
 .....
}

Now should I close connection? sql.close in a finally block? Or I just leave it there.

Now consider this:

SqL sql = Sql.newInstance (connection parameters);
sql.withTransaction {...}

Is sql.close() required now?

Now here is another variant.

Sql sql //(with any constructor).
Sql.//[do something] without a withTransaciton ...

Do I have to manage the connection myself this time? Or I can still leave it there without sql.close().

In any of the above case I'm writing my code in a Grails service which may or may not be transactional.

Thanks.

Upvotes: 2

Views: 3436

Answers (1)

jpertino
jpertino

Reputation: 2219

afaik, all methods do the connection handling themselves.
you should deal with it only if you used the constructor that receives a connection.
if you don't need transacions and you want to reuse the connection you colud use Sql#cacheConnection(Closure)

Upvotes: 3

Related Questions