Access to direct JDBC connection

18 views
Skip to first unread message

Mateusz Fiołka

unread,
Jul 21, 2021, 10:37:19 AM7/21/21
to jDBI
Hi everybody,

Is there a way to get access to raw JDBC connection from JDBI?

I know there is a getConnection method on Handle class, but it returns an object that is a proxy to a pooled connection, there are delegations and wrapping all over. Even if I wanted to access it via reflection it's not trivial to get it.

What's my use case?
I want to create a class that uses Postgres LISTEN/NOTIFY mechanism of pub-sub. To do that I'd like to start a long running thread that opens a single connections and gets notifications in a loop. For that I need a PgConnection object which has a getNotifications method. Which obviously is non standard JDBC interface.

Thanks, 
Mateusz

Steven Schlansker

unread,
Jul 21, 2021, 10:44:37 AM7/21/21
to jd...@googlegroups.com
This isn't really a JDBI question - once you getConnection() then you are back in JDBC land.
But I believe the answer you are looking for is the unwrap() method from the Wrapper interface.
You should be able to call handle.getConnection().unwrap(PGConnection.class), assuming your pool supports it
> --
> You received this message because you are subscribed to the Google Groups "jDBI" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/67365d1e-51ca-472c-9f20-12ee2ac7e316n%40googlegroups.com.

Mateusz Fiołka

unread,
Jul 21, 2021, 10:53:00 AM7/21/21
to jDBI
Thank you Steven, it works exactly as you said.
Reply all
Reply to author
Forward
0 new messages