File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Connections in static methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Connections in static methods?" Watch "Connections in static methods?" New topic

Connections in static methods?

Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78

I need to refactor some code because I have no idea how this is supposed to work in a multi-threaded environment. So here is what's going on:
- The UI is reading XMLs from a URLs. In order to avoid repetition of same code everywhere, methods like connect method (to open URLConnection and read xml string) and getDocument (to create a Document object from URL) were made static and are being used as Util.getConnection() or Util.getDocument() all over the code.

This is working fine until now, but I am not confident if this will still work when there are more than 1 person accessing the UI?

Any thoughts on this?

Greg Charles

Joined: Oct 01, 2001
Posts: 2968

We'd have to see the code to know for sure. When you call Util.getConnection(), does it just return the opened connection object to the caller (thread safe) or does it store it internally (not thread safe)? How are connections closed when you're finished with them? I assume that Util.getDocument() must have a connection to work with. Where is that coming from?

Threads are a difficult topic. Try to walk through imaginary scenarios where one thread gets part way through its work, but then has to wait while the second thread runs. Can something the first thread needs be modified by the second thread?

This topic is too complicated for beginners, and it is mainly about multi-threading issues, so I'll move it over to the Threads and Synchronization forum.
Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
Here is the code in Util class:

I am not returning any Connection here:

So, are these methods thread safe
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Yes, those look okay by themselves. If everything that is created is held as local variables, you are fine because each Thread gets its own copy of local variables. The only question comes out of shared data (data that is stored as static or instance variables, or which are passed in as parameters or returned as results).

From the code you provided, the only piece of the puzzle you would need to worry about is the Document. The Document is mutable, so if multiple threads had a hold on the same Document they could cause problems by simultaneously changing it, so you would need to make sure that the Document doesn't leak to multiple threads. That isn't something you can do in this Util class though.

Nick Sher
Ranch Hand

Joined: Nov 10, 2008
Posts: 78
Thank you Steve, your reply is helpful. So how do I make sure that the Document does not leak to multiple threads? I hope this can be done in the object that calls this method.
I agree. Here's the link:
subject: Connections in static methods?
It's not a secret anymore!