kvdb/postgres: use readonly db transaction if possible
This commit is contained in:
1 file changed
+11
-1
@@ -4,6 +4,7 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -39,7 +40,16 @@ func newReadWriteTx(db *db, readOnly bool) (*readWriteTx, error) {
|
|||||||
}
|
}
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
|
|
||||||
tx, err := db.db.Begin()
|
// Start the transaction. Don't use the timeout context because it would
|
||||||
|
// be applied to the transaction as a whole. If possible, mark the
|
||||||
|
// transaction as read-only to make sure that potential programming
|
||||||
|
// errors cannot cause changes to the database.
|
||||||
|
tx, err := db.db.BeginTx(
|
||||||
|
context.Background(),
|
||||||
|
&sql.TxOptions{
|
||||||
|
ReadOnly: readOnly,
|
||||||
|
},
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
locker.Unlock()
|
locker.Unlock()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in new issue
Block a user