{"record":{"id":"84871acd5600fe7f","repo":"dgraph-io/badger","slug":"cannot-use-newwritebatchat-with-manageddb-false-u","errorCode":null,"errorMessage":"cannot use NewWriteBatchAt with managedDB=false. Use NewWriteBatch instead","messagePattern":"cannot use NewWriteBatchAt with managedDB=false\\. Use NewWriteBatch instead","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"managed_db.go","lineNumber":36,"sourceCode":"// NewTransactionAt follows the same logic as DB.NewTransaction(), but uses the\n// provided read timestamp.\n//\n// This is only useful for databases built on top of Badger (like Dgraph), and\n// can be ignored by most users.\nfunc (db *DB) NewTransactionAt(readTs uint64, update bool) *Txn {\n\tif !db.opt.managedTxns {\n\t\tpanic(\"Cannot use NewTransactionAt with managedDB=false. Use NewTransaction instead.\")\n\t}\n\ttxn := db.newTransaction(update, true)\n\ttxn.readTs = readTs\n\treturn txn\n}\n\n// NewWriteBatchAt is similar to NewWriteBatch but it allows user to set the commit timestamp.\n// NewWriteBatchAt is supposed to be used only in the managed mode.\nfunc (db *DB) NewWriteBatchAt(commitTs uint64) *WriteBatch {\n\tif !db.opt.managedTxns {\n\t\tpanic(\"cannot use NewWriteBatchAt with managedDB=false. Use NewWriteBatch instead\")\n\t}\n\n\twb := db.newWriteBatch(true)\n\twb.commitTs = commitTs\n\twb.txn.commitTs = commitTs\n\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.","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/managed_db.go#L18-L54","documentation":"Badger's DB.NewWriteBatchAt allows the caller to set an explicit commit timestamp and is reserved for managed mode. If the DB was opened without managed transactions (WithManagedTxns(false), the default), the library panics with this message and directs you to NewWriteBatch. It is a guard against mixing timestamp-controlled writes with ordinary commit-timestamp allocation.","triggerScenarios":"Calling db.NewWriteBatchAt(commitTs) on a DB opened without WithManagedTxns(true). The panic fires immediately at the top of NewWriteBatchAt (managed_db.go:36) before any batch is created.","commonSituations":"Porting bulk-loading or replication code from a managed-mode deployment to a plain application DB; copy-pasted loader code that sets commit timestamps; forgetting that the default options do not enable managed transactions.","solutions":["Open the DB with WithManagedTxns(true) so NewWriteBatchAt is permitted.","If explicit commit timestamps are unnecessary, use db.NewWriteBatch() and let Badger assign timestamps.","Ensure all write paths in the app agree on managed vs non-managed mode; mixing both At and non-At APIs on one DB invites this panic.","Audit callers of NewWriteBatchAt and pair each with a DB opened via a shared helper that sets the managed flag."],"exampleFix":"// before\nopts := badger.DefaultOptions(dir)\ndb, _ := badger.Open(opts)\nwb := db.NewWriteBatchAt(42)\n// after\nopts := badger.DefaultOptions(dir).WithManagedTxns(true)\ndb, _ := badger.Open(opts)\nwb := db.NewWriteBatchAt(42)\n// ...or, without managed mode:\nwb := db.NewWriteBatch()","handlingStrategy":"validation","validationCode":"var wb *badger.WriteBatch\nif managedMode { // the flag you passed via WithManagedTxns\n    wb = db.NewWriteBatchAt(commitTs)\n} else {\n    wb = db.NewWriteBatch()\n}","typeGuard":"func canUseManagedBatch(managedTxns bool) bool { return managedTxns }","tryCatchPattern":"func safeNewWriteBatchAt(db *badger.DB, commitTs uint64) (wb *badger.WriteBatch, err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"NewWriteBatchAt requires managed mode: %v\", r)\n        }\n    }()\n    wb = db.NewWriteBatchAt(commitTs)\n    return\n}","preventionTips":["Centralize badger.Open so the managed flag is set wherever timestamped batches are used","Use NewWriteBatch by default; only reach for NewWriteBatchAt when commit timestamps must be controlled","Check caller utilities (loaders, benchmarks) for NewWriteBatchAt and align their open options","Wrap batch constructors in helpers that take the DB plus a managed-mode flag"],"tags":["go","badger","managed-transactions","write-batch","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"}