andpor / react-native-sqlite-storage

Full featured SQLite3 Native Plugin for React Native (Android and iOS)
MIT License
2.74k stars 521 forks source link

No rollback on failed transactions #566

Open laurent22 opened 6 months ago

laurent22 commented 6 months ago

I'm trying to get transactions to work using this library, but some reason rollback is not working. My expectation is that if any statement in the transaction failed, the whole transaction should be rollbacked. Instead it appears to commit all statements except the failed one.

Below is my test code. Any idea what might be the issue?

const queries = [
    {
        sql: `DROP TABLE IF EXISTS testing`,
        params: [],
    },
    {
        sql: `
            CREATE TABLE testing (
                id TEXT PRIMARY KEY,
                title TEXT NOT NULL DEFAULT ""
            )`,
        params: [],
    },
    {
        sql: `INSERT INTO testing (id, title) VALUES (?, ?)`,
        params: ["1", "one"],
    },
    {
        sql: `INSERT INTO doesnotexist (id, title) VALUES (?, ?)`,
        params: ["2", "two"],
    },
]

try {
    await db.transaction(tx => {
        for (const query of queries) {
            tx.executeSql(query.sql, query.params);
        }
    });
} catch (error) {
    console.error('SQL error:', error);
}

const result = await db.executeSql('SELECT * FROM testing');

// Displays "["1", "one"]", which shows that the transaction was not rollbacked:

console.info('RESULT:', result[0].rows.item(0));
LaGregance commented 1 month ago

Hi,

I just tested and I confirm transaction are not rollback on Android (it work fine on iOS).

I've created a fork of this repository that bring multiple things:

I hope that this fork will become the new reference for using SQLite with react native and will be regularly updated.
Do you have any idea of how to address this issue ? Or, alternatively, a direction in which to look for a solution (in which file it happens, for example) ? If possible I would be glad to add a fix in the fork.

The fork: https://www.npmjs.com/package/@boltcode/react-native-sqlite-storage

If you want to understand what I've done in this fork, there is a documentation about it.

Have a good day.