• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persistent data in servlets

 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So is there any way we can maintain persistent data in servlets?

Since tomcat acts as a container for running my servlet data, each time a connection is invoked, tomcat runs a new instance of the servlet on a different thread.

Say I want to hold some value in a variable and dont want to reinit but use it again on the next instance, is there some way we can do that ?

Regards,
Bharath
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bharath Neo wrote:So is there any way we can maintain persistent data in servlets?
No. See below.

Since tomcat acts as a container for running my servlet data, each time a connection is invoked, tomcat runs a new instance of the servlet on a different thread.

No, it doesn't. It uses the same servlet instance in mutltiple threads servicing multiple requests. Which is why you cannot use servlets themselves as a peristance mechanism.

Say I want to hold some value in a variable and dont want to reinit but use it again on the next instance, is there some way we can do that ?

Of course. That's what the various scopes are all about. If you want data stored on a per-user basis, use the session. if it's application-wide, use the application context.
 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. But looks like the application context has to be hard coded in the web.xml file. Cant we change it or set it from the servlet code?

Basically I dont want to connect to the DB always from the tomcat on every connection but just once.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bharath Neo wrote:Thanks for the info. But looks like the application context has to be hard coded in the web.xml file.

Huh? The application context exists for every web application. You don't have to do anything in the deployment descriptor.

Cant we change it or set it from the servlet code?

If you mean: can you store and change information in the application context, then yes.

Basically I dont want to connect to the DB always from the tomcat on every connection but just once.

For what purpose? You don't want to create a single connection and then use it throughtout the life of the web application. That's exactly the wrong way to do it. Rather, a connection pool that grabs connections only when needed, and releases them as soon as possible is the best way to go.
 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. Shall look at connection pools. But is there any code sample for using the application context? Googling returns it mostly as the context info the deployment desc file...
 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also cant I just have define it in the init() method of the servlet to make it global for the whole lifetime tomcat stays up?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bharath Neo wrote:But is there any code sample for using the application context?

It's just a tad confusing because programmatically, it's the servletContext. To set a scoped variable into application context: to fetch one:

Bharath Neo wrote:Also cant I just have define it in the init() method of the servlet to make it global for the whole lifetime tomcat stays up?

You can, but it's a bad bad idea. DB resource are precious and should remain open only as long as they are absolutely needed.

It's sort of like borrowing a library book. While you have the book out, nobody else can borrow it. So it's most polite to only reserve the book while you are reading it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic