LogoSkills

serverpod-sessions

Serverpod session types, lifecycle, InternalSession, cleanup callbacks. Use when creating manual sessions, debugging session-closed errors, or understanding session lifecycle.

Serverpod Sessions#

ํ•œ๋งˆ๋””๋กœ (๋น„๊ฐœ๋ฐœ์ž์šฉ)#

์„ธ์…˜(Session)์€ ์„œ๋ฒ„๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋™์•ˆ ๋“ค๊ณ  ๋‹ค๋‹ˆ๋Š” "์ถœ์ž…์ฆ ๊ฒธ ์ž‘์—… ๊ฐ€๋ฐฉ"์ด๋ผ๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ€๋ฐฉ ์•ˆ์—๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์บ์‹œ, ์ €์žฅ์†Œ, ๋กœ๊ทธ ๊ฐ™์€ ๋„๊ตฌ๊ฐ€ ๋“ค์–ด ์žˆ์–ด, ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ ํ•˜๋‚˜๋ฅผ ๋๋‚ผ ๋•Œ๊นŒ์ง€ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ๊บผ๋‚ด ์”๋‹ˆ๋‹ค. ๋ณดํ†ต์€ ์„œ๋ฒ„๊ฐ€ ์•Œ์•„์„œ ๊ฐ€๋ฐฉ์„ ์ฑ™๊ฒจ์ฃผ๊ณ  ๋๋‚˜๋ฉด ๋ฐ˜๋‚ฉํ•˜์ง€๋งŒ, ์ง์ ‘ ๋งŒ๋“  ๊ฐ€๋ฐฉ(InternalSession)์€ ์‚ฌ๋žŒ์ด ์†์ˆ˜ ๋ฐ˜๋‚ฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ๋Š” ๊ทธ ๊ฐ€๋ฐฉ์„ ์–ธ์ œ ๋งŒ๋“ค๊ณ  ์–ด๋–ป๊ฒŒ ์ œ๋Œ€๋กœ ๋‹ซ๋Š”์ง€๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

๋ฌด์—‡์„ยท์–ธ์ œ (๋น„๊ฐœ๋ฐœ์ž์šฉ)#

  • ๋ฌด์—‡์„ ํ•ด์ฃผ๋‚˜: ์„ธ์…˜์˜ ์ข…๋ฅ˜, ์ƒ์„ฑยท์ข…๋ฃŒ(์ƒ๋ช…์ฃผ๊ธฐ), ์ง์ ‘ ๋งŒ๋“œ๋Š” InternalSession, ๋‹ซํžˆ๊ธฐ ์ง์ „ ์ฒ˜๋ฆฌ(cleanup) ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด ์ค๋‹ˆ๋‹ค.
  • ์–ธ์ œ ์“ฐ๋‚˜:
    • ์ง์ ‘(์ˆ˜๋™์œผ๋กœ) ์„ธ์…˜์„ ๋งŒ๋“ค์–ด์•ผ ํ•  ๋•Œ
    • "์„ธ์…˜์ด ์ด๋ฏธ ๋‹ซํ˜”๋‹ค"๋Š” ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚˜ ์›์ธ์„ ์ฐพ์„ ๋•Œ
    • ์„ธ์…˜์ด ์–ธ์ œ ๋งŒ๋“ค์–ด์ง€๊ณ  ์–ธ์ œ ๋‹ซํžˆ๋Š”์ง€ ํ๋ฆ„์„ ์ดํ•ดํ•˜๊ณ  ์‹ถ์„ ๋•Œ
  • ํ•ต์‹ฌ ์ฃผ์˜: ์ง์ ‘ ๋งŒ๋“  ์„ธ์…˜์„ ๋‹ซ์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ƒˆ๊ณ  ๊ธฐ๋ก(๋กœ๊ทธ)๋„ ๋‚จ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ "ํ•ญ์ƒ ๋‹ซ๊ธฐ"๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ์šฉ์–ด (๋น„๊ฐœ๋ฐœ์ž์šฉ)#

์šฉ์–ด์‰ฌ์šด ์„ค๋ช…
Session(์„ธ์…˜)์„œ๋ฒ„๊ฐ€ ํ•œ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋™์•ˆ ์“ฐ๋Š” ๋„๊ตฌ ๋ฌถ์Œ(๊ฐ€๋ฐฉ). ๋๋‚˜๋ฉด ๋ฐ˜๋‚ฉ(๋‹ซ๊ธฐ)ํ•จ
InternalSession์‚ฌ๋žŒ์ด ์ง์ ‘ ๋งŒ๋“ค๊ณ  ์ง์ ‘ ๋‹ซ์•„์•ผ ํ•˜๋Š” ์„ธ์…˜
lifecycle(์ƒ๋ช…์ฃผ๊ธฐ)์„ธ์…˜์ด ์ƒ๊ธฐ๊ณ  ์‚ด์•„ ์žˆ๋‹ค๊ฐ€ ๋‹ซํžˆ๋Š” ์ „์ฒด ํ๋ฆ„
cleanup callback์„ธ์…˜์ด ๋‹ซํžˆ๊ธฐ ์ง์ „์— ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋งˆ๋ฌด๋ฆฌ ์ฒ˜๋ฆฌ
StateError์ด๋ฏธ ๋‹ซํžŒ ์„ธ์…˜์„ ๋‹ค์‹œ ์“ฐ๋ ค ํ•  ๋•Œ ๋‚˜๋Š” ์˜ค๋ฅ˜
future call๋‚˜์ค‘์—(์ง€์—ฐ ํ›„) ์‹คํ–‰ํ•˜๋„๋ก ์˜ˆ์•ฝํ•˜๋Š” ์ž‘์—…. ๋‹ซํžŒ ์„ธ์…˜์„ ๋ถ™์žก๋Š” ๋Œ€์‹  ์‚ฌ์šฉ

A Session provides access to database, cache, storage, messages, passwords, and logging. The framework creates and closes sessions automatically; only InternalSession requires manual management.

Session types#

TypeCreated forLifetime
MethodCallSessionEndpoint methodsSingle request
WebCallSessionWeb server routesSingle request
MethodStreamSessionStream methodsStream duration
StreamingSessionWebSocket connectionsConnection duration
FutureCallSessionFuture callsTask execution
InternalSessionManual creationUntil closed

Manual sessions (InternalSession)#

Always close in a finally block:

var session = await Serverpod.instance.createSession();
try {
  await doWork(session);
} finally {
  await session.close();
}

Unclosed sessions leak memory and never persist logs. Using a closed session throws StateError.

Cleanup callbacks#

session.addWillCloseListener((session) async {
  // Runs just before session closes (all session types)
});

Common pitfall: using session after method returns#

Sessions close when the endpoint returns. Do not capture for later use:

// BAD โ€” session already closed when callback runs
Timer(Duration(seconds: 5), () => user.updateLastSeen(session));

Fix: Use a future call (session.serverpod.futureCalls.callWithDelay(...)) or create a new InternalSession inside the callback.