Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if SQLite3 transaction is active

Tags:

sqlite

I am running an END TRANSACTION on my database and occasionally I get error

#1 that "cannot commit - no transaction is active"

Is there a way to determine if a transaction is active before trying a commit? I have been tracking my "BEGIN TRANSACTIONS" by hand but I feel there is a better way.

I am using the C API

like image 258
John Smith Avatar asked Nov 12 '09 03:11

John Smith


2 Answers

The interface you're looking for is actually implemented in this next version of sqlite as you can see in the notes: https://sqlite.org/draft/c3ref/txn_state.html

Determine the transaction state of a database

int sqlite3_txn_state(sqlite3*,const char *zSchema);

The sqlite3_txn_state(D,S) interface returns the current transaction state of schema S in database connection D. If S is NULL, then the highest transaction state of any schema on databse connection D is returned. Transaction states are (in order of lowest to highest):

SQLITE_TXN_NONE
SQLITE_TXN_READ
SQLITE_TXN_WRITE

If the S argument to sqlite3_txn_state(D,S) is not the name of a valid schema, then -1 is returned.
like image 132
chacham15 Avatar answered Oct 24 '22 11:10

chacham15


You might want to check this:

http://www.sqlite.org/c3ref/get_autocommit.html

According to the page, if you are in a transaction, sqlite3_get_autocommit() will return 0.

like image 30
MPelletier Avatar answered Oct 24 '22 10:10

MPelletier