{"record":{"id":"3287d9f50d4ac9a9","repo":"crowdsecurity/crowdsec","slug":"error-creating-transaction-w","errorCode":null,"errorMessage":"error creating transaction: %w","messagePattern":"error creating transaction: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/allowlists.go","lineNumber":142,"sourceCode":"\t}\n\n\tresult, err := q.First(ctx)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn result, nil\n}\n\nfunc (c *Client) AddToAllowlist(ctx context.Context, list *ent.AllowList, items []*models.AllowlistItem) (int, error) {\n\tadded := 0\n\n\tc.Log.Debugf(\"adding %d values to allowlist %s\", len(items), list.Name)\n\tc.Log.Tracef(\"values: %+v\", items)\n\n\ttxClient, err := c.Ent.Tx(ctx)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"error creating transaction: %w\", err)\n\t}\n\n\tfor _, item := range items {\n\t\tc.Log.Debugf(\"adding value %s to allowlist %s\", item.Value, list.Name)\n\n\t\trng, err := csnet.NewRange(item.Value)\n\t\tif err != nil {\n\t\t\tc.Log.Error(err)\n\t\t\tcontinue\n\t\t}\n\n\t\tquery := txClient.AllowListItem.Create().\n\t\t\tSetValue(item.Value).\n\t\t\tSetIPSize(int64(rng.Size())).\n\t\t\tSetStartIP(rng.Start.Addr).\n\t\t\tSetStartSuffix(rng.Start.Sfx).\n\t\t\tSetEndIP(rng.End.Addr).\n\t\t\tSetEndSuffix(rng.End.Sfx).","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/allowlists.go#L124-L160","documentation":"AddToAllowlist groups item inserts in a single ent transaction via c.Ent.Tx(ctx). If the transaction client cannot be created (the driver fails to begin a transaction), the error is wrapped as 'error creating transaction: %w'. Nothing has been written at this point.","triggerScenarios":"Client.AddToAllowlist called when the underlying DB connection is broken, the driver does not support transactions as configured, the context is already cancelled, or the connection pool is exhausted.","commonSituations":"SQLite 'database is locked' under concurrent LAPI writes; MySQL 'max_connections' exhausted; context deadline exceeded from an upstream API timeout; DB restarted mid-operation during a console allowlist import.","solutions":["Inspect the wrapped driver error; fix connectivity/pool issues (increase max connections, close leaked connections)","Ensure the passed context is not already cancelled/timed out before calling","For SQLite lock contention, reduce concurrent writers or migrate to MySQL/PostgreSQL","Retry the add after the transient DB issue clears"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if err := ctx.Err(); err != nil { return fmt.Errorf(\"context already cancelled: %w\", err) }","typeGuard":null,"tryCatchPattern":"added, err := client.AddToAllowlist(ctx, list, items)\nif err != nil && strings.Contains(err.Error(), \"error creating transaction\") {\n    // transient DB issue: retry with backoff\n    time.Sleep(backoff)\n    added, err = client.AddToAllowlist(ctx, list, items)\n}","preventionTips":["Pass a live (non-cancelled) context with a reasonable timeout","Keep DB connection pool sized for concurrent LAPI load","Prefer MySQL/Postgres over SQLite for high-concurrency writes"],"tags":["database","transaction","allowlist","sql"],"backgroundTag":"database-query-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}