Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataBase Utility class consisting of static methods

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Our is a webbased application , i am working on a existing Application , where the code is already developed .

There is a Utillity class for obtaining and closing Database connections , I am somewhat surprised as i am seeing that , all these methods are static , means for example


I am new and i cannot discuss this with Architect .
So please share your ideas



Thanks



 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those utility classes probably just call close() on the passed objects after a null check:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mostly Utility methods are static
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed; there is no need to create an instance of any object just to call these kinds of methods. And Sun uses this technique as well, just check java.util.Arrays or java.util.Collections.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear on what the actual question is.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating classes with utility methods that are all static is a common pattern that's also used in the standard Java library. Examples are classes java.lang.Math, java.util.Arrays and java.util.Collections.

It's not very object-oriented, but nevertheless it's a common thing. I've also written a number of StringUtils classes myself...

When you write such a utility method class, make sure to give it a private constructor so that nobody can instantiate it:

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for asking this question is that , in a web based application if we use static methods to close Database connections ,
then if one Thread (request ) closes the connection as they are static , i think for all the other requests/Threads it will entirely close the Database connection .

Please clarify this .
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter whether the method is static, what you should look at is if there is data that is shared between threads (for example because it is static).

If the code looks like what Rob posted above, then it doesn't matter, because the data (the Connection, Statement and ResultSet objects) are not shared data.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The instance of Connection passed to the close() method is not the same always? So after calling an open() on the Utility class a different Connection is passed to each close() so only that particular instance would be closed.

Cheers,
Raj.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Creating classes with utility methods that are all static is a common pattern that's also used in the standard Java library. Examples are classes java.lang.Math, java.util.Arrays and java.util.Collections.
It's not very object-oriented, but nevertheless it's a common thing. I've also written a number of StringUtils classes myself...


Creating utility classes that can't be instantiated or extended / partially overridden is something to be very cautious about nonetheless.
For something like a StringUtilities class this is perfectly fine, of course, but in other cases it could potentially get frustrating.
I've encountered 'utility' classes like that, which handle (file) I/O, for instance.
Without a very well thought out API and no way to create and inject a test double, writing (unit) tests to cover the code that uses such a utility can be a bit of a pain.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing is the killer. While static classes *can* be mocked, it's an aggravation.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much .

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic