Internal adapter does not respect transaction context when using secondary storage. #2822

Closed
opened 2026-03-13 10:22:21 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @shadiramadan on GitHub (Feb 4, 2026).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Setup better auth with drizzle secondary storage and enable transactions.

Current vs. Expected behavior

The session user object is null.

The user object is expected. Disabling transactions resolves the issue.

This section of code is the issue (createInternalAdapter -> createSession -> createWithHooks) :

					const user = await adapter.findOne({
						model: "user",
						where: [{
							field: "id",
							value: userId
						}]
					});

It also seems to be the only call that does not do: (await getCurrentAdapter(adapter)) and uses the adapter directly.

What version of Better Auth are you using?

1.4.18

System info

> $ pnpm dlx @better-auth/cli info --json
{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 25.2.0: Tue Nov 18 21:07:05 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6020",
    "release": "25.2.0",
    "cpuCount": 12,
    "cpuModel": "Apple M2 Max",
    "totalMemory": "32.00 GB",
    "freeMemory": "0.11 GB"
  },
  "node": {
    "version": "v24.12.0",
    "env": "development"
  },
  "packageManager": {
    "name": "pnpm",
    "version": "10.26.0"
  },
  "frameworks": null, # Next.js
  "databases": [
    {
      "name": "pg",
      "version": "catalog:libs" # ^8
    },
    {
      "name": "drizzle",
      "version": "catalog:libs" # drizzle-orm: ^0.45.1, drizzle-kit: ^0.31.8
    }
  ],
  "betterAuth": {
    "version": "Unknown", # ^1.4.18
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
  database: drizzleAdapter(db, {
    provider: "pg",
    // Disabled: causes user to be null in secondary storage hook during registration
    // because the hook reads outside the transaction context
    transaction: false,
  }),
});

Additional context

No response

Originally created by @shadiramadan on GitHub (Feb 4, 2026). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Setup better auth with drizzle secondary storage and enable transactions. ### Current vs. Expected behavior The session user object is null. The user object is expected. Disabling transactions resolves the issue. This section of code is the issue (createInternalAdapter -> createSession -> createWithHooks) : ```ts const user = await adapter.findOne({ model: "user", where: [{ field: "id", value: userId }] }); ``` It also seems to be the only call that does not do: `(await getCurrentAdapter(adapter))` and uses the adapter directly. ### What version of Better Auth are you using? 1.4.18 ### System info ```bash > $ pnpm dlx @better-auth/cli info --json { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 25.2.0: Tue Nov 18 21:07:05 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6020", "release": "25.2.0", "cpuCount": 12, "cpuModel": "Apple M2 Max", "totalMemory": "32.00 GB", "freeMemory": "0.11 GB" }, "node": { "version": "v24.12.0", "env": "development" }, "packageManager": { "name": "pnpm", "version": "10.26.0" }, "frameworks": null, # Next.js "databases": [ { "name": "pg", "version": "catalog:libs" # ^8 }, { "name": "drizzle", "version": "catalog:libs" # drizzle-orm: ^0.45.1, drizzle-kit: ^0.31.8 } ], "betterAuth": { "version": "Unknown", # ^1.4.18 } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, database: drizzleAdapter(db, { provider: "pg", // Disabled: causes user to be null in secondary storage hook during registration // because the hook reads outside the transaction context transaction: false, }), }); ``` ### Additional context _No response_
GiteaMirror added the adapter label 2026-03-13 10:22:21 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Feb 4, 2026):

It sounds like you're encountering an issue where the internal adapter in Better Auth does not respect transaction context when using secondary storage, leading to the session user object being null.

From what I can gather, a potential solution would be to ensure that you are utilizing getCurrentAdapter(adapter) when fetching the user. This allows the transaction-aware adapter to handle lookups correctly, which can help prevent deadlocks or null values [2].

In your setup, it seems that disabling transactions resolves the issue, but this is not ideal. If the problem persists, you may consider exploring other issues related to the internal adapter as there are known discussions around the adapter handling transactions and secondary storage [1].

Have you had the chance to try integrating getCurrentAdapter in your calls? If not, it could be a useful step to see if that resolves the issue. Additionally, if you're still struggling, don't hesitate to tag @bekacru or @ping__ for further assistance! What specific adjustments are you planning to try next?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Feb 4, 2026): It sounds like you're encountering an issue where the internal adapter in Better Auth does not respect transaction context when using secondary storage, leading to the session user object being null. From what I can gather, a potential solution would be to ensure that you are utilizing `getCurrentAdapter(adapter)` when fetching the user. This allows the transaction-aware adapter to handle lookups correctly, which can help prevent deadlocks or null values [[2]](https://github.com/better-auth/better-auth/issues/2127). In your setup, it seems that disabling transactions resolves the issue, but this is not ideal. If the problem persists, you may consider exploring other issues related to the internal adapter as there are known discussions around the adapter handling transactions and secondary storage [[1]](https://github.com/better-auth/better-auth/pull/7592). Have you had the chance to try integrating `getCurrentAdapter` in your calls? If not, it could be a useful step to see if that resolves the issue. Additionally, if you're still struggling, don't hesitate to tag @bekacru or @ping__ for further assistance! What specific adjustments are you planning to try next? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@shadiramadan commented on GitHub (Feb 4, 2026):

Looks like this is already fixed in the unreleased beta.

(I did try the beta but I had so many broken type issues I had to revert).

Closing this issue.

ed68d3d3c9/packages/better-auth/src/db/internal-adapter.ts (L361-L371)

@shadiramadan commented on GitHub (Feb 4, 2026): Looks like this is already fixed in the unreleased beta. (I did try the beta but I had so many broken type issues I had to revert). Closing this issue. https://github.com/better-auth/better-auth/blob/ed68d3d3c9f040f0a464384353f7c3eea4472088/packages/better-auth/src/db/internal-adapter.ts#L361-L371
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2822