Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Objects - Refresh

 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic