dynamic data displayed in static UI - java

My program uses Swing table to display data stored in MySQL, while the data is dynamic. I use a thread to repeatedly fetch data from database and refresh the table. I wonder if there are other much wiser methods to do this. Thanks for any suggestion.

as you said, using a single thread to query database and update the UI
is appropriate, am I right?
not, couldn't be true, or yes, everything depends of latency and accessibily of data source(s)
be sure that you'll update only visible Rectangle, cell in the JViewport, on periodical bases, other changes only to notify separatelly (JLabel, ToolTip, SystemTray e.g. ....)
example about JTable and Performance, called as ChristmastTree (lost somwhere after Oracle cleanup Suns tutorial and codesources in last year), bases on standard Workers Thread (before SwingWorker...)
I wonder if there are other much wiser methods to do this.
as I already commnented there are Database notifiers thata firing event when add/ delete/ change is triggered in DB Engine, never tried, can't suggest if use that or not

Related

Can TableRowSorter be used to do background sorting or backend sorting?

There is a usability issue when you have a JTable with a huge number of rows in conjunction with the normal TableRowSorter. Sorting on any column will fetch all the values for that column. This is all done on the EDT, which freezes the application for potentially a very long time. It's doubly-bad because it also does the sorting on the EDT even though it doesn't have to.
So I'm wondering whether there is a right way to do this.
I already have a thread-safe TableModel which is safe to call on a background thread. Is there an elegant way to have it fetch the values on another thread instead of doing it on the EDT?
But really, eventually I would like to do the sort at the backend, so perhaps there is an elegant way to hook the table's row sorter up to changing the actual query being used to fetch the data? It's something that I thought was being worked on in some jdesktop project in the past, but I don't think I ever saw what came from that.
In both these situations, there is an associated usability issue of indicating that a sort has been specified but hasn't completed yet, which I'm not entirely sure the JTable look and feel will display, but who knows, maybe it will.

Hibernate JTable Synchronisation

I want to synchronise my SQL database using Hibernate and show the entries in JTable.
In order to allow for editing the data in JTable, it is necessary to monitor changes made in JTable and update the database using Hibernate.
There are few examples how to achieve this by using a CustomTableModel. My question is whether you would suggest immediate updates, i.e. db queries after Table edit or collecting multiple changes and commit them together, however this would require to LOCK the db table to avoid parallel changes by other users.
Thanks your answers.
Its hard to give a black & white answer to this as it is very much up to many factors. It depends on the data, it depends on the application, it depends on the situation.
I think the middle ground here is to utilize optimistic locking. With what little context you provide, I'd say that is the most fitting.
http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html
This implies that if two users do try to alter the same data at the same time, one of them is not going to successfully complete the transaction. But the data is going to be safe.

Need a program that continously listens to the Oracle DB and if any DML's are made it calls another program?

I have an Oracle DB with a lot of tables ... How can I write something which listens to the Oracle DB and if any changes are made to the DB, it calls another program which does some processing
It would be asnync
I just need to trigger a java program if there is any kind of DML happening .. I dont want details about which table or what rows or new/old values ...
If there is a DML on any table in a DB call a java program thats it
http://docs.oracle.com/cd/B14117_01/win.101/b10118/o4o00118.htm
OracleDB has some events. Just try to use them; For example, when the db is updated - And to call another java program:
Runtime.getRuntime().exec(/Here the argument/);
I fear you're setting yourself up for failure here. I suggest a related, but slightly different course of action.
Rather than trigging your processing on every data change, consider instead having your processing run every X minutes/hours/whatever using something like Cron or Quartz.
If you're worried about having it run when no changes have been made, you can add triggers to your tables to update a "last updated" table and you can abort if no changes have been made since the last run.
This avoids the biggest concern you would have with an "on-update-processor", namely what do you do if there's an update, which triggers a process, and while that process is running another update happens. Do you trigger another process? What if they conflict? I think you'll find it better to allow there to be a slight delay between the update and the synchronization process.
That's a pretty tall order for a question.
For starters, you are going to need a way to detect that a DML operation (INSERT, UPDATE, DELETE) has occurred.
For auditing on individual tables, we can use TRIGGERS.
CREATE TRIGGER [BEFORE|AFTER] [INSERT][UPDATE][DELETE] ... [FOR EACH ROW]
The normative action is to record information about the information (whatever is needed later) in an "audit log" table. You have to make a decision whether the operations you need to perform are synchronous (should happen before the DML completes) or whether those operations can be asynchronous (can happen anytime after the DML completes).
Normally, synchronous processing is done in PL/SQL, but Oracle does provide a mechanism for calling external procedures (EXTPROC), though you wouldn't want to do that from a TRIGGER.
Oracle also provides "fine grained auditing". The information gathered by FGA is not at a "row by row" level, like we can get with a FOR EACH ROW trigger.
So, it really depends on what you are trying to achieve.
The next step is figuring out how you are going to get this information to your external process. Is that process going to periodically poll table (or set of tables), are you going to use AQ Advanced Queueing, or some other queue mechanism.

