@@ -766,27 +766,47 @@ def update_vendors(self, cve_data):
766
766
return updated_severity , updated_affected
767
767
768
768
def db_open_and_get_cursor (self ) -> sqlite3 .Cursor :
769
- """Opens connection to SQLite database with retry logic to handle 'database is locked' errors."""
769
+ """Opens connection to SQLite database with retry logic and cursor existence check."""
770
+
771
+ # If we already have a valid connection, try to use it
772
+ if self .connection :
773
+ try :
774
+ cursor = self .connection .cursor ()
775
+ if cursor is None :
776
+ LOGGER .error ("Database cursor does not exist" )
777
+ raise CVEDBError ("Cursor creation failed." )
778
+ return cursor
779
+ except sqlite3 .Error as e :
780
+ LOGGER .warning (f"Existing DB connection failed, retrying from scratch. Error: { e } " )
781
+ self .connection = None # Reset and retry below
770
782
771
783
retries = 0
772
784
while retries < MAX_RETRIES :
773
785
try :
774
- self .connection = sqlite3 .connect (self .dbpath , timeout = 10 ) # Extend timeout for better handling
775
- return self .connection .cursor ()
786
+ self .connection = sqlite3 .connect (self .dbpath , timeout = 10 )
787
+ cursor = self .connection .cursor ()
788
+ if cursor is None :
789
+ LOGGER .error ("Database cursor does not exist" )
790
+ raise CVEDBError ("Cursor creation failed." )
791
+ return cursor
776
792
except sqlite3 .OperationalError as e :
777
793
if "database is locked" in str (e ):
778
794
retries += 1
779
- wait_time = RETRY_DELAY * (2 ** retries ) # Exponential backoff
795
+ wait_time = RETRY_DELAY * (2 ** retries )
780
796
LOGGER .warning (f"Database locked, retrying in { wait_time :.2f} seconds..." )
781
797
time .sleep (wait_time )
782
798
else :
783
799
LOGGER .error (f"Database operation failed: { e } " )
784
- raise CVEDBError (f"Database operation failed: { e } " ) # Ensuring consistent error handling
800
+ raise CVEDBError (f"Database operation failed: { e } " )
801
+ except sqlite3 .Error as e :
802
+ LOGGER .error (f"Unexpected database error: { e } " )
803
+ raise CVEDBError (f"Unexpected database error: { e } " )
785
804
786
805
LOGGER .error ("Max retries reached. Could not obtain database connection." )
787
806
raise CVEDBError ("Failed to open database connection after multiple attempts" )
788
807
789
808
809
+
790
810
def db_close (self ) -> None :
791
811
"""Closes connection to sqlite database."""
792
812
if self .connection :
0 commit comments