Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JDBC connection pools singletons?

I am learning about JDBC and I am currently looking at JDBC DataSource and connection pooling. Do you need to ensure that the datasource you create is only created once in the application and then share it with all classes that needs a reference to a datasource? Or does it do something magic behind the scenes?

If you configure a datasource in a Java EE server or in Spring does it create only one datasource object? Are objects in JNDI singletons, or do you get a new each time you require one?

like image 950
LuckyLuke Avatar asked Mar 21 '26 03:03

LuckyLuke


1 Answers

DataSource instances aren't magic. If you create two of them, you'll have two, and each will have its own pool of connections. With JNDI, you register an object under a name, so by definition, that same object will be given out every time. You can think if it more or less as an overblown HashMap. In Spring, singleton is the default scope, so a single DataSource would be created and shared between everything that uses it unless you change the bean's scope.

like image 129
Ryan Stewart Avatar answered Mar 23 '26 18:03

Ryan Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!