]> granicus.if.org Git - pdns/commitdiff
make sqlite3 code be able to deal with destruction while get() still running
authorBert Hubert <bert.hubert@netherlabs.nl>
Tue, 10 Jan 2012 20:35:49 +0000 (20:35 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Tue, 10 Jan 2012 20:35:49 +0000 (20:35 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2336 d19b8d6e-7fed-0310-83ef-9ca221ded41b

modules/gsqlite3backend/ssqlite3.cc

index a6425e2ac84806ea3fb674513114c4690d31bf00..c59e996e6e3cfe2e6678f3fc63e8d73fd615c222 100644 (file)
@@ -26,7 +26,7 @@ SSQLite3::SSQLite3( const std::string & database )
 
   if ( sqlite3_open( database.c_str(), &m_pDB)!=SQLITE_OK )
     throw sPerrorException( "Could not connect to the SQLite database '" + database + "'" );
-    
+  m_pStmt = 0;
   sqlite3_busy_handler(m_pDB, busyHandler, 0);
 }
 
@@ -34,9 +34,18 @@ SSQLite3::SSQLite3( const std::string & database )
 SSQLite3::~SSQLite3()
 {
   int ret;
-  if((ret =sqlite3_close( m_pDB )) != SQLITE_OK) {
-    cerr<<"Unable to close down sqlite connection: "<<ret<<endl;
-    abort();
+  for(int n = 0; n < 2 ; ++n) {
+    if((ret =sqlite3_close( m_pDB )) != SQLITE_OK) {
+      if(n || !m_pStmt || ret != SQLITE_BUSY) { // if we have SQLITE_BUSY, and a working m_Pstmt, try finalize
+        cerr<<"Unable to close down sqlite connection: "<<ret<<endl;
+        abort();
+      }
+      else {
+        sqlite3_finalize(m_pStmt);
+      }
+    }
+    else
+      break;
   }
 }
 
@@ -120,6 +129,7 @@ bool SSQLite3::getRow( row_t & row )
   if(rc == SQLITE_CANTOPEN) {
     string error ="CANTOPEN error in sqlite3, often caused by unwritable sqlite3 db *directory*: "+string(sqlite3_errmsg(m_pDB));
     sqlite3_finalize(m_pStmt);
+    m_pStmt = 0;
     throw sPerrorException(error);
   }