How to run querys in background thread in javafx 2.1 - java

I have an issue with my app that is a stand alone Java application. I am using the core java, JDBC and javafx 2.1. I have to show users the number of rows present in a table in the database. For that I am firing SELECT COUNT(*) FROM SCHEMA.TABLENAME in my java code and displaying results in a tableView (javafx 2.1). As my database contains large number of tables with large rowcount (number of rows in a table), this process takes a lot of time (30 mins.). With that approach, my tableview is stuck and users will be unable to proceed further until the process finishes. I am using the normal Thread.sleep() in my code.
I would like to run the process in the background so that users will be able to do other tasks. Users should be notified once the process is done. I have tried with javafx 2.1 asynchronous but couldn't solve the issue.
Please give me some piece of code that will interact with the db in the background in javafx 2.1.
Thanks& Regards
Salamat

Use a Task as in the JavaFX Concurrency Tutorial.
This allows you to safely execute code on a background thread and not block the main UI thread.
Here is some sample code for accessing a database using a Task.

Related

Kill long running SQL query from grails application

I may look stupid by asking this question - but i want to kill any questions that i have in my mind regarding this..
So.. the question is --
Can we abort/kill a select SQL from my grails application which is triggered from my application itself?
In layman terms -
I have 'Search' and 'Stop' buttons on my screen
Action on search button will prepare a select SQL which will take quite sometime to execute and fetch results
I want to abort long running SQL (not just application stop waiting for result. It should abort the SQL process in the background) by clicking 'Stop' button
(My DB is Oracle)
Please help me clear this trash from my mind.
You might want to use a query timeout. Grails uses Hibernate which implements JPA.
JPA2 persistence property "javax.persistence.query.timeout" allows you to set the timeout for a query (assuming the underlying JDBC driver supports it).
So add the following to the grails-app/conf/Config.groovy:
javax.persistence.query.timeout=9000
The value is in milliseconds, so that would be 9 seconds.

Running programme to monitor file infiteley

I am trying to run infinitely a program that monitors a logfile and updates to the database the errors that have occured and keeping track of a max number of occurances for some error so to send an email if one error is occuring too many times. I managed to do the programme that listens and updates appropriately and sends the email but I don't know how I am gonna be able to start off the programm to run infinitely as it will be monitoring a live system. On the other hand I have a web UI that displays the info updated by the monitoring programme, in other words I don't want the updating programme to stop running even when I my web UI is closed in the browser, behind the scenes I want updating programme to continue to run. I want to separate them so they run independent of each other. I am using Servlets and JSPs for my front end. I was thinking of starting the updating programme in my servlet but I am thinking when it is closed the updating programme will stop run which is what I am trying to avoid. Any kind of advice is highly appreciated, any tool available that allows running a programme infinetely and only stops when server is down and thanks in advance.
NB: I wanted to use log4j to append errors to database to allow me easier
updating but the system wasn't designed to accommodate my requirements so
I have no choice but to use this approach.
Assuming that the Web UI displays information retrieved from the database and if you want to separate out the web UI from the monitoring program, I would suggest keeping a separate native Java process running that constantly monitors the log file and updates the database. The database will be used as a communication medium between the two Web UI and the Java process.
That way the Java process will remain independent of the Web UI and can run indefinitely.
Think it this way -
1. Java process updates the database as it monitors the log file
2. The Web UI reading from database whenever it is up.
The Java process can probably also communicate with Web UI service to provide some heart beat to acknowledge that it's up.
Hope this helps.

Detecting mysql table lock from java

I have a web application based on Java EE and MYSQL.
The problem which I am facing right now is, there is an event getting fired online, which is updating a table entry for a large number of rows.
There is also a batch job scheduled in the environment, say for every 6 minutes, which is also updating the same table entry, as mentioned above.
So the situation is that, when the online event is getting fired in the time range of the batch run, the table is getting locked.
So, experts please provide your ideas, on how to prevent the lock to take place.
Looks like a design issue,take a look at using temporary tables.

Multithreading on a database using oracle and mysql

Does anyone have a code sample of a multithreading case that has about 25 threads that monitor a specific table in the database for a change then execute a process based on that change?
If you just want to be notified in the client application that something has changed in the database and you need to react on that in the application itself (so that triggers are not an option) you can use Oracle's change notification.
In order to do that, you register a listener with the JDBC driver specifying the "result set" that should be monitored. That listener will be called whenever something changes in the database.
For details on how this works, see the manual:
http://download.oracle.com/docs/cd/B28359_01/java.111/b31224/dbmgmnt.htm#CHDEJECF
If you want to monitor the table (in the database) and make changes also in the database, then you should really consider Triggers.
Simply, Triggers are kind of procedures that run automatically BEFORE or AFTER changes on a table. You can monitor UPDATE, INSERT or DELETE transactions and then make your action.
Here is Simple tutorial on Oracle Triggers
Try directly using a Database Trigger instead.
Check this question about getting events in java from Database

JTable realtime update with MySQL Cluster

I read several doc that told me that if i want to make a realtime application, i should use MySQL Cluster. So this is what i done.
But i wasn't able to find how to make a JTable updated each time a value in the MySql Cluster was updated.
Do you have any links or example that can help with my issue.
Thanks in advance.
Is your java app the one performing the updates on the database or is possible for other processes to make changes?
If only your app is making the changes, you can fire an event every time an update/insert/delete happens that will refresh the table.
If another process can make changes to the database then you'll need a background thread that will occasionally query the database for changes.
In terms of displaying the data in the JTable, you'll probably need a pretty customized TableModel to cache data, etc.. to minimize the effects of refreshing if the data is changing rather frequently

Categories