“Spring事务”和“Hibernate事务”之间有什么区别

时间:2022-01-18 20:08:13

Could you please explain the difference between the following two types of transactions:

能否请您解释以下两种交易之间的区别:

  1. Hibernate transaction
  2. Hibernate事务
  3. Spring transaction
  4. 春季交易

Also I would like to know about the @Transactional annotation.

另外我想知道@Transactional注释。

1 个解决方案

#1


12  

Well for starters they are both Transactions, but they encompass different concepts and components.

对于初学者来说,他们都是交易,但它们包含不同的概念和组件。

TL;DR

TL; DR

Hibernate deals with database specific transactions, whereas spring provides a general transaction management service. @Transactional is a nice way of configuring transaction management behaviour.

Hibernate处理数据库特定的事务,而spring提供一般的事务管理服务。 @Transactional是配置事务管理行为的好方法。

The long story:

长篇故事:

Transactions

交易

Transactions are basically units of work (ie changes to something) that are managed as a single operation that can be either committed or rolled back. There are lots of different types of transactions in the java world - database, messaging systems like JMS, inter application transactions (for those who are not faint of heart) or anything else that may need to be included in a transaction. In the Java standard transactions are managed using the Java Transaction API which sets the rules for how to participate in a transaction.

事务基本上是工作单元(即对某些事物的更改),它们作为可以提交或回滚的单个操作进行管理。 Java世界中有许多不同类型的事务 - 数据库,JMS之类的消息传递系统,应用程序间事务(对于那些不是胆小的人)或者可能需要包含在事务中的任何其他事务。在Java标准中,使用Java Transaction API管理事务,该API设置如何参与事务的规则。

Hibernate

过冬

Hibernate is an ORM for abstracting database components to Java objects, so its transactions are specifically related to changes made within a database. A transaction may be made up of one or many writes to various database tables that are all committed once the operation is completed. Rolling back the transaction, eg f there are any errors during the operation, allows all the changes to be undone.

Hibernate是用于将数据库组件抽象为Java对象的ORM,因此其事务与数据库中所做的更改具体相关。事务可以由对各种数据库表的一次或多次写入组成,这些数据库表一旦操作完成就全部提交。回滚事务,例如,在操作期间存在任何错误,允许撤消所有更改。

Spring

弹簧

At its lowest level Spring is a application framework for managing configuration and dependencies between objects. In addition it also provides an interface for managing higher level services that are used in modern applications such as databases, messaging services, MVC frameworks and transactions.

在最低级别,Spring是一个用于管理对象之间的配置和依赖关系的应用程序框架。此外,它还提供了一个用于管理现代应用程序(如数据库,消息传递服务,MVC框架和事务)中使用的更高级别服务的接口。

Spring is designed to be used as an all-encompassing master of objects and services within your application, so its concept of a transaction is at a higher level than the database specific transactions that hibernate concerns itself with. Spring Transactions are designed to give you fine grained control of all your transactional resources while abstracting away the often messy coding required to co-ordinate the transactions.

Spring旨在用作应用程序中包含对象和服务的无所不包的主机,因此它的事务概念比hibernate关注的数据库特定事务处于更高级别。 Spring Transactions旨在为您提供对所有事务资源的精细控制,同时抽象出协调事务所需的经常混乱的编码。

@Transactional

@Transactional

Spring provides a few different methods for using transactions - among others there xml based aspects, coding to the API and annotation based declarative transactions. The annotation based transactions are handy because you dont need to add the transaction management boilerplate code to your app (even using PlatformTransactionManager via the API has quite a bit of coding overhead).

Spring提供了一些使用事务的不同方法 - 其中包括基于xml的方面,对API的编码和基于注释的声明性事务。基于注释的事务很方便,因为您不需要将事务管理样板代码添加到您的应用程序(即使使用PlatformTransactionManager通过API也有相当多的编码开销)。

So basically what happens with @Transactional is that at runtime spring scans your code base for @Transactional classes and methods and wraps them up in the transaction specific management code, based on what you have configured via the annotation. So a method like this:

所以@Transactional基本上发生的事情是,在运行时,sp​​ring会扫描你的代码库以获取@Transactional类和方法,并根据你通过注释配置的内容将它们包装在特定于事务的管理代码中。所以像这样的方法:

@Transactional(propagation = REQUIRES_NEW, rollbackFor = {Exception.class})
public void saveAndSendMessage(Foo foo) throws Exception {
    dbManager.save(foo);
    Bar bar = transform(foo);
    jmsSystem.send(bar);
}  

