Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Servlets and Threads - Am I in trouble ?  RSS feed

 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a standard client swing application that I have written and packaged into jar files. I would like to re-use the same database and business logic classes in a servlet so they are common. However they were not written with any consideration for thread safety as I knew this would not be an issue in the enviroment they were designed for.

Do I rewrite everything or can they be called in such a way as to ensure those elements of the application which are probably not thread safe are run by one session at a time.

If a rewrite is in order - how does one test reliably - seems like a minefield !

Dave
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well reading about thread safety in web applications would help. It's possible maybe a simple synchronized block may helpful or maybe many changes be required. It actually depends how is application designed. But I do think that DB layer should be safe.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not be in all that much trouble.

Each request that comes into the server is given its own thread. Any servelt in the system may hav multiple thread using it at the same time, so the servlets themselves must be thread-safe (no instance variables and blah blah).

However, once in the thread, if the servlet creates isntances of your classes, and maintains the reference to thos classes in a thread-safe manner (not in an instance variable and blah blah) those object are safe from thread issues. If the same servlet happens to be active in a another thres, it will create its own isntances of your classes.

That help?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and because multiple threads can be active, make sure that your database transactions are atomic.
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm, I think that helps, but I'm going to have to think about it.

Lets assume that all my database and business logic is in a single file :-

mystuff.jar

I have different packages defined for database and business logic.

com.mysite.db
com.mysite.bl

Within all this code there are bound to be lots of static variable etc and I know for a fact that I only have one database connection open at a time.

So I have a servlet called "my1stServlet" and within it I create a an instance of a class called "do_updates" defined in the package com.mysite.bl in the mystuff.jar.

10 clients connect to the website and each one invokes my1stServlet.

How many instances of my1stServlet are running on the server, I guess just one ?

How many instances of my "do_updates" are in memory ?

What about all those static variables ?

Thanks

Dave
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a design issue, it is bad to design your library in such a way that it is not thread safe _even_ if concurrent threads uses non-shared instances of your Object.

For example, if you let your users know that class "WorkA" is non-thread safe, it implies two threads cannot concurrently access an instance of WorkA. To get around this, the user usually creates unique instances when multi threading is a concern.
But in your case, it seems that unique instances of WorkA cannot be used concurrently.
 
M Easter
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a thought... what if you wrote a thread-safe front-end (e.g. a facade or adapter pattern) that wrapped the functionality of the original code?

it is certainly more work but not "re-writing everything".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I was wrong. You have some issues.

Originally posted by David Garratt:

Lets assume that all my database and business logic is in a single file


That's moot. They all just become part of the classpath. How they are organized into jars is not significant.

Within all this code there are bound to be lots of static variable


Static read-write variables? That's a problem in any scenario and needs to be fixed.

and I know for a fact that I only have one database connection open at a time.


Needs to be replaced with container-managed connection pooling.

How many instances of my1stServlet are running on the server, I guess just one ?


One.

How many instances of my "do_updates" are in memory ?


As many as there are threads.

What about all those static variables ?


Serve as a lesson to always write good object-oriented code. They'll obviously need to be converted to instance variables.

A good rule of thumb to keep in mind is that every time you use the keyword static, if it's not paired with final, it means it's time to think about your design since it's probably a problem.
[ March 09, 2007: Message edited by: Bear Bibeault ]
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem getting my head around some of my existing code and how I can reference it from a servlet. I've checked that most of my static variables are now paired with final as suggested. However the database connection is currently stored in a Class called Common as static variable. I did that as the application was s standalone j2se swing application and it seemed like a good idea to allow all of the database functionality to share the same connection.

I see that I can get a unique session id when running as a web servlet and store the session id in a hashmap with the connection object. Then when I need to get the connection I can always ask for the session id again from any class and lookup the connection object again.

However, if I want to use the same approach when I run the library from a j2se standalone app I don't think there is an equivalent session id.

I can generate a uniqie id myself, but then I have to store it in a static variable and I'm back with the same problem.

Can someone enlighten me ?

Dave
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delay in getting back to everyone on this subject. I've been reviewing my code and checking as best I can that it's thread safe. I'm only left with one design issue in as much as most of my exist library assumes a single database connection. To this end I would like someone to confirm my alternative solution.

Rather than redesign my application code to support multiple pooled different database connections per user session I have considered that it would be an acceptable workaround to publish the application on the same tomcat server using one context per database. I'm only talking about 2 or 3 databases max.

Am I right in the assumption that the same physical servlet deployed 3 times as part of the same application under 3 different contexts will be loaded by the servlet container in 3 distinct sessions and not conflict with one another.

i.e context "a" will have 5 concurrent sessions all using database 1
context "b" will have 5 concurrent sessions all using database 2
and so on.

If a serverlet called "myservlet" is loaded for context "a" it will
be loaded into memory again for the 1st user of connecting via context "b"

Hope my terminology is not too clunky !

Thanks

Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!