File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Real time update from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Real time update from database" Watch "Real time update from database" New topic
Author

Real time update from database

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hello Ranchers,

I am facing difficulty in achieving the following scenario.

Consider we have a employee table that has firstname and lastname as its column.
Employee (firstname, lastname)

I am writing the data in employee table to a file using java io. So suppose if one more record is added in the employee table, then the file must be updated with the new information.

We can achieve this by restarting the server manually. But in a real time environment, it is not possible to restart the server every time a record is inserted in the employee table.

Some how we need a component that is always checking the data in the employee table and keeps the file updated with the most current data.

Can you guys suggest a way to solve this problem?

I am using Java 5, Tomcat and Oracle.

Any suggestions, links and tutorials are welcome.

Thanks
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You could use a Listener. Every time some class modifies the file it notifies the listener.


JDBCSupport - An easy to use, light-weight JDBC framework -
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Sebastian Janisch wrote:You could use a Listener. Every time some class modifies the file it notifies the listener.

In the servlet environment, there are no such listeners. In fact, there is no guarantee that the browser even has the page loaded!

Why on earth are you writing the data to a file before displaying it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
Servlet Listener will not pool any database table, to get info like any new record inserted or deleted... I would suggest you that in Database (table trigger) we can write code to add new record details into a file.

Regards,

Sankar. S
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
@ Bear
The example I gave is not the exact implementation in the application.
The application has a component in the home page that reads the file that I am writing to. This component then (after some business logic) reads the file and displays the data in it.
Hope I have made myself clear.

@Sankar
Thanks for giving your idea. Writing a trigger will definitely be in the direction to solve the problem. But it is a huge overhead on the application.

Is there any other alternative to solve this problem?

Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30369
    
150

Matt Thomassan wrote:Is there any other alternative to solve this problem?

What problem? You still haven't described the purpose of this file. I would say the "solution" would be to get rid of the file and read from the database if you don't have a reason for the file existing.
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
@ Jeanne,

The following is the purpose of the file:
Home page has a flash component that reads data from xml file. This xml file is required to be updated when some database modifications happen.

What do you think about the problem now?

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

It may be too late in the day to suggest this but any reason you didn't just query the database form your Flash application? This would be a far easier way of doing this.

The only guarenteed way to push data out to the file is a database trigger. If your database supports it you could use Java to write such a trigger for update, insert and delete events. If it doesn't support Java some databases have their own mechanisms for doing this sort of thing. Not very nice adding the overhead of this extra IO work for every mutation on the table though. And if you ever deploy your application in a clustered environment you are stuffed.

Another alternative is to poll the database from your Java code using something like a timer task and update the file accordingly. This has the advantage of not burdening your database with the file creation, but has the disadvantage that your data is always out of date (though you have engineered that problem into your application anyway by using a file as an intermediary).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30369
    
150

Polling is what I'd suggest too if you can't change the Flash code. Or if you control the writer to have it update the file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Real time update from database