What will happen if we begin transaction in hibernate but do not commit it?
2015-09-16 10:08
489 查看
What will happen if we begin transaction in hibernate, then do some transaction but do not commit it? Will it save tempervoraly or it will rollback immediately?
Look at the following code, which accesses the database with transaction boundaries without use of commit:
By default, in a Java SE environment with a JDBC configuration, this is what happens if you execute this snippet:
A new
If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction
The call to
The SELECT is executed inside this JDBC transaction. The Session is closed, and the connection is returned to the pool and released by Hibernate — Hibernate calls
What happens to the uncommitted transaction?
The answer to that question is, “It depends!” The JDBC specification doesn’t say anything about pending transactions when
Obviously, this won’t be a problem for the SELECT you’ve executed, but look at this variation:
This code results in an INSERT statement, executed inside a transaction that is never committed or rolled back. On Oracle, this piece of code inserts data permanently; in other databases, it may not. (This situation is slightly more complicated: The INSERT is executed only if the identifier generator requires it. For example, an identifier value can be obtained from a sequence without an INSERT. The persistent entity is then queued until flush-time insertion — which never happens in this code. An identity strategy requires an immediate INSERT for the value to be generated.)
Look at the following code, which accesses the database with transaction boundaries without use of commit:
Session session = sessionFactory.openSession(); session.beginTransaction(); session.get(Item.class, 123l); session.close();
By default, in a Java SE environment with a JDBC configuration, this is what happens if you execute this snippet:
A new
Sessionis opened. It doesn’t obtain a database connection at this point.
If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction
The call to
get()triggers an SQL SELECT. The Session now obtains a JDBC Connection from the connection pool. Hibernate, by default, immediately turns off the autocommit mode on this connection with
setAutoCommit(false). This effectively starts a JDBC transaction!
The SELECT is executed inside this JDBC transaction. The Session is closed, and the connection is returned to the pool and released by Hibernate — Hibernate calls
close()on the JDBC Connection.
What happens to the uncommitted transaction?
The answer to that question is, “It depends!” The JDBC specification doesn’t say anything about pending transactions when
close()is called on a connection. What happens depends on how the vendors implement the specification. With Oracle JDBC drivers, for example, the call to
close()commits the transaction! Most other JDBC vendors take the sane route and roll back any pending transaction when the JDBC Connection object is closed and the resource is returned to the pool.
Obviously, this won’t be a problem for the SELECT you’ve executed, but look at this variation:
Session session = getSessionFactory().openSession(); session.beginTransaction(); Long generatedId = session.save(item); session.close();
This code results in an INSERT statement, executed inside a transaction that is never committed or rolled back. On Oracle, this piece of code inserts data permanently; in other databases, it may not. (This situation is slightly more complicated: The INSERT is executed only if the identifier generator requires it. For example, an identifier value can be obtained from a sequence without an INSERT. The persistent entity is then queued until flush-time insertion — which never happens in this code. An identity strategy requires an immediate INSERT for the value to be generated.)
相关文章推荐
- Hibernate Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- 深入理解Hibernate中的flush机制
- 解析使用jdbc,hibernate处理clob/blob字段的详解
- 浅析java程序中hibernate的应用总结
- java面试常见问题之Hibernate总结
- 关于Hibernate的一些学习心得总结
- Hibernate管理Session和批量操作分析
- 基于spring+hibernate+JQuery开发之电子相册(附源码下载)
- java Hibernate 一对多自身关联问题
- SpringMVC+MyBatis项目总结(二)