samedi 18 juillet 2015

Checking if SQLite Database is Locked in Java

Is there any way to check if an SQLite database is locked in a Java program?

I've got a java program that uses SQLite and will be used by multiple users. All of the SQL statements close the connection after use, so no problem there. However, there may be some occasions where I need to use a third party bit of software (FlySpeed SQL Query) to alter the database to fix errors that I might not be able to fix in my own program. However, I've noticed that FlySpeed (and NetBeans I think) sometimes locks the database file. This locking can then lead to SQLExceptions being thrown. To avoid any potential problems (more than likely due to my less than perfect coding!), I want my program to check if the database file is locked before my program starts interacting with it. Any thoughts how this could be done?

I've thought that I could do a dummy insert statement, but this seems a little wasteful and slow as I'd have to wait for a timeout. Any thoughts?

Thanks muchly!

(The main reason I want to implement this is that one of my functions deletes a lot of records from the database followed by adding replacements. From what I can determine, I think I have to have this a two separate statements in separate transactions. I once had Flyspeed and Netbeans open and was testing my program. For some reason unknown to me, the function deleted the correct records, but then returned the SQLException on trying to add the records. I don't want this to happen if a similar scenario comes up in the future, or in the less likely event that another user ties up the database long enough for the timeout to kick in.)

Aucun commentaire:

Enregistrer un commentaire