您可以尝试将hibernate与JDBC相关联,您将获得关于事务的一些提示。
在JDBC中,您只需打开一个连接即可开始工作,最后您可以提交或回滚。
但是,如果您有许多不同的并行任务,它们可能相互依赖或独立。然后,您可能需要单独提交/回滚每个任务,或者如果任何失败则需要回滚。
for example
Big Task :
small task1
small task2
small task3 and many more
回滚大任务,如果任何小任务失败。这可能是许多业务需求之一。
在JDBC中,Connection接口提供了commit()和rollback()方法。
在jpa/hibernate中,Transaction接口提供了commit()和rollback()方法。
所以一个会话可以有很多依赖或独立的事务。
下面是文档从org.hibernate.Transaction
Allows the application to define units of work, while maintaining
abstraction from the underlying transaction implementation (eg. JTA, JDBC).
A transaction is associated with a Session and is usually
initiated by a call to org.hibernate.Session.beginTransaction().
A single session might span multiple transactions since the notion of a session
(a conversation between the application and the datastore) is of coarser granularity
than the notion of a transaction. However, it is intended that there be at most
one uncommitted transaction associated with a particular Session at any time.
这也可以帮助你 What is the difference between a session and a transaction in JPA 2.0?
这与Java或休眠没有任何关系。事务是一个数据库事情 – Jens