GWT wait for setVisibleRangeAndClearData to finish

This a variant for the classic "wait rpc call problem" in GWT.
I have done a complete CRUD screen in GWT, calling RPC to atack an Oracle database. It has one panel for the search criteria and the results table, and other for the detail fields. I swap the visiblity of both as i need to.
The results table is a CellTable and i call the "setVisibleRangeAndClearData" method to get the table populated, which that takes care for paging, column sort and so on (for example when i come back from an update RPC call i keep the range i came from).
I use to make the "synchro" by putting the "after code" in the "onSuccess" block of the RPC call (i.e. swaping the panels), but when calling "setVisibleRangeAndClearData", the cellTable by itself launches another RPC call to get new data which is out of my control. So, what happens is that i swap the panels (wich is immediate) and after that, the user sees the table data update, which is ugly.
So the question is: How can i control that? How can i wait or get called when the "setVisibleRangeAndClearData" is finisehd?
Thanks in advance,
David
Showing the table right away and then populating it with data is not ugly - in fact, it's the right solution from a UI perspective. Users expect immediate result of their actions, and showing them a loading indicator immediately after they press the button is the right approach.
CellTable has two methods you may find useful:
setLoadingIndicator() - your table should display it until the data arrives;
onLoadingStatusChange().

JAVA - Strange problem (probably thread problem) with JTable & Model

I am using 2 Tables (JTable) with their DefaultTableModels.
The first table is already populated.
The second table is populated for each row of the first table (using an SQL Query).
My purpose is to export every line of the first table with it's respective lines of the second in an Excel File.
I am doing it with a for (for each line of 1st table), in which I write a line of the 1st table in the Excel File, then I populate the 2nd table (for this line of 1st Table), I get every line from the Table (from it's Model actually) and put it in the Excel File under the current line of 1st table.
This means that if I have n lines in first table I will clear and populate again the second table n times.
All this code is called in a seperate thread.
THE PROBLEM IS:
Everything works perfectly fine ecxept that I am getting some exceptions.
The strange thing is that I'm not getting anything false in my result.
The Excel file is perfect.
Some of the lines of the exceptions are:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
at javax.swing.JComponent.paint(JComponent.java:1017)
at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:803)
I am assuming that the problem lies in the fact that the second table needs some more time to be populated before I try to get any data from it. That's why I see RepaintManager and paintDirtyRegions in my exceptions.
Another thing I did is that I ran my program in debug mode and I put a breakpoint after each population of the 2nd table. Then I pressed F5 to continue for each population of 2nd table and no exception appeared. The program came to it's end without any exceptions.
This is another important fact that tells me that maybe in this case I gave the table enough time to be populated.
Of course you will ask me:
If your program works fine, why do you care about the exceptions?
I care for avoiding any future problems and I care to have a better understanding of Java and Java GUI and threads.
Why do you depend on a GUI component (and it's model) to get your information and why don't you recreate the resultset that populates your tables using an SQL Query and get your info from the resultset?
That would be the best and the right way. The fact is that I have the tables code ready and it was easier for me to just get the info from them. But the right way would be to get everything direct from database. Anyway what I did brought out my question, and answering it would help me understand more things about java. So I posted it.
The Swing API is not thread-safe except for a few method calls: repaint, revalidate, and invalidate. All other calls unless otherwise noted for a specific class must be made on the Event Dispatch Thread.
Transferring such call processing from a spawned background/worker thread can be done via SwingUtilities.invokeLater() or SwingUtilities.invokeAndWait().
There is also some specific discussion regarding both JTable and any TableModel that has been attached to an instance of it in the javax.swing package summary javadocs. Neither is thread-safe, so any calls accessing data from them must be performed on the Event Dispatch Thread.
This is the most probable cause of the exceptions you are encountering, and the different behavior that you experience when running in a debugger is a classic sign of a race condition. There is also no reliable way to hack around this via introducing your own locks, etc. Such practices invariably lead to trouble (such as deadlocks with the Event Dispatch Queue lock deep inside the Swing library) in the long run since Swing really, truly was not designed to be thread-safe.
The exception is happening because one of the table models is returning null for a getValueAt(int row,int column) call. The reason for this is probably an internal issue in swing or the data model due to the fact that you are using a secondary thread to access the data models. The swing api specifically states that you can not use a secondary thread in the way you described.
The following article provides further details on the single thread rule in swing.
http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html

Categories

Resources