Forums Register Login

static constructor instantiation

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Can you demonstrate why you think new Connections are being created?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2832 times.
Similar Threads
serializable problem
plz explain and tell out put
Access modifiers
Getting it right from the beginning : Object vs Reference
Overloaded method question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:08:32.