简体   繁体   English

Springs @Transactional仅适用于代理

[英]Springs @Transactional only works for proxy

I have a method that uses Springs @Transactional for database rollbacks. 我有一个使用Springs @Transactional进行数据库回滚的方法。 It works fine when i define the transaction manager like this: 当我像这样定义事务管理器时它工作正常:

    <tx:annotation-driven transaction-manager="txManager" mode="proxy" />

but when i change to mode="aspectj" it does not rollback when the method throws an exception. 但当我更改为mode="aspectj"它不会在方法抛出异常时回滚。

are there some differences in how the two modes should be used? 这两种模式应该如何使用?

The "aspectj" mode will only work if load-time weaving or compile-time weaving are enabled. 只有在启用了加载时编织或编译时编织时,“aspectj”模式才有效。 If not then the @Transactional annotation will not be applied to the annotated method. 如果不是,那么@Transactional注释将不会应用于带注释的方法。

The default proxy mode cannot be used to annotate private methods. 默认代理模式不能用于注释private方法。 This is where aspectj mode is useful. 这是aspectj模式很有用的地方。 Certain frameworks (such as W2O for Web Services) require the class type itself to work. 某些框架(例如Web服务的W2O)需要类类型本身才能工作。 This can be impossible using proxy mode as the class is wrapped in a proxy class wrapper. 使用代理模式这是不可能的,因为类包含在代理类包装器中。 This is another reason for using AspectJ . 这是使用AspectJ另一个原因。

If public methods are used and there are no special framework restrictions, then proxy mode is sufficient. 如果使用public方法并且没有特殊的框架限制,那么代理模式就足够了。

See Also: Transaction Management 另请参见: 事务管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM