为什么使用@transactional与@service insted与@controller [英] why use @transactional with @service insted of with @controller

查看:714
本文介绍了为什么使用@transactional与@service insted与@controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在堆栈溢出文章中看到很多评论。我发现了 @transactional @service @controller


通常,应在服务层放置一个事务。



正常情况是在服务层级上注释



认为事务属于服务层,它是知道工作和使用单位的一个如果您有多个DAO注入到需要在单个事务中协同工作的服务中,这是正确的答案。 [来源]


使用@service图层使用@transactional的缺点


如果我有2方法例如saveUser()和saveEmail()(因为我将电子邮件存储在数据库中以后发送它们 - 像队列)我将在我的服务中创建一个方法saveUserAndSendEmail(用户用户),这将是事务性的。 [来源]


这意味着我在服务层中创建了许多方法,而不是一个保存通用方法,如下所示

  public< T>长存储(T实体)throws DataAccessException {
会话session = sessionFactory.getCurrentSession();
long getGenVal =(Long)session.save(entity);
return getGenVal;
}

根据上述解决方案,这意味着我们有很多方法正在LOL ..



public< T& long saveAccount(T entity)....



public< T& long saveWithAuditLog(T entity,K entity1)....



public< T& long saveWithAuditLogAndEntries(T entity,K entity,M entity)....



OVERCOME this situation p>

我在@controller中使用@Transactional,只需使用通用保存方法并使用这种简单的保存方法保存所有实体/模型。如果任何方法无法保存,那么控制器中的所有事务都会成功地回滚。



其他情况确保@transactional应该与@controller



在@controller中:

  pt.save(entity1); 
pt.save(entity2);
int a = 2/0;
pt.save(entity3);

如果在服务上使用@transactional,前2个实体已成功保存,回滚所有事务



如果@controller上有@transactional,所有事务回滚作为异常发生 b
$ b

为什么堆栈溢出问,不要在你的控制器中做交易,把它们放在你的服务层类。
[source]

解决方案

您正在询问最佳实践,最佳做法是标记 @Transactional 在服务层中,因为 @Controller 不应该意识到MVC逻辑中的数据持久性。

@服务是基于从分析生成的用例构建的,并且知道工作单元,并且在重用方面实现思考:如果从Web上下文切换到桌面(例如,其他可视化前端)其中 @Controller 图层不存在您没有问题,因为所有都封装在服务层。

A @Service 是合同,并且表示层中的修改不应要求重写 @Service 代码。 b $ b但Spring不关心你的事务边界,你可以放在 @Controller ,但你的应用程序可能更难被维护。 p>

我希望这很清楚。对不起,如果没有;英语不是我的母语。


I have seen many comments in stack-overflow articles I found certain things about either @transactional use with @service or with @controller

"Usually, one should put a transaction at the service layer."

"The normal case would be to annotate on a service layer level"

"think transactions belong on the Service layer. It's the one that knows about units of work and use cases. It's the right answer if you have several DAOs injected into a Service that need to work together in a single transaction." [Source]

Drawback to use @transactional with @service layer

If I had 2 methods for example saveUser() and saveEmail() (because I store the emails in a database to send them later - like a queue) I would create in my service a method saveUserAndSendEmail(User user) which would be transactional. [Source]

it means I create many methods in service layer instead of one Save Generic Method as follow

public <T> long save(T entity) throws DataAccessException {
    Session session = sessionFactory.getCurrentSession();
    long getGenVal=(Long) session.save(entity);
    return getGenVal;
}

According to the above solution , it means we have many methods like following LOL..

public <T> long saveAccount(T entity)....

public <T> long saveWithAuditLog(T entity, K entity1)....

public <T> long saveWithAuditLogAndEntries(T entity, K entity, M entity)....

OVERCOME this situation

I USE THE @Transactional in @controller and Just make a Generic Save Method and save all the entities/ model using this simple save method. and if any method fail to save then all the transactions in controller rollback successfully

Other situation that ensure that @transactional should be use with @controller

In @controller:

pt.save(entity1);
pt.save(entity2);
int a = 2/0;
pt.save(entity3);

In case , @transactional on Service, first 2 entity successfully saved but third not it not rollback all the transaction

In case , @transactional on @controller all the transaction rollback as exception occur

why stack-overflow asked , "Don't do transactions in your controller. Put them in your service layer classes."? [source]

解决方案

You are asking about best practice, and best practice is to mark @Transactional in the service layer because a @Controller should not be aware of data persistence in a MVC logic.
@Service is constructed on use-case generated from analysis and knows about unit of works and is also realized thinking in terms of reuse: if you switch from a web context to a desktop one (for example, or some other visual frontend) where @Controller layer doesn't exist you don't have problems because all is encapsulated in service layer.
A @Service is a contract and a modification in presentation layer should not require a rewrite of @Service code.
But Spring don't care about where you put your your transaction boundaries, you can put on @Controller but your application may will be harder to be maintained.

I hope this is clear enough. Sorry if not; English is not my native language.

这篇关于为什么使用@transactional与@service insted与@controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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