Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static constructor instantiation

 
Sani babangida
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you demonstrate why you think new Connections are being created?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sani babangida
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic