wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java Objects - Refresh" Watch "Java Objects - Refresh" New topic
Author

Java Objects - Refresh

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Guys,

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?

Thanks,
Guru
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
Sheriff

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: http://aspose.com/file-tools
 
subject: Java Objects - Refresh
 
Similar Threads
forEach with a resultSet ?
Ordered List tag inside jsp
how to read html table in servlet
How to store HTML table values in DB
MSSQL and Unicode in WIndows and Linux