This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes static constructor instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static constructor instantiation" Watch "static constructor instantiation" New topic
Author

static constructor instantiation

Sani babangida
Greenhorn

Joined: Jan 31, 2007
Posts: 16
I have a Problem and would be grateful for any suggestion I have a class

class apple(){

static Connection gh;
public apple(){
if (gh==null)//if gh=null get new connection that is only instantiate new connection if connection=null
gh=new Connection();

}


}

and i have class b
**************************************************************************
class b{


void shout(){
apple a=new apple();//instantiate the apple class and get connection
}//end method



}

and another class c
**********************************************************************
class c{
void doSomething(){
apple a=new apple();//instantiate the apple class and get connection
}

}


The Problem is instead of reusing an allready instantiated connection..the two application keep
instantiating new connections. And i am using "static" for the connection variable what can be the issue here, any suggestion would be welcome
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
The classes are not making a new connection object everytime. It's only initialized once. As you have used a static variable (gh) everybody would be using the same gh.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Can you demonstrate why you think new Connections are being created?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The use of the static variable is not thread safe, so it's actually possible for more than one connection to be opened, but not every time. Is the problem just that you see more than one connection, or do you really see one every time?

Also, note that static variables are per-ClassLoader. If your classes are loaded by more than one ClassLoader, as in a Servlet container, you could see multiple connections. And of course if there's more than one JVM involved, each JVM has its own copy(ies) too.


[Jess in Action][AskingGoodQuestions]
Sani babangida
Greenhorn

Joined: Jan 31, 2007
Posts: 16
Originally posted by Keith Lynn:
Can you demonstrate why you think new Connections are being created?


I entered a System.out.println at the end of every instantiation to obtain a connection
if (gps==null ){
try {
gps=weba.getWebServiceAgent();
System.out.println("WebService Constructor Executed");
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();

and on the console i always get

***********************************************************
WebService Constructor Executed
Using Web Services************************************
WebService Constructor Executed
Using Web Services************************************
WebService Constructor Executed
.
\(^_^)GALC/ Complete to start Application.
Using Web Services************************************
Using Web Services************************************
Using Web Services************************************
WebService Constructor Executed
Using Web Services************************************
WebService Constructor Executed
Using Web Services************************************
Using Web Services************************************
Using Web Services********************************

In this case you would notice the connection is executed 5 times
Sani babangida
Greenhorn

Joined: Jan 31, 2007
Posts: 16
Originally posted by Ernest Friedman-Hill:
The use of the static variable is not thread safe, so it's actually possible for more than one connection to be opened, but not every time. Is the problem just that you see more than one connection, or do you really see one every time?

Also, note that static variables are per-ClassLoader. If your classes are loaded by more than one ClassLoader, as in a Servlet container, you could see multiple connections. And of course if there's more than one JVM involved, each JVM has its own copy(ies) too.


i think thats the problem , Two of the classes that call the apple class to get a connection ,it seems are using different classLoaders. Is there a way i can get around this. I originally tried without the "static" but when i could'nt solve the issue that was the reason why i felt adding "static" might help. Any suggestions would be welcome
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Basically, the way to get around it is to make sure the class that holds the connection cache is only loaded by one classloader. Turn connection-caching into a sort of "service" that all web apps can use, put it into its own library, and put that library into the container's common library area. If we're talking servlets in Tomcat, for example, you'd want to put your connectionCaching.jar into $TOMCAT/common/lib . Then your static trick will work as you expect.

Of course, there's no need to write your own: connection pooling is something everybody does, and there are many implementations available; look around, and just use one.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Sani babangida:
...The Problem is instead of reusing an allready instantiated connection..the two application keep
instantiating new connections. And i am using "static" for the connection variable what can be the issue here, any suggestion would be welcome


Two applications? You are using this from two separate JVMs?
Sani babangida
Greenhorn

Joined: Jan 31, 2007
Posts: 16
Originally posted by Mr. C Lamont Gilbert:


Two applications? You are using this from two separate JVMs?


Actually i meant two different classes. Two different swing Classes are using one class for connection to a web services .one is loaded via class loader and they keep instaintiating connection to the web service which is not very efficient
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static constructor instantiation