“春季交易”的区别是什么?和“休眠事务” [英] What is the difference between a "Spring transaction" and a "Hibernate transaction"

查看:96
本文介绍了“春季交易”的区别是什么?和“休眠事务”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. Hibernate事务

  2. Spring事务

另外我想知道 @Transactional 注释。

解决方案

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


Hibernate处理数据库特定的事务,而Spring提供一个通用的事务管理服务。 @Transactional 是配置交易管理行为的好方法。


$ b

交易

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

Hibernate

是一个用于将数据库组件抽象为Java对象的ORM,因此它的事务特别与数据库中所做的更改相关。事务可以由一次或多次写入各种数据库表组成,这些数据库表在操作完成后全部被提交。回滚事务,例如在操作过程中出现任何错误,允许撤消所有更改。



Spring



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

Spring旨在用作应用程序中对象和服务的全面掌握,因此它的事务概念处于比数据库特定的更高级别与hibernate有关的交易。 Spring Transactions 旨在为您提供对所有交易资源进行细粒度控制,同时抽象出协调交易所需的常常混乱的编码。

@Transactional

< Spring为使用事务提供了几种不同的方法 - 其中包括基于xml的方面,对API进行编码和基于注释的声明式事务。基于注释的事务处理非常方便,因为您不需要将事务管理样板代码添加到应用程序中(即使通过API使用PlatformTransactionManager也有相当多的编码开销)。因此,基本上 @Transactional 会发生什么,因为在运行时,sp​​ring会扫描您的用于@Transactional类和方法的代码库并根据您通过注释配置的内容将它们包装在交易特定的管理代码中。所以一个像这样的方法:

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

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


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

  1. Hibernate transaction
  2. Spring transaction

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

解决方案

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

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.

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.

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.

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 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.

@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).

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(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.

这篇关于“春季交易”的区别是什么?和“休眠事务”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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