Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring close connection after committing transaction?

I've recently read in one tutorial that Spring closes connection after transaction commit.

Is that true? I can't find anything about it in the Spring reference documentation.

What's the rationale behind it? Since now, I thought that there is a one-to-many relation between connection and transactions.

like image 552
woof-woof Avatar asked Aug 13 '13 09:08

woof-woof


People also ask

Does @transactional close session?

@Transactional helps you to extend scope of Session . Session is open first time when getCurrentSession() is executed and it is closed when transaction ends and it is flushed before transaction commits.

How does transaction work in Spring?

Transactions and Proxies. At a high level, Spring creates proxies for all the classes annotated with @Transactional, either on the class or on any of the methods. The proxy allows the framework to inject transactional logic before and after the running method, mainly for starting and committing the transaction.

How does @transactional works in Spring boot?

So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

Is Spring Service Transactional by default?

Spring offers you a PlatformTransactionManager / TransactionManager interface, which, by default, comes with a couple of handy implementations. One of them is the datasource transaction manager.


1 Answers

Spring calls close() when the transaction finishes which could be from either a commit or rollback. Whether or not close() actually closes a real JDBC connection depends on the DataSource configuration. If it's a plain JDBC connection, then it will actually close. If it is a connection pool then it will probably just be returned to the pool on close.

like image 97
AngerClown Avatar answered Sep 27 '22 17:09

AngerClown