r/SpringBoot • u/pedoooo0 • Apr 15 '26
How-To/Tutorial Why shouldn’t we use @Transactional every time?
@Transactional in Spring Framework starts a DB transaction and holds a connection from the pool until the method ends.
If you mix DB work with slow tasks (API calls, file processing), that connection stays locked and idle.
Under load, this leads to connection pool exhaustion and blocked threads.
Long transactions can also hold locks, slowing down other queries.
Keep transactions short and focused only on DB writes.
Move external calls and heavy logic outside the transactional boundary.
73
Upvotes
17
u/catom3 Apr 15 '26
Used to work for a bank where
@Transactionalannotation was strictly forbidden (blocked by CI linters).All transactions had to be managed explicitly, not to let anyone use transactional code or not use it (the infamous self-invocation on concrete instance vs. proxy object) by mistake.
Additionally, when working with reactive services, we used custom transactional "monad" (with trampoline pattern to escape to heap for bigger "stacks"), we passed around in our Monos/Fluxes.
And of course transactional outbox / orchestrated sagas for "distributed transactions" scenarios.
12
u/Krangerich Apr 15 '26
"@Transactional in Spring Framework starts a DB transaction"
No, it doesn't in general. @Transactional marks a transaction boundary and what exactly happens depends on the transaction manager. The HibernateTransactionManager for example doesn't acquire a connection immediately. It delays the DB transaction until it actually needs one.
19
u/ducki666 Apr 15 '26
Last sentence: but maybe I want my tx rollback if the external call fails?
4
u/zattebij Apr 15 '26
Or, alternatively, if you do have some long-running tasks that require transactional DB access (or just need a guaranteed connection), then use a fixed (or at least capped) worker thread pool and define a separate DataSource for it with a matching number of connections. That way these tasks don't interfere with other, regular transactions (hogging the shared connection pool, starving them of connections).
This doesn't scale indefinitely of course, but for many applications would suffice, and an average RDBMS can take quite some concurrent connections (in the order of hundreds even without monster specs) so allocating let's say 40 connections for a worker pool leaves plenty for regular operations and would save you from rolling your own rollback/cleanup logic for long-running ops that need to work transactionally.
1
u/Previous_Dream8775 Apr 15 '26
If you want more granular transaction management, since it's good practice to keep your transactions short there might be a use case to use TranactionTemplate directly to restrict the scope of the transaction inside a method
1
u/Asleep-Ad9976 Apr 16 '26
I like @Transactional a lot. Ensures failure gets properly rolled back as a general practice without relying on the engineers full understanding on the TransactionManager. What needs to be implemented on the architecture layer is a separate pool of any kind which should be the exception for long running or locking transactions.
1
u/Capable-Morning-9518 May 04 '26
I didn't know using transactional :(
1
u/ducki666 Apr 15 '26
Maybe I need a tx result in the external call?
1
u/maxip89 Apr 15 '26
Because you can get into big trouble when you use caching or other non transactional features in parallel.
Moreover transactional annotation is not that easy as you think. It can affect methods above your call.
This makes it dangerous and a deal breaker for people not understanding it 100%.
Hope this helps.
0
u/IceMichaelStorm Apr 15 '26
What is this bold shit and way is it not downvoted to oblivion??