0

I am using react-native-sqlite-storage in my app. How I can enable WAL to achieve non-locking read operations. basically, I enabled WAL using PRAGMA journal_mode=WAL but still no change, my read queries are still blocked.

How can I achieve WAL with react-native-sqlite-storage? I am new to SQLite

Suresh Prajapati
  • 3,991
  • 5
  • 26
  • 38

1 Answers1

0

For all those who are not aware of the topic questioned, let me tell you that what is WAL, it is Write.Ahead Logging. What does it mean? basically, the concept is to improve the write operation on DB.

We all don't know how third party lib works, how efficient they are? until we get deep into the third party we don't know it's efficiency. react-native-sqlite-storage is a whizzo work so nothing to bother about however we have to implement few things feature on our side to make things look better.

PRAGMA journal_mode=WAL is a query to add new data to the write-ahead logging file. I mean whenever we write new data in DB then what happens is that it copies the previously written data to the rollback files and the new data is written directly to the DB file Thus, it is not a good practice when large amount of data is written in the DB files hence, results in two disk write for every DB change.

WAL can make you live peacefully by not writing new data to the DB directly instead of it writes the new data to the logging file Hence, this results in single disk write.

Now let's how it works:

In the below code snippet when you open the DB for the first time this function will be invoked and you can populate your DB by executing another query in this function after mode query.

populateDatabase(db) {
    const selectMode = 'PRAGMA journal_mode=WAL;'
    db.executeSql(selectMode, [],
            () => {
              console.log('running query...')
            },
            (error) => {
              console.log('error implementing', error)
            })
  }

Everything seems cool. Please don't hesitate to ask questions.

Cheers :)

EDIT 1:

There is a performance issue while using WAL, Performance for reads can suffer from a large WAL file because data has to be searched in two places.

Codesingh
  • 3,316
  • 1
  • 11
  • 18
  • The reason I enabled WAL is to avoid locking in reading while the write operation is running. But enabling WAL yet doesn't help :(. My read queries are blocked while writes are in progress. – Suresh Prajapati Jun 24 '17 at 12:33
  • Thank you for your time. I agree that data has to be searched in two places. So what one can do to solve this locking problem?. Please check https://stackoverflow.com/questions/8104832/sqlite-simultaneous-reading-and-writing#answer-16205732 Any other suggestions are welcomed. – Suresh Prajapati Jun 24 '17 at 12:48
  • you can use async await,setTimeout, callbacks, promises to wait for one function to complete execution. – Codesingh Jun 24 '17 at 12:52