-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't run two queries in same transaction with promises #60
Comments
For now, I had to omit support for transactions by doing this instead:
This achieves the correct response. I tried looking in the source code, but don't really see much for the term "transaction". Could you point me down the right path to looking more into this issue? Thanks so much, |
here is an exmaple of how I managed to get it done in case someone is interested: // sqlite transactions don't play with promises nicely.
// the fn param here shouldn't use promises, see `save` method as example.
const transactional = async (fn: (tx: Transaction) => void) => {
const db = await getDb();
const x = new Promise((resolve, reject) => {
const trans = db.transaction(fn, (e) => {
console.log('transaction failed ')
reject(e)
},
() => {
console.log('transaction finished ok')
resolve();
});
});
await x;
}; and it's called like this: async function save(image: FileMetadata) {
try {
await transactional((tx) => {
const last = (tx: Transaction) => {
// ... omitted code
const statement = `INSERT INTO table...`;
tx.executeSql(statement, vals, () => { }, sqliteErrorLogger);
}
const second = (tx: Transaction) => {
// ... omitted code
const statement = `INSERT INTO table...`;
tx.executeSql(statement, vals, last, sqliteErrorLogger);
}
tx.executeSql(`INSERT ....`, params , second, sqliteErrorLogger);
})
} catch (err) {
console.error('failed to save image', err)
}
} so inside the transaction scope you cannot use promises because sqlite will commit the transaction as far as I understood it only supports call backs (could be a bug or misunderstanding from my end) at least that's what I observed in my experiments and just a note the error call back is the most important thing to know what is going on: |
@blabadi Am right, that you're still not able to pass the results of the first |
No solution has been found for this issue yet ? |
I'd love to use SQLite in my React Native project, but I'm experiencing this issue:
sqlite.ts
index.ts
Log output is:
But if I remove the second query:
Note that
done with both
is missing. Looks like an issue with promises, because I can run these two fine using the callback style. Maybe the transaction block closes before the others are run? By the way, I'm running this on Android.Is there something I'm doing wrong here?
The text was updated successfully, but these errors were encountered: