Add support for the ReadOnly option in BeginTx#1372
Add support for the ReadOnly option in BeginTx#1372jmanero wants to merge 1 commit intomattn:masterfrom
Conversation
This change is similar to the modernc implementation, which ignores the transaction mode defined in the `_txlock` DSN parameter when the `ReadOnly` attribute in `TxOptions` values passed to `BeginTx` is `true`
|
The fact that this doesn't actually make the transaction readonly seems very confusing. I understand that having the side effect of doing To that end, I think we need to leverage |
|
Agreed. Also thinking through the unexpected behavior changes that this could cause to anything that's currently setting
Would you be open to this ^ kind of change @rittneje? |
|
I'm not sure what the use case for the proposed "weak" mode is. With regards to backwards compatibility, it seems unfortunate that people who explicitly set Whether |
|
I'm not sure this change is necessary. If you need different transaction behaviors for read-only vs read-write operations, you should use separate connections: // For writes: use immediate to prevent lock contention
writeDB, _ := sql.Open("sqlite3", "file.db?_journal_mode=WAL&_txlock=immediate")
writeDB.SetMaxOpenConns(1)
// For reads: use deferred or read-only mode
readDB, _ := sql.Open("sqlite3", "file.db?_journal_mode=WAL&_txlock=deferred")
// or even safer:
readDB, _ := sql.Open("sqlite3", "file.db?_journal_mode=WAL&mode=ro")This approach:
The problem you're trying to solve seems like an application design concern rather than something the driver should handle. |
|
@mattn I'd certainly agree that having two pools that can be independently configured is the best approach in general. However, I do still think this library ought to respect |
This change is similar to the modernc implementation, which ignores the transaction mode defined in the
_txlockDSN parameter when theReadOnlyattribute inTxOptionsvalues passed toBeginTxistrueWhile the resulting transactions are not actually read-only, this change achieves the result desired by #400 within the confines of the
database/sqlinterface.In
WALmode, this allows us to use DSN options liketo enable blocking on concurrent calls to
BeginTx(ctx, &sql.TxOptions{})instead of polling forSQLITE_BUSYerror codes, while making non-blocking read-only(ish) calls toBeginTx(ctx, &sql.TxOptions{ReadOnly: true}).