aspose file tools*
The moose likes Threads and Synchronization and the fly likes synchronized Problem - variable return problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized Problem - variable return problem" Watch "synchronized Problem - variable return problem" New topic
Author

synchronized Problem - variable return problem

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 442
hi, i have 2 methods , which use synchronized, and both methods share a variable as following template code :



the problem here is when i set the list which in this case to size=2, but how come when i retrieve back the list using getParamList() and the size=0 ?

can anyone spot the problem , thanks for guidance

note :: i run this as utility class in my web application, which i start the method with simpleSQL() first then only generalQueryMethod()
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There's nothing wrong here, but you're obviously not showing the real code (the line

resultList = generalQueryMethod(String _query)

gives that away.) We can't find problems if we don't see the real code!

Nothing you've shown here involves multiple threads, so nothing requires these methods to be synchronized (of course, nothing requires "list" to be a member as opposed to a local, either, so again you're not showing us the whole picture.


[Jess in Action][AskingGoodQuestions]
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 442
sorry for unclear, here is my real sample code, it might too long to show here, that's why i just give a scenario, and i take out the exception part









[ November 09, 2005: Message edited by: Nakata kokuyo ]
[ November 09, 2005: Message edited by: Nakata kokuyo ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Maybe you're confused about where the code is going? There are a lot of variables and methods with similar names here, and maybe you're looking at the wrong ones. I know it's hard for me to find my way around. For example, you show the method

public synchronized ResultSet getSqlResultSet(String _sql) throws NpException

but it isn't called anywhere in your example code; you don't show

List getSqlResultList(ResultSet rs)

which is called from getDefaultSalesmanInfo(); in fact, it's the only thing called after setSqlParam(), so it's the only thing that would have a chance to report the problem you're seeing. Take a good look at getSqlResultList() and see if that doesn't clear things up...
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 442
getSqlResultList() code


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, so now, in the call tree starting from getDefaultSalesmanInfo(), you're never calling getSqlParam(). So how do you know that the member sqlList is of length 0?
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 442
the getSqlParam() call from getSqlResultSet(), which is pass to another list

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Right, but that's [before you call setSqlParam(). Of course the list is still empty at that point; you haven't set it to anything yet.
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 442
Ernest, you are right , i very disappointed with such stupid mistake,

anyway, when we use synchronized method, should we put all method as synchronized ?

for example, should i get rig on synchronized in getSqlResultSet() method? the reason i think on get rid it, is due to the getSqlResultSet() might share by other method..

say if getSqlResultSet() is a general method to execute query, it might call by other method as well , if i make it synchronized, would it affect other method which couldn't access it ?

lastly , thank you very much for your guidance and have a nice day
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized Problem - variable return problem