Hibernate中的Session和Connection有什么区别? [英] What is the difference between a Session and a Connection in Hibernate?

查看:350
本文介绍了Hibernate中的Session和Connection有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清除3个基本要点,

I want to clear the basic 3 points,

在旧会话上开始新的数据库事务是否获得 新的连接并恢复会话?

Does beginning a new database transaction on an old session obtains a new connection and resumes the session?

提交数据库事务是否会断开会话与 JDBC连接并将连接返回到池吗?

Does committing a database transaction disconnects a session from the JDBC connection and returns the connection to the pool?

从Hibernate文档中,需要较早版本的Hibernate 会话的显式断开和重新连接.这些方法 不推荐使用,因为开始和结束交易具有相同的 影响.它们有什么相同的效果?

From Hibernate Documentation, earlier versions of Hibernate required explicit disconnection and reconnection of a Session. These methods are deprecated, asbeginning and ending a transaction has the same effect. How do they have the same effect?

推荐答案

休眠会话只是一个事务性

A Hibernate Session is just a transactional write-behind cache that translate entity state transitions into DML statements. The Hibernate Session can be connected or disconnected from the database. When it is disconnected, it cannot flush current pending entity state changes to the underlying database.

可以通过多种方式关联Hibernate会话到数据库事务:

  • 每次请求会话(会话绑定到单个逻辑@Transaction和一个物理数据库事务的生命周期)
  • 长时间对话(该会话可以跨越多个@Transaction操作,因此涉及多个数据库事务)
  • session-per-request (the session is bound to the life-cycle of a single logical @Transaction and one physical database transaction)
  • long conversation (the session can span to multiple @Transaction operations, hence there are multiple database transactions involved)

涉及数据库事务时,有两种不同的方法:

When it comes to database transactions, there are two different approaches:

  • RESOURCE_LOCAL事务,使用单个数据源将始终将物理数据库事务绑定到Hibernate会话(在单个逻辑事务的上下文内,这意味着您仍然可以实现长时间对话以跨越多个此类逻辑事务) .

  • RESOURCE_LOCAL transactions, using a single DataSource will always bind a physical database transaction to a Hibernate Session (within the context of a single logical transaction, meaning that you can still implement long conversations to span over multiple such logical transactions).

JTA,使用多个数据源. JTA指出,应在每个语句后积极释放连接,但实际上,您仍然可以在单个逻辑事务的上下文中获得相同的JDBC连接句柄.

JTA, using multiple DataSources. JTA state that connections should be aggressively released after each statement, but it practice you still get the same JDBC Connection handle within the context of a single logical transaction.

现在回到您的问题:

  1. 在旧会话上开始新的数据库事务是否获得新的连接并恢复会话?

是的. Hibernate会话已重新连接,可以进行刷新/提交.

Yes. The Hibernate session is reconnected and flushing/committing can proceed.

  1. 提交数据库事务是否断开会话与JDBC连接的连接并将连接返回到池中?

默认情况下,当您提交事务时,会话将关闭,基础连接也将关闭.如果您使用连接池,则数据库连接确实会返回到池中.

By default, when you commit a transaction, the Session is closed and the underlying connection is closed. If you use connection pooling, the database connection will indeed return to the pool.

  1. 从Hibernate文档中,早期版本的Hibernate需要显式断开并重新连接Session.这些 不赞成使用方法,因为开始和结束事务具有 同样的效果.它们如何产生相同的效果?
  1. From Hibernate Documentation, earlier versions of Hibernate required explicit disconnection and reconnection of a Session. These methods are deprecated, as beginning and ending a transaction has the same effect. How do they have the same effect?

不推荐使用这些方法,因为

These methods are deprecated because the connection management is now controlled by the connection release modes.

这篇关于Hibernate中的Session和Connection有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