views:

34

answers:

3

Hi,

I am faced with a peculiar requirement which is as follows:
A network-intensive operation is triggered to a server by multiple clients, through a web-interface. However, only one operation is allowed at a time, and hence an entry(tuple) is made in an SQL table to indicate that the operation is in progress. Once the operation is complete (irrespective of success or failure), the appropriate result is displayed back to the client(s), and the corresponding tuple is removed from the SQL table.
Since the operation is network-intensive, a scenario where the operation needs to be "considered" to be cancelled, after some timeout (10 minutes) has to be introduced.
Is there ANY way the lifetime of a row in SQL be associated with a timeout value, so that is is deleted after certain time? My application is primarily written in Java 1.5 and EJB 3.0, using JPA/Hibernate to access Oracle 10g DB engine.

Thanks in advance.

Regards,
Nagendra U M

A: 

It sounds like you're implementing a mutex using the database, take a look at this question and see if it helps? Sounds like transactional access to a flag table will solve this for you, as long as you catch both success & failure states in your server code.

Jon Freedman
@Jon: EXACTLY !! The purpose of my SQL table is to ensure that only one operation is going on at a time. But in case there is no response from the server, even after a long time, then I need a way to deal with timeouts, so that I can release the operation to be done by other clients. i.e., to remove the entry from the table after 10 minutes. How can this be done? any way through native SQL statements or even JPA? And, thanks for the speedy response. :-)
Nagendra U M
Your transaction could read like: `begin tran; if not exists (select 1 from table where started >= dateadd(mi, -10, getdate())) begin; insert into table ..., getdate(); end; commit` - either that, or whatever process decides to stop running your network intenseive operation, also updates the table
Jon Freedman
A: 

I don't know that Oracle has this kind of facility but I think no db engine have this.

If you want to do it at DB level,

  1. you must have a datetime column, e.g.; 'CreatedDate' in table. This column will have datetime when record was created.

  2. Write a procedure and put it in a schedule job. This job will run after every 10 minutes and remove the 10 minutes old records. The query will be like this.

T-SQL: Please convert it according to your db engine.

DELETE FROM yourtable WHERE CreatedDate < DATEADD(mi, -10, GETDATE())

This will delete all records older than 10 minutes from table.

This is just to give you idea of schedule job. It is in SQL Server. I don't know about Oracle

step_by_step_guide_to_add_a_sql_job_in_sql_server_2005

Muhammad Kashif Nadeem
+1  A: 

I would suggest that you try using a timestamp column containing the start time of the task.

A before trigger can be then made to delete the old column before a new one is inserted if the task timed out.

If you want to have multiple tasks with different timeouts, you can even add a column with the timeout in seconds. Just code your trigger accordingly.

Amanda Leishman