New topic: Committing an SQL insert or update
<http://forums.realsoftware.com/viewtopic.php?t=47595> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message dgdavidge Post subject: Committing an SQL insert or updatePosted: Sun Apr 14, 2013 6:39 pm Joined: Fri Jun 02, 2006 1:43 pm Posts: 195 Location: Santa Ynez, CA Two questions: 1. I have noticed that when I use a prepared statement for RealSQLDatabase, I don't have to commit the transaction. In fact, if I do, I get an error message that there is nothing to commit. But if I execute a straight sql insert or update, I have to commit it or the changes are not stored. The database autocommit property is false in both cases. Is this a bug that may break my code if fixed in a future update? or just the way it works? 2. My app has a possibility that two users could try to write to the database at the same time so I check for errors after each write and loop back and try again if there is an error. Do I need to loop back after both the insert and the commit or is just one OK? In other words, is the database locked error raised on the insert or on the commit? Top Thom McGrath Post subject: Re: Committing an SQL insert or updatePosted: Mon Apr 15, 2013 6:33 am Site Admin Joined: Tue May 06, 2008 1:07 pm Posts: 1413 Location: NotEvenOnTheMap, CT You should get into the habit of doing all modifications from inside a transaction. Don't rely on auto commit. Begin a transaction, do your work, then commit. If you run into an error, rollback and stop processing. The problem with auto commit - which you said is false anyway, good - is that we have no idea how many future commands are coming. So a transaction is started and committed for every command. For a single command, this is fine. But for multiple commands, this is very expensive. The begin/commit cycle is a powerful design, but costs extra CPU cycles. Commuting more frequently than necessary will make the commands slower. What you're probably seeing is the lack of a "BEGIN TRANSACTION" statement. Without it, there is nothing to commit. As for issue #2, I'm not sure I can advise. SQLite (and thus REALSQLDatabase) was not designed to be used by multiple users. _________________ Thom McGrath - @tekcor Web Framework Architect, Real Software, Inc. Top dgdavidge Post subject: Re: Committing an SQL insert or updatePosted: Mon Apr 15, 2013 10:13 am Joined: Fri Jun 02, 2006 1:43 pm Posts: 195 Location: Santa Ynez, CA Thank you for your answer Thom, but you did not really address the issue. Autocommit is true for prepared statements and false for normal (not prepared statements). This seems strange to me and I am asking if it is supposed to be this way? If it is, I will remove the db.commit statements. If not, I will leave them (without error checking) so the program doesn't break in the future if it get changed. Top Thom McGrath Post subject: Re: Committing an SQL insert or updatePosted: Mon Apr 15, 2013 10:15 am Site Admin Joined: Tue May 06, 2008 1:07 pm Posts: 1413 Location: NotEvenOnTheMap, CT What I suggested towards the end of my message is to be sure you insert matching "BEGIN TRANSACTION" statements, and turn autocommit off. _________________ Thom McGrath - @tekcor Web Framework Architect, Real Software, Inc. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
