开发者

Java: A synchronized method is accessed several times by reflection?

I've a very odd behaviour on one of my class, and don't know exactly what is happening.

1) I've a JSP which sends a request to a Servlet with AJAX 2) The servlet process this request in the following way: - It makes a reflection of a class, and then call a method (which is supplied as a parameter from the JSP), it then output the result of the method back to the Ajax.

So here are the original problem, and then the partial solution I came up with:

The call to the method was being done several times, but I don't know why. The JSP-Ajax only call it ONCE (tested many times), but the servlet was like "reloading itself" and calling the method several times, thus, causing errors in the process. Since I couldn't find what was going on with the servlet, I made the "reflected" method synchronized, so no matter how many times it got requested, one request will finish and then the other, and so on. I put a couple of messages to the output, writing when the method was accessed and when it finished the job.

But... the message that signals it's been accessed, is written several times before the end message is thrown... so I don't understand, maybe synchronization doesn't work well with reflection? What am I missing, why the method is accessed several times before it finishes? Simply don't get it. Can you help me?

Also, is you have any ideas for the original issue (servlet like "reloading" itself)? In this problem I'm thinking that maybe a header is sent that makes the开发者_如何学Go servlet to reload, but don't know for sure.

EDIT:

Actually I'm doing an instance. This is my servlet code:

Class clase = Class.forName("com.cargaporinterfase.CargaPorInterfase_"+cd_matriz); Object obj = clase.newInstance(); Method met = clase.getDeclaredMethod(metodo, new Class[]{String.class, String.class});

Ant the method being called is:

public synchronized String procesar(String url,String nu_spid) throws CargaPorInterfaseException{ //... more processing }


You might want to get more background on what synchronized does.

I made the "reflected" method synchronized, so no matter how many times it got requested, one request will finish [...]

synchronized on a method is the same as synchronized(this) {...}, where this is the object on which you're calling the method.

It's quite possible that you are working with multiple distinct instances of the same class (on which you're using reflection), in which case synchronized wouldn't work as you seem to think. In addition, synchronizing on the reflected object is completely different to synchronizing per request (I presume you'd have to synchronize on your Servlet instance, although you might also have multiple instances in some cases.)


Some assumptions:

When you say "It makes a reflection of a class" I'm guessing that means you're using reflection to create a new object.

I'm guessing the methods you're calling are instance methods (not static).

So if those guesses are right, making the method you call through reflection synchronized didn't prevent the calls from overlapping because you were calling the method on a different object each time. If you add the synchronized keyword to a method definition, it uses the object the method is called on as its lock.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