Jooq & HikariCP

1,511 views
Skip to first unread message

skr...@gmail.com

unread,
Jan 20, 2016, 2:56:24 PM1/20/16
to jOOQ User Group
Hi guys,

I'm looking for use jooq with HikariCP but im a beginer with that 2 projects.
So is something able to link me a tutorial or examples for this ?

I saw that Jooq has DatasourceConnectionProvider(DataSource source) but how to use it ? I'm confused, should i put my HikariCP Datasource into this constructor ? And then ?

Thank you

Lukas Eder

unread,
Jan 20, 2016, 3:14:24 PM1/20/16
to jooq...@googlegroups.com
Hello,

Thanks for your message. Indeed, that's what you should do. Here are some further hints:

A DataSource is a common abstraction that hides away the creation and management of JDBC Connections. Connection pools use this abstraction to implement the pool semantics. Clients will block on the DataSource.getConnection() call, once the pool is exhausted, until a Connection becomes free again via Connection.close() (at least, that's how it's usually implemented).

jOOQ has its own abstraction to help users inject their Connection management lifecycle: The ConnectionProvider. When jOOQ needs a Connection (a query is executed), it gets the Connection from ConnectionProvider.acquire(). When jOOQ has finished with the Connection, it returns it to ConnectionProvider.release().

The DataSourceConnectionProvider bridges the jOOQ semantics to the common DataSource semantics.

With Hikari, you can simply do, for example:

DSL.using(hikariDataSouce, SQLDialect.H2) // H2 as an example
   .selectOne()
   .fetch();

or, more verbosely:

DSL.using(new DataSourceConnectionProvider(hikariDataSouce), SQLDialect.H2)
   .selectOne()
   .fetch();

Hope this helps
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Romain Pillot

unread,
Jan 20, 2016, 6:56:00 PM1/20/16
to jOOQ User Group
Thank you for you answer lukas!
Anyways, i don't understand when i have to turn off/on auto commits, is it automatly ? I usually make it by myself, making it thread safe with a synchronized object.
And when .release() method is called ? After a fetch() ?

Romain Pillot

unread,
Jan 20, 2016, 6:58:23 PM1/20/16
to jOOQ User Group
Because i don't understand when my connection is closed (jooq close my connection, right ?)..
And i want to use Jinq with jooq, it's possible too ? Jooq Jinq + Hikari ?

Sorry for my questions, i'm a newbie and not too good with enlighs :/ Then i got some problems to understand javadocs

Lukas Eder

unread,
Jan 21, 2016, 4:04:30 AM1/21/16
to jooq...@googlegroups.com
Hi Romain,

2016-01-21 0:56 GMT+01:00 Romain Pillot <skr...@gmail.com>:
Anyways, i don't understand when i have to turn off/on auto commits, is it automatly ?

Usually, you will never run your application with auto commit set to true, but you'll use a transaction layer instead (e.g. Java EE, Spring) or manage transactions manually (e.g. jOOQ, JDBC). The best option is to configure Hikari to provide Connections with auto commit set to false.
 
I usually make it by myself, making it thread safe with a synchronized object.

There's no need to making transactions thread safe, normally. When you check out a Connection from a pool like Hikari, the assumption is that this Connection is used only in a single thread anyway.
 
And when .release() method is called ? After a fetch() ?

As soon as jOOQ doesn't need the Connection (and the PreparedStatement / ResultSet) anymore. This can be after a fetch(), or after a jOOQ transaction.

Lukas Eder

unread,
Jan 21, 2016, 4:07:17 AM1/21/16
to jooq...@googlegroups.com
2016-01-21 0:58 GMT+01:00 Romain Pillot <skr...@gmail.com>:
Because i don't understand when my connection is closed (jooq close my connection, right ?)..

Yes, in DataSourceConnectionProvider.release()
 
And i want to use Jinq with jooq, it's possible too ? Jooq Jinq + Hikari ?

Yes it is. You will find instructions here:
 
Sorry for my questions, i'm a newbie and not too good with enlighs :/ Then i got some problems to understand javadocs

Well, you're using a lot of new concepts / APIs at once :)

Romain Pillot

unread,
Jan 21, 2016, 11:20:24 AM1/21/16
to jOOQ User Group
Thank you !
Hi Lukas :)

But i don't understand why Jooq close my connection. My software will create a connection for each transaction ? I need a database framework for a MMO emulator.
Imagine with 1000 players, Hikari is useless if Jooq close each connection, i can create by myself 1 connection for each transaction lol, but imagine with 1000 players, it will create 1000^X connections / xtime, wtf is this

Romain Pillot

unread,
Jan 21, 2016, 11:29:09 AM1/21/16
to jOOQ User Group
I thought Hikari creates connections and put them in cache. And that connections will be managed by Hikari, meaning it will return a Connection (getConnection()) which isn't full (like 20 transactions per connection).
I don't understand if u know what i mean, or didn't i understand how hikari works ? Fuuuh :(

Romain Pillot

unread,
Jan 21, 2016, 11:34:14 AM1/21/16
to jOOQ User Group
So should i create my own implementation of Provider and then don't close my connection into the .release() method ? And so make it thread-safe etc..

Alexey Belostotsky

unread,
Jan 21, 2016, 11:58:08 AM1/21/16
to jOOQ User Group
HikariCP datasource should give you wrapped connection and when you call close on that wrapper, the underlying connection should be returned to connection pool.

Romain Pillot

unread,
Jan 21, 2016, 2:10:47 PM1/21/16
to jOOQ User Group
Ok so i have to make my transactions thread-safe ?

Romain Pillot

unread,
Jan 22, 2016, 1:01:08 PM1/22/16
to jOOQ User Group
lost :(

Lukas Eder

unread,
Jan 23, 2016, 2:20:35 AM1/23/16
to jooq...@googlegroups.com
Hi Romain,

I'm sorry you feel lost. I can only advise you these things:

- Re-read all the answers we've given so far. I don't think there should be any open questions
- Remove some APIs from your stack (e.g. JINQ) to make things much simpler

If there are still any doubts remaining, please take the time and explain exactly where you're lost. So far, all we could do is *guess* what you're trying to achieve, and why you're worrying about the transaction thread safety.

Lukas

2016-01-22 19:01 GMT+01:00 Romain Pillot <skr...@gmail.com>:
lost :(

Romain Pillot

unread,
Jan 23, 2016, 9:14:43 AM1/23/16
to jOOQ User Group
Ok i think i have understood, so when JOOQ is closing my connection, this connection won't be closed really, it's going to go back into the connection pool. So Hikira create wrapped connections ? Right ?
Thank you guys for your answers !

Lukas Eder

unread,
Jan 23, 2016, 9:23:41 AM1/23/16
to jooq...@googlegroups.com
That's exactly it

2016-01-23 15:14 GMT+01:00 Romain Pillot <skr...@gmail.com>:
Ok i think i have understood, so when JOOQ is closing my connection, this connection won't be closed really, it's going to go back into the connection pool. So Hikira create wrapped connections ? Right ?
Thank you guys for your answers !

--

Romain Pillot

unread,
Jan 23, 2016, 11:44:47 AM1/23/16
to jOOQ User Group
Thank you ! It was really helpful
Reply all
Reply to author
Forward
0 new messages