You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用react-native-sqlite-storage删数据库遇事务未完成错误求解决

Fixing "database cannot be closed while a transaction is in progress" When Deleting SQLite DB in React Native

Hey there, let's work through this frustrating error you're hitting with react-native-sqlite-storage. The root cause here is exactly what the error states: there's an active transaction still running when you try to delete the database. The library can't close (and thus delete) the DB until all pending transactions are fully wrapped up.

Here's a step-by-step solution to resolve this:

1. Wrap Up All Pending Transactions

First, you need to ensure every transaction you've started is either committed or rolled back. If you have any ongoing db.transaction calls that didn't finish (maybe missing callbacks or unhandled promises), those will leave the transaction in a pending state.

For example, if you had a transaction that was supposed to run some queries, make sure it's fully resolved:

// Example: Ensure a pending transaction is completed
db.transaction(
  (tx) => {
    // Run a dummy query if there's no remaining work to trigger transaction completion
    tx.executeSql('SELECT 1 FROM sqlite_master', [], () => {});
  },
  (error) => {
    // Transaction failed, log it and proceed (we just need to wrap it up)
    console.log('Transaction rolled back due to error:', error);
  },
  () => {
    // Transaction completed successfully
    console.log('All pending transactions finished');
  }
);

2. Manually Close the Database Connection

Even after transactions are done, the database connection might still be open. Explicitly closing it before deletion ensures there are no lingering connections:

const closeDb = () => {
  return new Promise((resolve, reject) => {
    if (db) {
      db.close((error) => {
        if (error) {
          reject(error);
        } else {
          console.log('Database connection closed successfully');
          resolve();
        }
      });
    } else {
      // No open connection exists, just resolve to proceed
      resolve();
    }
  });
};

3. Delete the Database (Only After Cleanup)

Once you've wrapped up transactions and closed the connection, you can safely call deleteDatabase. Chain these operations using promises to ensure they run in the correct order:

// Put all cleanup steps together
closeDb()
  .then(() => {
    SQLite.deleteDatabase(
      { name: 'your-database-name' },
      () => {
        console.log('Database deleted successfully!');
      },
      (error) => {
        console.error('Error deleting database:', error);
      }
    );
  })
  .catch((error) => {
    console.error('Error during cleanup process:', error);
  });

Key Tips to Avoid This in the Future

  • Always handle transaction callbacks: Every db.transaction should have success and error handlers to ensure the transaction doesn't hang in a pending state.
  • Use promises for async flow: Wrapping database operations in promises makes it easier to sequence cleanup steps and avoid race conditions.
  • Check for multiple connections: If you've opened the database multiple times (e.g., in different components), make sure all instances are closed before attempting deletion.

内容的提问来源于stack exchange,提问作者Tzvetlin Velev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:04:06