What will happen if we begin transaction in hibernate but do not commit it?
来源:互联网 发布:最新网页制作软件 编辑:程序博客网 时间:2024/05/19 02:45
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:
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 Session
is 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.)
- What will happen if we begin transaction in hibernate but do not commit it?
- We are what we repeatedly do. Excellence then, is not an act, but a habit.
- WHEN YOU TYPE CHINESE CHARACTER IN PYTHON SCRIPT AND RUN IT, WHAT WILL HAPPEN?
- What will happen if Schwarzenegger meet with Whitman
- What will happen with TMG?
- Troubleshooting error in cloud but not happen in local
- What we will Miss from Java in 2016?
- What can we do?
- What do we count?
- What do we know?
- What we do?
- YP.1.1 What We Will Try to Do and 1.2 How We Will Get There(双语)
- if we do not declare the sql-type or attribute type, it defaults to String or VARCHAR(255).
- What should we do when meet a crash in android?
- What should we do when meet a crash in android
- what will the goverment do in next year
- What will I do in next 100 days for programming?
- can begin transaction in hibernate called while commiting/rollback
- Android进程回收的先后顺序
- Java JVM(十):JavaVisualVM 的个人使用
- ARM9 ADS8344 SPI驱动移植 (四)
- JavaScript实现页面下拉刷新
- centOS 操作记录一
- What will happen if we begin transaction in hibernate but do not commit it?
- ## memset 清除结构体的方法
- android:contentDescription 的用途
- 为发烧而生的是小米 为合同管理而生的是奥普
- No live SolrServers available to handle this request
- hdu 5438
- 第一次初始化Git仓库,提交本地仓库到远程
- Jetty in Win7
- conn.setAutoCommit(true)和(false)的区别