GeeCON Prague 2014*
The moose likes JDBC and the fly likes Design Connection with respect to DataBase Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Design Connection with respect to DataBase Connection" Watch "Design Connection with respect to DataBase Connection" New topic
Author

Design Connection with respect to DataBase Connection

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528

Hello Ranchers ,


I am planning to write a Utility class for opening and closing DataBase connections for the Application.

The scenario is my application would be a web application where simultaneous requests may occur .



what will be the pros and cons if i make the getConnection() static ??



If you want something you never had do something which you had never done
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Depends on the rest of your design.

You know what 'static' does, do you?

I would suggest you to carefully read this tutorial how to do the DAO stuff properly: http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
hh
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
RaviNada Kiran wrote:I know what static does mainly useful in Singleton classes .

Uh. OK.

Do you know what it does?

I gone through your tutorial but could not get the concept.
Do not scan the article. Read the article.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
I think that you are referring to the Thread Safety on to the multithreaded environment.


so you mean to say that

If i make it as static then all the Threads will be acting on it . so don't do it .

Am i correcst??

Hoping a reply .
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Oh sick mind


Anyone please
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

RaviNada Kiran wrote:
If i make it as static then all the Threads will be acting on it . so don't do it .

Yes, generally, the the use of static methods is not recommended in web app, because its multi-threaded environment, any request may trying to call getConnection(), method asynchronously, which can cause a null connection object return


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks a lot sagar .

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

RaviNada Kiran wrote:Thanks a lot sagar .

You're Welcome
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
That's a bad answer. Java is multithreaded at any way, static or not. The only what controls threading is the 'synchronized' modifier, but that has completely nothing to do with this question.

As said, learn what 'static' means. Go through a basic Java book/tutorial.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Bauke Scholtz wrote:That's a bad answer.

Sorry, I'm weak in Java
Bauke Scholtz wrote:
Java is multithreaded at any way, static or not. The only what controls threading is the 'synchronized' modifier, but that has completely nothing to do with this question.


Ok,
Original question:
what will be the pros and cons if i make the getConnection() static ??

I replied:
Yes, generally, the the use of static methods is not recommended in web app, because its multi-threaded environment, any request may trying to call getConnection(), method asynchronously, which can cause a null connection object return


Now, What 'static' means to me:
Its one per class and can be accessed as ClassName.staticMethod(), that's means its Global method.
Now as author said its an web app, and multiple thread may call it simultaneously, like

[Lets consider the same example posted by author]


As we can see here, when ThreadOne returns the "conn" object, it becomes null because of the asynchronous
access by ThreadTwo. [I had mentioned one term asynchronously, in my previous post].
Yes, we can avoid this, by using some mutex for this static method.

Please Note, This explanation assumes the original posters question.

Some other meanings of static to me:
1. Class only method, so its implicitly final
2. Yo can't override this method in sub class, but can be hidden/shadowed by same method in sub class
3. Static methods only access static variable or method only.
4. Static instance variable persists still JVM exists, it adheres to the last assigned value.

If I'm wrong in somewhere explaining, Pleas correct me, I liked to know the correct answer from experts


Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
How would it behave if you don't declare it static with regard to threading?

Yes, it is not different! Again, you're confusing it with the sychronized modifier.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Bauke Scholtz wrote:How would it behave if you don't declare it static with regard to threading?

If you don't declare it static, then you need to create an object of that Connection class and call the getConnection() method, like


Here, each thread[a new request for that servlet] create different MyConnection object having different connection object, each time. [Considering there is no DBCP used]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
That's not the point.

How is the difference in concurrency if two threads call MyConnection.getConnection() OR myConnection.getConnection() concurrently? There is NO difference. You're confusing it with the synchronized modifier.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Bauke ,

As you said "when ThreadOne returns the "conn" object, it becomes null" .Its absolutely a very good point.

Then why the Hibernate utility classes which gets access to Session are of static type ??

Can you please help.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
That was not mine. And that makes absolutely no sense. The 'conn' is not a class variable, it is a threadlocal variable, so this problem won't occur. Sagar is confusing 'static' modifier with the 'synchronized' modifier and also between class/instance/threadlocal variables.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Lets put synchronized thing aside.
Bauke Scholtz wrote:The 'conn' is not a class variable, it is a threadlocal variable, so this problem won't occur.

Now, It means a separate stack frames are created every time a method is invoked, no matter its static or not ? If the answer is 'Yes' then I need to brush up my basics
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Method variables are always threadlocal variables, yes.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ok, that's wrapped up my story, but still the original question remains unanswered, Why the use of static in this method is discouraged?

Now don't say "looks for what static means", I went through the statics-logy and nothing seems to be wrong in this context
Just share your wisdom , please.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
I didn't said that. I just said "Depends on the rest of your design". As the answer on that is never been given, I can't give a more suitable answer. Please read posts carfully and don't put words in other's mouth.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Bauke Scholtz wrote: don't put words in other's mouth.

Sorry Sir
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Sagar Rohankar wrote:

Now don't say "looks for what static means


 
GeeCON Prague 2014
 
subject: Design Connection with respect to DataBase Connection