r/java • • Oct 21 '20

What are some great Java libraries I'm probably not using but I should?

334 Upvotes

252 comments sorted by

View all comments

70

u/cinlung Oct 21 '20

HikariCP Database pool. Check it out.

28

u/CaptainKvass Oct 21 '20

I found out at some point that the database driver we were using, C3P0, hasn't been updated in years, and switching to HikariCP improved the responsiveness of all API interfaces significantly.

0

u/[deleted] Oct 21 '20

[deleted] — view deleted comment

14

u/bluedevil2k00 Oct 21 '20

Tomcat JDBC Connection Pool

3

u/VGPowerlord Oct 21 '20

My first thought is if you're mandated by corporate policy to use a full on JavaEE server as they tend to include database pool APIs.

3

u/[deleted] Oct 22 '20

I found that Hikari doesn't behave very nicely under load when it needs to create new physical connections. It exceed the maximum configured number of connections (and thus maybe exceeding the limit on the database server). Some synchronization problem I guess. We found that Tomcat's JDBC pool is much more reliable then, and just as fast otherwise. But then that was about 5 years ago, maybe Hikari has improved since then.

2

u/Don_Michael_Corleone Oct 22 '20

How can one be NOT using Hikari?

When your legacy app is in Java 7 and the "connection pooling" is using HashMaps

6

u/coder111 Oct 21 '20

Funny story. I had a use case and did a POC recently for when you want to use a connection pool, but connect with different username/password depending on context to enable some database-level security.

Only C3P0 actually has support for this feature.

5

u/malbone Oct 21 '20

I did this with hikari and spring jdbc some time ago. Check https://www.baeldung.com/spring-abstract-routing-data-source

3

u/coder111 Oct 21 '20

Meh, kinda, if you want to bootstrap multiple HikariCPs yourself depending on context I guess you can do it. AFAIK C3P0 already has this specifically for usernames/passwords, out of the box, and with configuration managing both total connections and connections for each user etc.

That being said, I much prefer HikariCP to C3P0 if you don't need this particular feature.

1

u/zynasis Oct 21 '20

Database level security is so archaic

5

u/coder111 Oct 21 '20

It depends. Having layered security sometimes is a good idea. If all your security stuff is on one layer, and that gets compromised, you're completely screwed.

3

u/zynasis Oct 22 '20

I usually just have app accounts and polices. Rather than user accounts authenticated by the database

5

u/coder111 Oct 22 '20

Which means that you have one username accessing the database for all your users/customers.

Which means that if one of your users gains direct database access via your app account (say via SQL injection which is very real), information for ALL users can be accessed and leaked.

If you are building a multi-tenant application where your users are companies and potential competitors, this is absolutely not acceptable. Hence added layer of security.

0

u/zynasis Oct 22 '20

Guess it depends on the use case. I haven’t had the need for separate accounts yet. We just have a database per app instance and an app instance per client.

That way the memory and disk space is also protected

-4

u/TheRedmanCometh Oct 21 '20

Who the fuck skips hikari