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

Java Objects - Refresh

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273

I have a servlet which composes a java data object by querying the data from certain table. The servlet binds this java object at the server side when you start the servlet container.

My requirement is that when somebody changes the data on the database table, this java data object should be in sync. How to do that?

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
You mean change in database from your application. If it is as simple as I mentioned then just reload that object for every update.

But if you mean change in database from any source, your app, some other app or directly, etc. then there is not any clear cut solution. You need to query the DB after every short interval.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If all access to the database is through your app this is fairly straightforward. Add a refresh method to your object. Then have any methods that update the tables also call this method to let the object know that there has been a change. (Read up on the "Observer Pattern" to see how this is commonly done).

Depending on your needs, you could also have it work the other way around. Have set an isChanged property in the objects that update the data. Then have your object check that property to see if it needs to get the data from the database when it's called. This could eliminate some unecessary calls to the database.

If applications other than your own can update the tables, this is more difficult. You may need to set some kind of flag at the database tier that your object can read when called. Since 'getting' the connection is usually the most expensive operation in the process, it may not be worth caching under these circumstances.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Java Objects - Refresh
It's not a secret anymore!