can have spring set up a new transaction for the database and jms system, and co-ordinate them without needing to add all the specific tx management code automagically.

可以让spring为数据库和jms系统设置一个新的事务,并协调它们,而不需要自动添加所有特定的tx管理代码。

#1


12  

Well for starters they are both Transactions, but they encompass different concepts and components.

对于初学者来说,他们都是交易,但它们包含不同的概念和组件。

TL;DR

TL; DR

Hibernate deals with database specific transactions, whereas spring provides a general transaction management service. @Transactional is a nice way of configuring transaction management behaviour.

Hibernate处理数据库特定的事务,而spring提供一般的事务管理服务。 @Transactional是配置事务管理行为的好方法。

The long story:

长篇故事:

Transactions

交易

Transactions are basically units of work (ie changes to something) that are managed as a single operation that can be either committed or rolled back. There are lots of different types of transactions in the java world - database, messaging systems like JMS, inter application transactions (for those who are not faint of heart) or anything else that may need to be included in a transaction. In the Java standard transactions are managed using the Java Transaction API which sets the rules for how to participate in a transaction.

事务基本上是工作单元(即对某些事物的更改),它们作为可以提交或回滚的单个操作进行管理。 Java世界中有许多不同类型的事务 - 数据库,JMS之类的消息传递系统,应用程序间事务(对于那些不是胆小的人)或者可能需要包含在事务中的任何其他事务。在Java标准中,使用Java Transaction API管理事务,该API设置如何参与事务的规则。

Hibernate

过冬

Hibernate is an ORM for abstracting database components to Java objects, so its transactions are specifically related to changes made within a database. A transaction may be made up of one or many writes to various database tables that are all committed once the operation is completed. Rolling back the transaction, eg f there are any errors during the operation, allows all the changes to be undone.

Hibernate是用于将数据库组件抽象为Java对象的ORM,因此其事务与数据库中所做的更改具体相关。事务可以由对各种数据库表的一次或多次写入组成,这些数据库表一旦操作完成就全部提交。回滚事务,例如,在操作期间存在任何错误,允许撤消所有更改。

Spring

弹簧

At its lowest level Spring is a application framework for managing configuration and dependencies between objects. In addition it also provides an interface for managing higher level services that are used in modern applications such as databases, messaging services, MVC frameworks and transactions.

在最低级别,Spring是一个用于管理对象之间的配置和依赖关系的应用程序框架。此外,它还提供了一个用于管理现代应用程序(如数据库,消息传递服务,MVC框架和事务)中使用的更高级别服务的接口。

Spring is designed to be used as an all-encompassing master of objects and services within your application, so its concept of a transaction is at a higher level than the database specific transactions that hibernate concerns itself with. Spring Transactions are designed to give you fine grained control of all your transactional resources while abstracting away the often messy coding required to co-ordinate the transactions.

Spring旨在用作应用程序中包含对象和服务的无所不包的主机,因此它的事务概念比hibernate关注的数据库特定事务处于更高级别。 Spring Transactions旨在为您提供对所有事务资源的精细控制,同时抽象出协调事务所需的经常混乱的编码。

@Transactional

@Transactional

Spring provides a few different methods for using transactions - among others there xml based aspects, coding to the API and annotation based declarative transactions. The annotation based transactions are handy because you dont need to add the transaction management boilerplate code to your app (even using PlatformTransactionManager via the API has quite a bit of coding overhead).

Spring提供了一些使用事务的不同方法 - 其中包括基于xml的方面,对API的编码和基于注释的声明性事务。基于注释的事务很方便,因为您不需要将事务管理样板代码添加到您的应用程序(即使使用PlatformTransactionManager通过API也有相当多的编码开销)。

So basically what happens with @Transactional is that at runtime spring scans your code base for @Transactional classes and methods and wraps them up in the transaction specific management code, based on what you have configured via the annotation. So a method like this:

所以@Transactional基本上发生的事情是,在运行时,sp​​ring会扫描你的代码库以获取@Transactional类和方法,并根据你通过注释配置的内容将它们包装在特定于事务的管理代码中。所以像这样的方法:

@Transactional(propagation = REQUIRES_NEW, rollbackFor = {Exception.class})
public void saveAndSendMessage(Foo foo) throws Exception {
    dbManager.save(foo);
    Bar bar = transform(foo);
    jmsSystem.send(bar);
}  

can have spring set up a new transaction for the database and jms system, and co-ordinate them without needing to add all the specific tx management code automagically.

可以让spring为数据库和jms系统设置一个新的事务,并协调它们,而不需要自动添加所有特定的tx管理代码。