Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design Connection with respect to DataBase Connection

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ??


 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hh
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sick mind


Anyone please
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot sagar .

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RaviNada Kiran wrote:Thanks a lot sagar .

You're Welcome
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method variables are always threadlocal variables, yes.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote: don't put words in other's mouth.

Sorry Sir
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:

Now don't say "looks for what static means


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic