hibernate是在内部使用jdbc还是在内部使用JTA或者它是可配置的?“ [英] Does hibernate uses jdbc internally or JTA internally or it is configurable?"

查看:136
本文介绍了hibernate是在内部使用jdbc还是在内部使用JTA或者它是可配置的?“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是hibernate的新手,开始学习hibernate。我将通过使用hibernate和JDBC()。我的第一个问题是
hibernate在内部使用jdbc还是内部使用JTA 执行坚持并获得操作?
但是我在这里也可以看到JTA的一些提及,例如getCurrentSeessionSession()方法获取与当前JTA事务关联的会话。问题: - 基本上我想了解JTA和jdbc在hibernate中的作用。

问题2: - 我可以在下面的代码片段中看到在hibernate中的任何操作

  try {
session = factory.openSession();
tx = session.beginTransaction();
session.save(myClass);
tx.commit();
}
finally {
session.close();
}

在这里,我想了解line // tx = session的作用。的BeginTransaction();根据理解,每个会话将使用一个连接。所以即使是
,如果我们从同一个会话开始多个交易,我们将使用相同的连接。一旦我们提交特定事务,从同一个会话创建的所有事务
将被提交一次。那么我们试图用// tx = session.beginTransaction()来实现什么呢? ?

解决方案


Hibernate在内部使用jdbc还是内部使用JTA来执行持久化和获取操作?


JDBC和JTA不可互换。 JDBC是Java应用程序用来与数据库进行交互的标准API。 JTA是用于管理的标准API 跨一个或多个资源的交易。与你的问题最接近的答案是内部,Hibernate使用JDBC与数据库进行交互。


像getCurrentSeessionSession()与当前JTA交易相关的会话。


不完全。 SessionFactory.getCurrentSession() a>根据当前会话上下文获取会话。该策略的一个实现是 JTA会话上下文,它基本上将会话与JTA事务相关联。 JTA事务不会有Hibernate会话,因为JTA对Hibernate一无所知,并且说Hibernate在内部使用JTA并不完全正确。它只是有能力与JTA集成,并让它管理交易。


这里我想了解行的作用// tx = session.beginTransaction();

它以您使用的任何交易机制开始交易,交易机制由 TransactionFactory 在使用中。例如,使用JDBCTransactionFactory,它只是确保 auto-commit 被关闭,以便在事务完成之前不会提交更改。


一次我们提交了具体的事务,从同一个会话创建的所有事务将被提交一次。


在正常情况下,会话仅与一笔交易。多次调用Session.beginTransaction()将简单地返回相同的底层交易


所以我们试图用// tx = session.beginTransaction ()


就这样:告诉管理你的事务的任何事情,你开始一个新的事务。这意味着在commit()或rollback()之前发生的所有事情都应该具有普遍接受的数据库事务的语义


I am new to hibernate and started studying hibernate.The chapter I am going thru uses hibernate with JDBC().My first question is Does hibernate use jdbc internally or JTA internally to perform persist and get operations? But I can see some mention of JTA also here, like getCurrentSeessionSession() method obtains the session associated with current JTA transaction. Question:- Basically I want to understand the role of JTA and jdbc here in hibernate.

Question2 :- I can see below code snippet in any operation in hibernate

try{ 
session=factory.openSession();
tx=session.beginTransaction();
session.save(myClass);
tx.commit();
}
finally{
session.close();
}

Here, I want to understand the role of line // tx=session.beginTransaction(); As per understanding, each session will be using one connection . So even if we start multiple transactions from the same session, we will be using same connection. once we commit the specific transaction all transactions created from the same session will be committed once. So what are we trying to achieve with // tx=session.beginTransaction(); ?

解决方案

Does hibernate uses jdbc internally or JTA internally to perform persist and get operations?

JDBC and JTA aren't interchangeable. JDBC is the standard API that Java applications use to interact with a database. JTA is the standard API for managing transactions across one or more resources. The closest answer to your question would be that "internally", Hibernate uses JDBC to interact with the database.

Like getCurrentSeessionSession() method obtains the session associated with current JTA transaction.

Not exactly. SessionFactory.getCurrentSession() gets a session according to the current session context. One implementation of that strategy is the JTA session context, which essentially associates sessions with a JTA transaction. A JTA transaction doesn't "have" a Hibernate session, since JTA knows nothing about Hibernate, and it wouldn't be quite right to say that Hibernate uses JTA internally. It simply has the ability to integrate with JTA and let it manage transactions.

here i want to understand the role of line // tx=session.beginTransaction();

It begins a transaction in whatever transaction mechanism you're using, which is governed by the TransactionFactory in use. For instance, with the JDBCTransactionFactory, it just ensures auto-commit is turned off so that changes won't be committed until the transaction is complete.

once we commit the specific transaction all transactions created from a same session will be commited once.

Under normal circumstances, a Session is associated to only one transaction. Multiple calls to Session.beginTransaction() will simply return the same underlying Transaction.

So what we are trying to achieve with // tx=session.beginTransaction()

Just that: tell whatever is managing your transactions that you're beginning a new transaction. That implies that everything that happens until you commit() or rollback() should have the generally accepted semantics of a database transaction.

这篇关于hibernate是在内部使用jdbc还是在内部使用JTA或者它是可配置的?“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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