Synchronized block is accessed by other jsps

B

biswa

HI.
I have a synchronized block where i am creating a temp table and
deleting it in the same method after processing. I ve to make it a
synchronized to avoid other requests from accessing the temp table
while the other request is processing.

But strangely, i can see more than one requests entering to the method
at the same time. If there are two requests at the same time, one
tries to delete the temp table while other is accessing.

How is it possible although the method is synchronized it is accessed
by more than one request at the same time. I am calling the mothod in
JSP file. Am i missing something?

Any help would be great.

Thanks.
 
M

Michael Borgwardt

biswa said:
I have a synchronized block where i am creating a temp table and
deleting it in the same method after processing. I ve to make it a
synchronized to avoid other requests from accessing the temp table
while the other request is processing.

But strangely, i can see more than one requests entering to the method
at the same time. If there are two requests at the same time, one
tries to delete the temp table while other is accessing.

How is it possible although the method is synchronized it is accessed
by more than one request at the same time. I am calling the mothod in
JSP file. Am i missing something?

On what are you synchronizing? synchronized instance methods synchronize
on the instance, and a container might well have a separate instance
of the servlet for each request.
 
F

Frank Gerlach

HI.
I have a synchronized block where i am creating a temp table and
deleting it in the same method after processing. I ve to make it a
synchronized to avoid other requests from accessing the temp table
while the other request is processing.

But strangely, i can see more than one requests entering to the method
at the same time. If there are two requests at the same time, one
tries to delete the temp table while other is accessing.

How is it possible although the method is synchronized it is accessed
by more than one request at the same time. I am calling the mothod in
JSP file. Am i missing something?

Any help would be great.
Post a short snippet of your code. Most probably you are synchronize()ing
on two DIFFERENT object instances.
 
J

John C. Bollinger

biswa said:
I have a synchronized block where i am creating a temp table and
deleting it in the same method after processing. I ve to make it a
synchronized to avoid other requests from accessing the temp table
while the other request is processing.

I'm not sure what you mean by "creating" a temp variable. Are you
talking about a JSP scripting variable? In that case you should be
using a request-scope, or perhaps even a page-scope variable instead of
a session- or application-scope variable. That will eliminate any need
to synchronize.

Or are you talking about a Java variable in scriptlet code? In that
case make it a local variable by declaring it in normal scriptlet code (
e.g. <% int localVariable = 0; %> ) instead of a an instance variable,
which is what you get by using a JSP declaration (e.g, <%! ... %>).
That will also remove any need to synchronize.


John Bollinger
(e-mail address removed)
 
B

biswa

I have the synchronized block in my class file
public class MyClass {

public synchronized boolean myFunc() {

//--> Table created
//--> Table accessed
//--> Table dropped
}
}

I am calling this method in the JSP file
This is my code in jsp file.
<%
MyClass myClass = new MyClass();
//.. some codes here

if(myClass.myFunc())
//do something
else
//do something

//-- some codes here.
%>

myClass.myFunc() is accessed by more than one request at same time
although the method is synchronized in the java file. I do not
understand why.

But to test I kept the method in a synchronzied block within Jsp file
and i do not see two requests accessing myFunc() any more.
new code looks like this
<%
MyClass myClass = new MyClass();
//.. some codes here

synchronized(this) {
if(myClass.myFunc())
//do something
else
//do something
}
//-- some codes here.
%>

Is the 2nd method correct way of synchronizing a method to access in
JSP file?? if so i have to change all my JSP files :((

thanks
biswa.
 
J

John C. Bollinger

biswa said:
I have the synchronized block in my class file
public class MyClass {

public synchronized boolean myFunc() {

//--> Table created
//--> Table accessed
//--> Table dropped
}
}

I am calling this method in the JSP file
This is my code in jsp file.
<%
MyClass myClass = new MyClass();
//.. some codes here

if(myClass.myFunc())
//do something
else
//do something

//-- some codes here.
%>

myClass.myFunc() is accessed by more than one request at same time
although the method is synchronized in the java file. I do not
understand why.

As others wrote already, the method is being invoked concurrently on
_different instances_ of your class. The method is synchronized on
"this", so synchronization does not prevent that. To get
synchronization working as you hope, you need all instances to
synchronize on a common object. You can achieve that by making the
method static (and still synchronized) if that is feasible, or by
providing a static member that myFunc() synchronizes on. (E.g.:

public class MyClass {

private final static Object MY_FUNC_LOCK = new Object();

[...]

public boolean myFunc() {
synchronized(MY_FUNC_LOCK) {
[...]
}
}
}

)
But to test I kept the method in a synchronzied block within Jsp file
and i do not see two requests accessing myFunc() any more.
new code looks like this
<%
MyClass myClass = new MyClass();
//.. some codes here

synchronized(this) {
if(myClass.myFunc())
//do something
else
//do something
}
//-- some codes here.
%>

That will work only as long as nothing else (no other page, no third
object, etc) tries to invoke MyClass.myFunc(). It is a kluge, not a
solution.
Is the 2nd method correct way of synchronizing a method to access in
JSP file?? if so i have to change all my JSP files :((

No, it isn't. See above. But better by far than getting
synchronization right is to avoid the need for synchronization in the
first place. In your example it ought to be possible to write
MyClass.myFunc() so that concurrent execution is possible, since it does
not make any persistent changes to application state. That is what I
recommended to you in the first place.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top