Can I access current thread-local java.sql.Connection?

129 views
Skip to first unread message

Dmitry Grigoriev

unread,
Mar 21, 2010, 6:51:40 AM3/21/10
to Squeryl
Hello Maxime,

I need to do some DDL and would like to leverage connection already
managed by Squeryl, to avoid passing it as extra argument. Is Subj
possible?

Dmitry Grigoriev

unread,
Mar 21, 2010, 8:39:00 AM3/21/10
to Squeryl
In fact, this is the use case I need quite often when working with
databases of dynamic structure. For example, consider some sort of
hierarchical catalog where admin can configure structure (visible
properties list) of each category. This obviously requires executing
ALTER TABLE along with editing category metadata, in the same
transaction. So the problem is much deeper than just setting up and
passing around another connection for DDL.

So could you please add some static method like
Session.currentConnection(): java.sql.Connection, which would throw if
outside transaction? I need this one urgently. :(

Maxime Lévesque

unread,
Mar 21, 2010, 10:20:21 AM3/21/10
to squ...@googlegroups.com

 You can do this already with

  Session.currentConnection.connection

Regarding altering the schema within a transaction, some
databases perform a commit of the current transaction
when any DML is invoked (ok, I'm talking of oracle, ... it could be the only
one doing this !;-))

 So you *might* want to do this in a dedicated transaction
(simply be wrapping the DML inside a transaction {} block),
as opposed to using the current one.

 Cheers



To unsubscribe from this group, send email to squeryl+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

Dmitry Grigoriev

unread,
Mar 21, 2010, 11:18:06 AM3/21/10
to squ...@googlegroups.com

> You can do this already with
>
> Session.currentConnection.connection
Great, thanks! :)

> Regarding altering the schema within a transaction, some
> databases perform a commit of the current transaction
> when any DML is invoked (ok, I'm talking of oracle, ... it could be
> the only
> one doing this !;-))
>
> So you *might* want to do this in a dedicated transaction
> (simply be wrapping the DML inside a transaction {} block),
> as opposed to using the current one.

Nooo... This time I meant exactly what I said. In the example I
explained, both "update category_metadata where id=N" and "alter table
category_N" form a single atomic logical operation.

BTW, it's a great benefit of PostgreSQL that it correctly logs DDL
operations along with DML. Besides my current concern, this allows
complex automatic database schema upgrades without the need to manually
backup it first and manually restore backup if update fails... I don't
know anything about Oracle, but I know how terrible and dangreous MySQL
is (or was) about DDLs in transactions.

> Cheers
Yeah, 2 you too. :)

Reply all
Reply to author
Forward
0 new messages