{"record":{"id":"7f87cb04c724bb05","repo":"dgraph-io/badger","slug":"cannot-use-commitat-with-manageddb-false-use-comm","errorCode":null,"errorMessage":"Cannot use CommitAt with managedDB=false. Use Commit instead.","messagePattern":"Cannot use CommitAt with managedDB=false\\. Use Commit instead\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"managed_db.go","lineNumber":60,"sourceCode":"\treturn wb\n}\nfunc (db *DB) NewManagedWriteBatch() *WriteBatch {\n\tif !db.opt.managedTxns {\n\t\tpanic(\"cannot use NewManagedWriteBatch with managedDB=false. Use NewWriteBatch instead\")\n\t}\n\n\twb := db.newWriteBatch(true)\n\treturn wb\n}\n\n// CommitAt commits the transaction, following the same logic as Commit(), but\n// at the given commit timestamp. This will panic if not used with managed transactions.\n//\n// This is only useful for databases built on top of Badger (like Dgraph), and\n// can be ignored by most users.\nfunc (txn *Txn) CommitAt(commitTs uint64, callback func(error)) error {\n\tif !txn.db.opt.managedTxns {\n\t\tpanic(\"Cannot use CommitAt with managedDB=false. Use Commit instead.\")\n\t}\n\ttxn.commitTs = commitTs\n\tif callback == nil {\n\t\treturn txn.Commit()\n\t}\n\ttxn.CommitWith(callback)\n\treturn nil\n}\n\n// SetDiscardTs sets a timestamp at or below which, any invalid or deleted\n// versions can be discarded from the LSM tree, and thence from the value log to\n// reclaim disk space. Can only be used with managed transactions.\nfunc (db *DB) SetDiscardTs(ts uint64) {\n\tif !db.opt.managedTxns {\n\t\tpanic(\"Cannot use SetDiscardTs with managedDB=false.\")\n\t}\n\tdb.orc.setDiscardTs(ts)\n}","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/managed_db.go#L42-L78","documentation":"Txn.CommitAt commits a transaction at a caller-specified commit timestamp and therefore requires managed transactions. If the DB was opened without WithManagedTxns(true), the library panics with this message and points you to the ordinary Commit method. The panic occurs before txn.commitTs is set, so nothing is written.","triggerScenarios":"Calling txn.CommitAt(ts, cb) on a Txn obtained from a DB opened without managed mode; the check happens at the top of CommitAt (managed_db.go:60). Note the callback path: even passing a non-nil callback does not bypass the guard.","commonSituations":"Code ported from Dgraph or another timestamp-managed layered system; mixing a normal DB handle with CommitAt copied from managed-mode examples; test harnesses that set timestamps manually against default options.","solutions":["Open the DB with WithManagedTxns(true) before creating the txn that you will CommitAt.","Replace txn.CommitAt(ts, cb) with txn.Commit() (and txn.CommitWith(cb) if you need async completion) when not in managed mode.","Move DB opening into a single constructor so managed mode and At-style commit APIs cannot diverge.","If migrating an app to managed mode, update every commit site consistently — mixing Commit and CommitAt on one DB signals a configuration problem."],"exampleFix":"// before\nopts := badger.DefaultOptions(dir)\ndb, _ := badger.Open(opts)\ntxn := db.NewTransaction(true)\nerr := txn.CommitAt(200, nil)\n// after\nopts := badger.DefaultOptions(dir).WithManagedTxns(true)\ndb, _ := badger.Open(opts)\ntxn := db.NewTransaction(true)\nerr := txn.CommitAt(200, nil)\n// ...or, without managed mode:\nerr := txn.Commit()","handlingStrategy":"try-catch","validationCode":"if managedMode {\n    err := txn.CommitAt(commitTs, cb)\n} else {\n    if cb != nil {\n        txn.CommitWith(cb)\n    } else {\n        err := txn.Commit()\n    }\n}","typeGuard":"func commitAtSupported(managedTxns bool) bool { return managedTxns }","tryCatchPattern":"func safeCommitAt(txn *badger.Txn, commitTs uint64, cb func(error)) (err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"CommitAt requires managed mode: %v\", r)\n        }\n    }()\n    return txn.CommitAt(commitTs, cb)\n}","preventionTips":["Only use CommitAt on transactions from a DB opened with WithManagedTxns(true)","Use Commit (or CommitWith for callbacks) in non-managed applications","Code-review any commit path that passes an explicit timestamp and trace it back to the DB open options","Never mix Commit and CommitAt across the same application's DB handles"],"tags":["go","badger","managed-transactions","commit","panic"],"backgroundTag":"managed-mode-required","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}