wood burning stoves 2.0*
The moose likes Struts and the fly likes Question about thread safe implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Question about thread safe implementation" Watch "Question about thread safe implementation" New topic
Author

Question about thread safe implementation

Thierry Collogne
Greenhorn

Joined: Oct 19, 2005
Posts: 16
Hi all,

I am developping a struts application with database communication.

For the database functionality, I use a java class "DatabaseHandler" wich is stored in the session. I
n this class I implement all my database functionality. All my results of methods in the DatabaseHandler are stored in a javabean called Reponse.
The object stored is a Vector with objects of Player.

It goes like this.

public class GetPlayersAction extends Action {
public ActionForward execute(...) {

HttpSession session = request.getSession();

DatabaseHandler dh = null;

if(session.getAttribute("databaseHandler") != null)
dh = session.getAttribute("databaseHandler");
else
dh = new DatabaseHandler();

Response response = dh.GetPlayers();

Vector players = response.getPlayers();

request.setAttribute("players",players);

return mapping.findForward("players");

}

}


Then on the JSP page I can get the players vector from the request and display its content on the page.

Can this pose thread safe issues or not?

Thierry
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I don't see any glaring threading issues here, but a lot would depend on your DatabaseHandler class. Did you roll your own database connection code, or are you using some type of connection pooling? Since you are storing your DatabaseHandler on the session, each user will have their own instance. If each instance of your DatabaseHandler class tries to maintain its own database connection then I could see scalability issues.

BTW, do you ever call session.setAttribute to set the "databaseHandler" attribute?

- Brent
[ March 03, 2006: Message edited by: Brent Sterling ]
Thierry Collogne
Greenhorn

Joined: Oct 19, 2005
Posts: 16
Thank you for your response

I call the session.setAttribute(). I just forgot to mention it.

For database connection I use a java.sql.datasource which is declared in my tomcat server.xml. I believe that this gives me connection pooling, or am I wrong?
 
jQuery in Action, 2nd edition
 
subject: Question about thread safe implementation
 
Similar Threads
StringTokenizer in Jdk1.5
session expire exception
Writing thread-safe Actions
Session does not persist
bookmarking a page using session variables