groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Wagenleitner <john.wagenleit...@gmail.com>
Subject Re: Sql cacheConnection/withTransaction always closes connection
Date Wed, 09 Dec 2015 16:16:42 GMT
On Wed, Dec 9, 2015 at 6:20 AM, Alexander Holand Salgado <
alexander.salgado@otrum.com> wrote:

> Hi,
>
>
> It seems that groovy.sql.Sql's cacheConnection {} and withTransaction {}
> both close the used connection on returning, regardless of the
> surrounding context, and if using a data source the currently used
> connection is set to NULL. E.g. if you do:
>
> sql.cacheConnection { cc ->
>
>     sql.withTransaction { tc -> /* tc is same object as cc */
>
>     }
>
>     /* connection closed/null here */
> }
>
> You'll get an error if trying to execute any database queries using the
> cc-connection after the withTransaction closure.
> Also, for the same reason, you cannot have nested cacheConnection {}
> closures (if you wanted to for some reason).
>
> Looking at the source code for these methods it seems that they both
> purposely ignore the current cacheConnection state during resource
> clean-up and therefore end up closing/nulling the connection regardless
> of context.
>
> To me this seems incorrect - am I missing something here?
>
>

I don't think you're missing something, those methods don't look like they
were designed to be nested (with each other or themselves).   If nesting
were allowed, it doesn't seem like the following could be handled correctly
for both datasource and single connection backed instances.

withTransaction (or cacheConnection) {
    insert A...
    withTransaction {
        insert B...
    }
    // both A and B are committed
}

Mime
View raw message