您会使用AOP进行数据库事务管理吗?

时间:2022-10-01 21:01:38

A while back I wrote an application which used Spring AOP for defining which methods were transactional. I am now having second thoughts as to how much of a great idea this was; I have been hit a few times after a minor refactor (changing method signatures etc), which of course doesn't become apparent until something actually goes wrong (and I have a logically inconsistent database).

前段时间我写了一个应用程序,它使用Spring AOP来定义哪些方法是事务性的。我现在想到这是多么伟大的想法;在一个次要的重构(更改方法签名等)之后,我被击中了几次,当然,直到出现问题(并且我有一个逻辑上不一致的数据库)时,这一点才会变得明显。

So I'm interested in a few things:

所以我对以下几点感兴趣:

  1. Have other people decided to revert to explicit transaction management (e.g. via @Transactional annotations)?
  2. 是否有其他人决定恢复显式交易管理(例如通过@Transactional注释)?

  3. Are there useful tools I can use as part of a build process to help identify whether anything has been "broken"?
  4. 我是否可以使用有用的工具作为构建过程的一部分来帮助确定是否有任何“已损坏”?

  5. If people are using AOP to manage transactions, what steps are they taking to avoid the mistakes I've made?
  6. 如果人们使用AOP来管理交易,他们采取了哪些措施来避免我犯的错误?

I'm using IntelliJ IDEA which allows you to browse decorated methods and will refactor Spring XML config together with method name changes, but this is not always sufficient (adding a parameter to a method in the wrong place can affect whether an aspect fires for example)

我正在使用IntelliJ IDEA,它允许您浏览修饰的方法,并将Spring XML配置与方法名称更改一起重构,但这并不总是足够的(在错误的位置向方法添加参数会影响方面是否触发,例如)

3 个解决方案

#1


4  

I am currently using declarative transaction management in the two Java projects I work on, specifying which methods need transactional scope with @Transactional annotation. In my opinion, it is a good mix of flexibility and robustness: you are able to see which methods have transactional behavior via a simple text search, can adjust isolation and propagation attributes by hand if needed, and the additional amount of typing is practically negligent.

我目前在我工作的两个Java项目中使用声明式事务管理,指定哪些方法需要具有@Transactional注释的事务范围。在我看来,它是灵活性和健壮性的良好组合:您可以通过简单的文本搜索查看哪些方法具有事务行为,如果需要可以手动调整隔离和传播属性,并且额外的键入量实际上是疏忽的。

On one of those projects, I have security/logging implemented via aspects and have occasionally stumbled on same obstacles you when renaming a method or changing signatures. In the worst case, I lost some logging data of user accessing contracts, and in one release, some user roles were not able to access all application features. Nothing major, but, as far as database transactions go, though, I think it's simply not worth it, and I it is better to type @Transactional bit yourself. Spring does the hard part, anyway.

在其中一个项目中,我通过方面实现了安全性/日志记录,并且在重命名方法或更改签名时偶尔会遇到相同的障碍。在最糟糕的情况下,我丢失了一些用户访问合同的日志记录数据,并且在一个版本中,一些用户角色无法访问所有应用程序功能。没什么大不了的,但是,就数据库事务而言,我觉得它根本不值得,而且我最好自己输入@Transactional位。无论如何,春天确实很难。

#2


2  

Regarding (1): I found @Transactonal a more practical solution in all projects worked on in the past few years. In some very specific cases, however, I had also to use Spring AOP to allow the use of more than one JDBC connection / TransactionManager because @Transaction is tied to a single transaction manager.

关于(1):我发现@Transactonal在过去几年中所有项目中都是一个更实用的解决方案。但是,在某些非常特殊的情况下,我还必须使用Spring AOP来允许使用多个JDBC连接/ TransactionManager,因为@Transaction绑定到单个事务管理器。

