Zoltan Ersek
Zoltan Ersek

Reputation: 775

Will the state of the transaction remain even if I open and close connections to the database

I have a "private" method that executes an sql instruction on my sqlite database:

-(void)runQuery:(const char*)query isQueryExecutable:(BOOL)queryExecutable withArguments:(NSArray*)arguments

The problem is that at the beginning of the method, it opens a connection to the database and finally at the end of the methods it closes it.

Now, what I want is to create 3 private methods for creating, committing and rolling back a transaction, that each will call

sqlite3_exec(db, %command%, 0, 0, 0)

where %command% is "BEGIN", "COMMIT", "ROLLBACK", depending on the method.

And, what I want to do next is call begin transaction and afterward use the runQuery:isQueryExecutable:witArguments: method described earlier, and finally commit or rollback.

My question is: will the transaction remain open, even though each time I call runQuery:isQueryExecutable:witArguments: I open and close a connection to the database ?

Sample code:

createTransaction
for n times:
    call runQuery
commitTransaction

Upvotes: 0

Views: 57

Answers (1)

CL.
CL.

Reputation: 180162

Every connection uses exactly one transaction; closing a connection with a still-active transaction will roll it back.

You should not re-open the database repeatedly. Just use a single connection for all your database accesses.

Upvotes: 1

Related Questions