I've gotten used to the following paradigm, and I just wanted to get another opinion as to if it's safe or not:
Let's say I have a controller (myController) in the web tier of my app, that calls a static method on a generic utility class (myUtilClass) that in turn calls a stored procedure. Assuming one JVM here. So:
public class myController {
...
myUtilClass.deleteTrans(id);
}
public class myUtilClass {
public static void deleteTrans(String id) throws Exception {
//call stored proc "deleteTrans"+idx;
}
}
Is this safe to do? Or is it possible that my input value could get clobbered by a subsequent call to this method before the first one completes? I'd rather not put the 'synchronized' keyword in front of the static method if I do not have to.
-
Calling static methods from the web tier (7 messages)
- Posted by: Keith Craw
- Posted on: July 02 2004 11:04 EDT
Threaded Messages (7)
- Calling static methods from the web tier by Race Condition on July 02 2004 12:18 EDT
- Calling static methods from the web tier by Steven Wang on July 02 2004 12:42 EDT
- Static methods a bottleneck? by Stefan Zobel on July 03 2004 17:21 EDT
-
Static methods a bottleneck? by Race Condition on July 04 2004 02:41 EDT
- Static methods a bottleneck? by Stefan Zobel on July 04 2004 04:52 EDT
-
Static methods a bottleneck? by Race Condition on July 04 2004 02:41 EDT
- Calling static methods from the web tier by Scott Carlson on July 02 2004 15:26 EDT
- No problem by Rakesh Malpani on July 02 2004 18:19 EDT
-
Calling static methods from the web tier[ Go to top ]
- Posted by: Race Condition
- Posted on: July 02 2004 12:18 EDT
- in response to Keith Craw
I don't have a definitive answer but I have always avoided static methods because they are a system bottleneck. If a method is static there is only one instance of that method in the JVM. Every call to that method is goes through the one instance. If it is not synchronized I think there could be thread trouble. Synchronizing the method eliminates the potential threading problem but creates the bottleneck.
Someone please correct me if I'm wrong. -
Calling static methods from the web tier[ Go to top ]
- Posted by: Steven Wang
- Posted on: July 02 2004 12:42 EDT
- in response to Race Condition
Static method should only be used for stateless, non-synchronized situation
and also, the static calss/method should not need any initialization
if can not meet the condition above, I would use instance level method. - after all, now object instantiation is not that expensive, and you can cache your object.
Static method should be a place only be used for hold a collection of related stateless functions. -
Static methods a bottleneck?[ Go to top ]
- Posted by: Stefan Zobel
- Posted on: July 03 2004 17:21 EDT
- in response to Race Condition
... I have always avoided static methods because they are a system bottleneck. If a method is static there is only one instance of that method in the JVM. Every call to that method is goes through the one instance.
I've heard that several times and I think it's plain wrong. Of course, there's only one "instance" of the method, but the same is true for real instance (object-level) methods: they simply have an additional unvisible "this" parameter that identifies the object instance they are applied to. So, as long as a method is stateless, a static method is actually slightly faster, since it is faster to call (no virtual call, no additional "this" parameter on the stack frame).
Correct me, if I'm wrong:)
Kind regards,
Stefan -
Static methods a bottleneck?[ Go to top ]
- Posted by: Race Condition
- Posted on: July 04 2004 02:41 EDT
- in response to Stefan Zobel
Well, I'm not commenting on whether a static method is any faster or slower than a non-static method. I don't know. I agree that a syncronized static method could be fast. But it seems to me that if several threads were lined up waiting for a turn, it could become a bottleneck and slow the system down.... I have always avoided static methods because they are a system bottleneck. If a method is static there is only one instance of that method in the JVM. Every call to that method is goes through the one instance.
I've heard that several times and I think it's plain wrong. Of course, there's only one "instance" of the method, but the same is true for real instance (object-level) methods: they simply have an additional unvisible "this" parameter that identifies the object instance they are applied to. So, as long as a method is stateless, a static method is actually slightly faster, since it is faster to call (no virtual call, no additional "this" parameter on the stack frame).Correct me, if I'm wrong:)Kind regards,Stefan
Who can answer this? -
Static methods a bottleneck?[ Go to top ]
- Posted by: Stefan Zobel
- Posted on: July 04 2004 16:52 EDT
- in response to Race Condition
Synchronizing the method eliminates the potential threading problem but creates the bottleneck.
But it seems to me that if several threads were lined up waiting for a turn, it could become a bottleneck and slow the system down.
Ok, I got it. I didn't read you original post as accurately as possible. Sorry :)
Kind regards,
Stefan -
Calling static methods from the web tier[ Go to top ]
- Posted by: Scott Carlson
- Posted on: July 02 2004 15:26 EDT
- in response to Keith Craw
Your String id is put onto the stack when the method is executed. Any other call to the same method would also put their value on the stack. The two stacks do not overlap, and a static method can use any parameters without worrying.
I agree with Steven, but make sure that you still seperate the logic from your controller.
Hope that helps. -
No problem[ Go to top ]
- Posted by: Rakesh Malpani
- Posted on: July 02 2004 18:19 EDT
- in response to Keith Craw
As rightly said, any function call has its own variable instances.. doesn't matter if its staitic or not, its only the static class member variables which would have such a problem of being shared between multiple function calls... and regarding the consideration mentioned about using it in the first place, I too agree to that, use it only if you need it that way.
Hope this helps.
Regards,
Rakesh.