1

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.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
MaxNevermind
  • 2,784
  • 2
  • 25
  • 31

1 Answers1

1

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.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • So what then "..as well as its capability to fall back to 'auto-commit'." means? If we can achieve that with no effort anyway, what does the api docs says? Does spring do something about it to fall back or not? – MaxNevermind Nov 07 '14 at 13:21
  • So you say we you are not obliged to open transaction from the client code at all. I mean we should but it won't throw any exception if we don't. I was confused with hibernate docs saying "Database, or system, transaction boundaries are always necessary." [http://docs.jboss.org](http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html_single/#transactions-demarcation) I want to try to work with hibernate it outside of spring if it works without exceptions I'll accept the answer. – MaxNevermind Nov 07 '14 at 13:55
  • Of course it works. Why to you think there's a Hibernate property such as [hibernate.connection.autocommit](https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch03.html#configuration-jdbc-properties). – Vlad Mihalcea Nov 07 '14 at 14:02