@@ -165,7 +165,6 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
165165 self -> statement_cache -> decref_factory = 0 ;
166166 Py_DECREF (self );
167167
168- self -> inTransaction = 0 ;
169168 self -> detect_types = detect_types ;
170169 self -> timeout = timeout ;
171170 (void )sqlite3_busy_timeout (self -> db , (int )(timeout * 1000 ));
@@ -385,9 +384,7 @@ PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
385384 }
386385
387386 rc = pysqlite_step (statement , self );
388- if (rc == SQLITE_DONE ) {
389- self -> inTransaction = 1 ;
390- } else {
387+ if (rc != SQLITE_DONE ) {
391388 _pysqlite_seterror (self -> db , statement );
392389 }
393390
@@ -418,7 +415,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
418415 return NULL ;
419416 }
420417
421- if (self -> inTransaction ) {
418+ if (! sqlite3_get_autocommit ( self -> db ) ) {
422419
423420 Py_BEGIN_ALLOW_THREADS
424421 rc = sqlite3_prepare (self -> db , "COMMIT" , -1 , & statement , & tail );
@@ -429,9 +426,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
429426 }
430427
431428 rc = pysqlite_step (statement , self );
432- if (rc == SQLITE_DONE ) {
433- self -> inTransaction = 0 ;
434- } else {
429+ if (rc != SQLITE_DONE ) {
435430 _pysqlite_seterror (self -> db , statement );
436431 }
437432
@@ -463,7 +458,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
463458 return NULL ;
464459 }
465460
466- if (self -> inTransaction ) {
461+ if (! sqlite3_get_autocommit ( self -> db ) ) {
467462 pysqlite_do_all_statements (self , ACTION_RESET , 1 );
468463
469464 Py_BEGIN_ALLOW_THREADS
@@ -475,9 +470,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
475470 }
476471
477472 rc = pysqlite_step (statement , self );
478- if (rc == SQLITE_DONE ) {
479- self -> inTransaction = 0 ;
480- } else {
473+ if (rc != SQLITE_DONE ) {
481474 _pysqlite_seterror (self -> db , statement );
482475 }
483476
@@ -1158,6 +1151,17 @@ static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self
11581151 }
11591152}
11601153
1154+ static PyObject * pysqlite_connection_get_in_transaction (pysqlite_Connection * self , void * unused )
1155+ {
1156+ if (!pysqlite_check_connection (self )) {
1157+ return NULL ;
1158+ }
1159+ if (!sqlite3_get_autocommit (self -> db )) {
1160+ Py_RETURN_TRUE ;
1161+ }
1162+ Py_RETURN_FALSE ;
1163+ }
1164+
11611165static int pysqlite_connection_set_isolation_level (pysqlite_Connection * self , PyObject * isolation_level )
11621166{
11631167 if (isolation_level == Py_None ) {
@@ -1168,7 +1172,6 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
11681172 Py_DECREF (res );
11691173
11701174 self -> begin_statement = NULL ;
1171- self -> inTransaction = 0 ;
11721175 } else {
11731176 const char * const * candidate ;
11741177 PyObject * uppercase_level ;
@@ -1606,6 +1609,7 @@ PyDoc_STR("SQLite database connection object.");
16061609static PyGetSetDef connection_getset [] = {
16071610 {"isolation_level" , (getter )pysqlite_connection_get_isolation_level , (setter )pysqlite_connection_set_isolation_level },
16081611 {"total_changes" , (getter )pysqlite_connection_get_total_changes , (setter )0 },
1612+ {"in_transaction" , (getter )pysqlite_connection_get_in_transaction , (setter )0 },
16091613 {NULL }
16101614};
16111615
@@ -1667,7 +1671,6 @@ static struct PyMemberDef connection_members[] =
16671671 {"NotSupportedError" , T_OBJECT , offsetof(pysqlite_Connection , NotSupportedError ), READONLY },
16681672 {"row_factory" , T_OBJECT , offsetof(pysqlite_Connection , row_factory )},
16691673 {"text_factory" , T_OBJECT , offsetof(pysqlite_Connection , text_factory )},
1670- {"in_transaction" , T_BOOL , offsetof(pysqlite_Connection , inTransaction ), READONLY },
16711674 {NULL }
16721675};
16731676
0 commit comments