ThreadLocal implementation problem

Dear All,

I have a problem with my implementation of ThreadLocal. When I populate a table with data loaded from JDBC SQL Query, sometimes another instance of application is displaying another instance’s query result, this is happen when the query is slow. I think when Connection object is instantiated in a threadlocal vaadin application, it should also have unique thread, so different query will be processed, and it should have not display another query from another connection., but since each thread process different query string, can it use different query result processed from another Connection object from another ThreadLocal application instance?



My first guess would be that you have declared some variable as static and therefore are sharing that with all application instances. In the typical case there should be no need to create separate threads for accessing the database.

Note also that application servers are reusing the same threads from a pool to serve all requests from all users. Therefore, any ThreadLocal variables (for non-background threads) should be set at the beginning of every request and ideally also cleared at the end of the request. This can be done e.g. with an HttpServletRequestListener or a TransactionListener.

I’m not sure if this is the issue here, but something to be aware of at least.

Thanks for your response Henri,

But I have a question, my application is built using Vaadin 6.5.4 (and we decided to keep that version, since custom Gwt widgets developed by earlier development are using that version) , Tomcat 6.0.32 and MySql JDBC driver 5.1.6.

I noticed strange behaviors like a thread is sharing it’s data to another thread, except when the data is ‘refreshed’ (recalling query to server then repopulate it’s table) , for example instance A have a table populated with data, then another instance ( B ) making updates on it’s data (like filling and commiting a form data in a window ), after that instance B closed the form, immediately it had table updated from instance A’s query result. This is strange since A and B are implementing ThreadLocal, and there’re no more static variables. Are there something like data-leaking to another thread?



Data might be shared between threads via explicitly shared references to the same object or statics.
Note that these can also be outside your code e.g. as a cache in a database access library or a container - this is probably what happens in your case.

On the other hand, as I mentioned earlier, also ThreadLocal should be managed carefully so that you reset it at the end of a request and set it again at the beginning of the next one - request handling threads of application servers are reused from a pool for other sessions.

That sounds like a good suspicion.

My suggestion to Abiel would be to add logging anywhere data is accessed, displayed, etc. Give your threads useful names, and have the threads log something about the data returned from a db query. When that data is set on some UI component, log it there as well along with the current thread name. You may end up with a mess of logging to sort through (logged at a level where you can turn it off later), but that can be more reliable than trying to step through separate threads in a debugger (which also messes up your application’s normal timing).

Good luck: threads will get the unwary.