Regarding (2): Having said that, in a mixed scenario, I do a lot of automated testing to find possibly broken code. I use Spring's AbstractTransactionalJUnit4SpringContextTests / AbstractTransactionalTestNGSpringContextTests to create my tests. It's been a very effective solution so far.

关于(2):话虽如此,在混合场景中,我做了很多自动化测试,以找到可能损坏的代码。我使用Spring的AbstractTransactionalJUnit4SpringContextTests / AbstractTransactionalTestNGSpringContextTests来创建我的测试。到目前为止,这是一个非常有效的解决方案。

#3


0  

I tend to be more of a purist, but I try to keep any and all transaction management beyond a simple autocommit, inside the database itself. Most databases are excellent at handling transaction management, after all, its one of the key components of what a database is meant to do.

我倾向于更多的纯粹主义者,但我试图在数据库本身内部保持任何和所有事务管理超出简单的自动提交。毕竟,大多数数据库都非常善于处理事务管理,它是数据库意图的关键组件之一。

#1


4  

I am currently using declarative transaction management in the two Java projects I work on, specifying which methods need transactional scope with @Transactional annotation. In my opinion, it is a good mix of flexibility and robustness: you are able to see which methods have transactional behavior via a simple text search, can adjust isolation and propagation attributes by hand if needed, and the additional amount of typing is practically negligent.

我目前在我工作的两个Java项目中使用声明式事务管理,指定哪些方法需要具有@Transactional注释的事务范围。在我看来,它是灵活性和健壮性的良好组合:您可以通过简单的文本搜索查看哪些方法具有事务行为,如果需要可以手动调整隔离和传播属性,并且额外的键入量实际上是疏忽的。

On one of those projects, I have security/logging implemented via aspects and have occasionally stumbled on same obstacles you when renaming a method or changing signatures. In the worst case, I lost some logging data of user accessing contracts, and in one release, some user roles were not able to access all application features. Nothing major, but, as far as database transactions go, though, I think it's simply not worth it, and I it is better to type @Transactional bit yourself. Spring does the hard part, anyway.

在其中一个项目中,我通过方面实现了安全性/日志记录,并且在重命名方法或更改签名时偶尔会遇到相同的障碍。在最糟糕的情况下,我丢失了一些用户访问合同的日志记录数据,并且在一个版本中,一些用户角色无法访问所有应用程序功能。没什么大不了的,但是,就数据库事务而言,我觉得它根本不值得,而且我最好自己输入@Transactional位。无论如何,春天确实很难。

#2


2  

Regarding (1): I found @Transactonal a more practical solution in all projects worked on in the past few years. In some very specific cases, however, I had also to use Spring AOP to allow the use of more than one JDBC connection / TransactionManager because @Transaction is tied to a single transaction manager.

关于(1):我发现@Transactonal在过去几年中所有项目中都是一个更实用的解决方案。但是,在某些非常特殊的情况下,我还必须使用Spring AOP来允许使用多个JDBC连接/ TransactionManager,因为@Transaction绑定到单个事务管理器。

Regarding (2): Having said that, in a mixed scenario, I do a lot of automated testing to find possibly broken code. I use Spring's AbstractTransactionalJUnit4SpringContextTests / AbstractTransactionalTestNGSpringContextTests to create my tests. It's been a very effective solution so far.

关于(2):话虽如此,在混合场景中,我做了很多自动化测试,以找到可能损坏的代码。我使用Spring的AbstractTransactionalJUnit4SpringContextTests / AbstractTransactionalTestNGSpringContextTests来创建我的测试。到目前为止,这是一个非常有效的解决方案。

#3


0  

I tend to be more of a purist, but I try to keep any and all transaction management beyond a simple autocommit, inside the database itself. Most databases are excellent at handling transaction management, after all, its one of the key components of what a database is meant to do.

我倾向于更多的纯粹主义者,但我试图在数据库本身内部保持任何和所有事务管理超出简单的自动提交。毕竟,大多数数据库都非常善于处理事务管理,它是数据库意图的关键组件之一。