Results 1 to 4 of 4
  1. #1
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Deleting data while it's modified by a thread

    Hi,

    i have entitys that periodically get data from a server on the internet. Each entity runs in a thread like this:

    Java Code:
    	scheduler.scheduleAtFixedRate(new Runnable() {
    			@Override public void run() {
    		       	get data ...
                                     do something with data...
                                      ....
    An entity can be deleted from the database. I now dont know what to do if the user deletes and entity that is currently updated. I could try to stop the thread before the delete, but as i read it's not guaranted that is's stopped and even if it is what would happen with all the variables etc. in that code ?

  2. #2
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Deleting data while it's modified by a thread

    I use EclipseLink by the way.
    If i just delete an entity while it is updating, i get an PersistenceException saying that a connection can't be closed while a transaction is active

  3. #3
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Deleting data while it's modified by a thread

    Nobody can help?
    If my problem isn't clear please ask.

    Here is a more detailed explanation:

    - i have 2 tables in a Database (Stock and StockPrice)
    - Stock has the fields stockid, stockname, currency, tickersymbol etc ...
    - StockPrice has id, price of a stock, time of a Stock and as foreign key the stockid
    - A StockPrice can't exist without a stockid

    Every stock in the stock table now has a thread running to get the actual stockprice (from a server on the Internet) and save it in the table StockPrice.
    To save it in the table a stockPrice has to have a stockid.

    The problem i have now occurs when the user deletes a stock, cause then the Thread tries to insert a stockprice in Stockprice with the stockid null.

    I don't really know now what's the right way to handle this situation. Should i try to immediately stop the Scheduler before i delete the Stock? Thus canceling the thread in the middle of doing something? And if yes how?

    Or should i delete the Stock and try to handle the Exceptions the thread in execution is then throwing and then shutdown the scheduler normally (after finishing executing it's work) ?

  4. #4
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Deleting data while it's modified by a thread

    Databases has synchronization program that handles exceptions such as write before read .......
    Just look PL/SQL :> to write trigger or procedure
    you have to write trigger don't use thread to change that values. Trigger will solve your problem

    if you write trigger then database automatically changes the other tables according to relation between tables.
    You have to write correct trigger ofcourse.


    if this is not your solution, then you can catch error and ignore that case

    try{
    insertStockprice();
    }catch(Exception e)//I do not know the exact exception so you can see in error log
    {
    //Not to do :> just blank: It will skip this insertion. Also you can kill the thread in here it's your choice
    }


    I hope it helps

Similar Threads

  1. problem in deleting data
    By harsimran05 in forum JDBC
    Replies: 1
    Last Post: 10-12-2011, 09:03 PM
  2. deleting the data in the column model
    By bigj in forum New To Java
    Replies: 0
    Last Post: 02-08-2010, 08:15 AM
  3. Modified Pixels
    By monkey04 in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:15 AM
  4. JSP Last Modified Date
    By thagentleman in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-18-2008, 04:52 AM
  5. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •