@Transactional 注解属于哪里?

2022-01-13 00:00:00 annotations transactions spring dao java

您应该将 @Transactional 放在 DAO 类和/或其方法中,还是最好注释使用 DAO 对象调用的服务类?或者注释两个层​​"有意义吗?

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to annotate both "layers"?

推荐答案

我认为事务属于服务层.它了解工作单元和用例.如果您将多个 DAO 注入到需要在单个事务中协同工作的服务中,那么这是正确的答案.

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

相关文章