您可以尝试将 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?