Claude Code database

ORM Lazy Loading Disabled, Performance Regression

After refactoring ORM relationship configurations, related entities stopped lazy loading. Now every query that accesses relationships triggers additional database queries, causing severe N+1 query problems and timeouts.

The ORM configuration was changed to eager loading for debugging but wasn't reverted, or the lazy loading annotations were accidentally removed during refactoring.

Error Messages You Might See

LazyInitializationException: could not initialize proxy No session or session was closed Connection pool exhausted waiting for connections
LazyInitializationException: could not initialize proxyNo session or session was closedConnection pool exhausted waiting for connections

Common Causes

  1. All relationships set to FetchType.EAGER instead of LAZY
  2. Lazy loading disabled in ORM configuration globally
  3. Missing @Lazy annotation on relationship fields
  4. Relationship loaded outside of active transaction session
  5. Serialization attempt forcing full relationship materialization

How to Fix It

Set relationships to FetchType.LAZY (default). Use eager loading only for relationships always needed (profile picture, user name). Enable lazy loading in ORM config. Use @Transactional on service methods to keep session open. Load required relationships in same query with JOIN FETCH or explicit initialization.

Real developers can help you.

Luca Liberati Luca Liberati I work on monoliths and microservices, backends and frontends, manage K8s clusters and love to design apps architecture Vlad Temian Vlad Temian 15+ years shipping production infrastructure for startups. Former CTO at qed.builders (acquired by The Sandbox). Cursor ambassador and agentic tooling builder. I've scaled systems, automated deployments, and built observability tools for AI coding workflows. I specialize in taking vibe-coded apps from broken prototype to production-ready: fixing Supabase auth/RLS, Stripe integrations, deployment pipelines, and cleaning up AI-generated spaghetti. I build tools in this space (agentprobe, claudebin, micode) and understand both sides: how AI generates code and why it breaks. https://blog.vtemian.com/ Taufan Taufan I’m a product-focused engineer and tech leader who builds scalable systems and turns ideas into production-ready platforms. Over the past years, I’ve worked across startups and fast-moving teams, leading backend architecture, improving system reliability, and shipping products used by thousands of users. My strength is not just writing code — but connecting product vision, technical execution, and business impact. Richard McSorley Richard McSorley Full-Stack Software Engineer with 8+ years building high-performance applications for enterprise clients. Shipped production systems at Walmart (4,000+ stores), Cigna (20M+ users), and Arkansas Blue Cross. 5 patents in retail/supply chain tech. Currently focused on AI integrations, automation tools, and TypeScript-first architectures. Matthew Butler Matthew Butler Systems Development Engineer @ Amazon Web Services Nam Tran Nam Tran 10 years as fullstack developer Sage Fulcher Sage Fulcher Hey I'm Sage! Im a Boston area software engineer who grew up in South Florida. Ive worked at a ton of cool places like a telehealth kidney care startup that took part in a billion dollar merger (Cricket health/Interwell health), a boutique design agency where I got to work on a ton of exciting startups including a photography education app, a collegiate Esports league and more (Philosophie), a data analytics as a service startup in Cambridge (MA) as well as at Phillips and MIT Lincoln Lab where I designed and developed novel network security visualizations and analytics. I've been writing code and furiously devoted to using computers to make people’s lives easier for about 17 years. My degree is in making computers make pretty lights and sounds. Outside of work I love hip hop, the Celtics, professional wrestling, magic the gathering, photography, drumming, and guitars (both making and playing them) Anthony Akpan Anthony Akpan Developer with 8 years of experience building softwares fro startups rayush33 rayush33 JavaScript (React.js, React Native, Node.js) Developer with demonstrated industry experience of 4+ years, actively looking for opportunities to hone my skills as well as help small-scale business owners with solutions to technical problems Costea Adrian Costea Adrian Embedded Engineer specilizing in perception systems. Latest project was a adas camera calibration system.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

When should relationships be eager loaded?

Only load eagerly if the relationship is ALWAYS used with the parent. Use explicit loading strategies (JOIN FETCH, @EntityGraph) instead.

How to fix LazyInitializationException?

Keep the database session open with @Transactional, or pre-load the relationship within the transaction using JOIN FETCH.

How to detect N+1 problems?

Enable SQL logging. Look for 1 parent query + N identical child queries. Each query executed separately = N+1 problem.

Related Claude Code Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help