Boost Your Next.js App Performance: Avoid Prisma.$transaction BottlenecksBoost Your Next.js App Performance: Avoid Prisma.$transaction Bottlenecks
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
How prisma.$transaction Can Quietly Kill Your Next.js App Performance (And How to Fix It)
When building high-performance e-commerce platforms, database optimization is everything. I recently tackled a notorious bottleneck in my Next.js (App Router + Turbopack) application that was triggering a heavy Prisma P2028 Timeout Error and occasionally causing connection drops (Connection terminated unexpectedly).
❌ The Culprit
I was fetching products and their total count for pagination using Prisma's default transaction wrapper:
const [products, totalCount] = await prisma.$transaction([ prisma.product.findMany({ skip, take, include: { variants: true } }), prisma.product.count() ]);
While this looks clean, $transaction forces the database to isolate resources and lock connections until both queries resolve sequentially or tightly together. Under heavy local state refreshes or network latency, this blocks the connection pool, leading to massive 16.6s response times and abrupt crashes.
💡 The Fix: Parallel Execution via Promise.all
Since these are read-only queries that don’t depend on each other's state, a transaction wrapper is completely unnecessary.
I refactored the code to fetch data concurrently using Promise.all:
TypeScript
const [products, totalCount] = await Promise.all([
prisma.product.findMany({ skip, take, include: { variants: true }, orderBy: { createdAt: "desc" } }),
prisma.product.count()
]);

⚡ The Results
Timeout Errors: Eliminated completely (P2028 resolved).
Performance Boost: Response times plummeted from 16,633ms down to just 136ms!
UX Improvement: Smoother client-side transitions and zero unhandled server rejections.
Takeaway: Don't bundle independent read operations into transactions. Let them run in parallel to keep your connection pool healthy and your Lighthouse scores flawless.
#NextJS #Prisma #DatabaseOptimization #FullStack #WebPerf
Post image
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started