wood burning stoves*
The moose likes Java in General and the fly likes DataBase Utility class consisting of static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "DataBase Utility class consisting of static methods" Watch "DataBase Utility class consisting of static methods" New topic
Author

DataBase Utility class consisting of static methods

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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




Save India From Corruption - Anna Hazare.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Those utility classes probably just call close() on the passed objects after a null check:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

mostly Utility methods are static
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Sep 29, 2008
Posts: 12617

I'm not clear on what the actual question is.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Mar 02, 2005
Posts: 443
    
    1

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

Joined: Mar 10, 2008
Posts: 1753
    
    7

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.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Testing is the killer. While static classes *can* be mocked, it's an aggravation.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you very much .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataBase Utility class consisting of static methods
 
Similar Threads
How to create a Template for EntityManager in a WebApplication
Appropiate place for creating and closing EntityManagerFactory in Application Managed EntityManager
Database Program - Web application
main(),printwriter and collections
Reconnecting to Database