MaxNevermind
MaxNevermind

Reputation: 2933

HibernateTemplate fall back to auto-commit, where?

HibernateTemplate api says:

...its capability to fall back to 'auto-commit' style behavior when used outside of transactions

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate3/HibernateTemplate.html

Can anyone please point out where that happens:

https://github.com/

http://grepcode.com

I searched for like an hour with no success. I'm looking for the answer for when transactions are created in my app, as I understand hibernate demands a transaction to be open for every interaction with db but I don't open any transaction neither I use JTA and I still have reading operations working fine.

Upvotes: 1

Views: 690

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 154030

All database statements are executed within the context of a physical transaction, even when we don’t explicitly declare transaction boundaries (BEGIN/COMMIT/ROLLBACK). Data integrity is enforced by the ACID properties of database transactions.

So if you don't enrol your current unit of work in the context of a logical transaction (Spring transaction interceptor), then each DB statement will execute in a separate database physical transaction, hence you are going to run in "auto-commit" mode.

...its capability to fall back to 'auto-commit' style behavior when used outside of transactions

This phrase tell you that you can use TransactionTemplate from within a @Transactional context and even without a transaction context as well. So you are not obliged to use the Spring transaction demarcation, although for performance reasons you should do so.

So your TransactionTemplate related code works independently of having or not having a currently running transaction context.

With plain-old JDBC your code must explicitly declare a transaction boundary (begin/commit/rollback) if you want to enroll multiple statements in a single database transaction. If you don't do that you will run in "auto-commit" mode.

Upvotes: 1

Related Questions