Skip to content

CGLIB proxies should still consider @Transactional annotations on interface methods [SPR-14322] #18894

Closed

Description

@spring-projects-issues

Oliver Drotbohm opened SPR-14322 and commented

If an application components implements an interface whose methods carry annotations that are triggering interceptors (e.g. for transactions), enabling target class proxying will result in the interceptors for those annotations not being triggered anymore. Here's a sample:

interface SomeComponent {

  @Transactional
  void init();
}

@Component
class SomeComponentImpl  implements SomeComponent {

  @Override
  public void init() {
    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
      throw new IllegalStateException("Expected transaction to be active!");
    }
  }
}

@Component
class Invoker {

  public Invoker(List<SomeComponent> components) {
    components.forEach(SomeComponent::init);
  }
}

If the above is bootstrapped with standard @EnableTransactionManagement the instances handed to the constructor of Invoker are JDK proxies and the lookup of the advice chain results in the interceptor for transactions being returned and thus activated. If proxyTargetClass is set to true, the instances received by the constructor are CGLib proxies and the lookup of the advice chain results in an empty one and thus no transaction is created in the first place.


Affects: 4.2.6, 4.3 RC2

Attachments:

Issue Links:

1 votes, 9 watchers

Activity

  1. spring-projects-issues commented on May 31, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Drotbohm commented

    Attached a sample using Spring Boot. On 1.3 (with target class proxying disabled by default) the application can be bootstrapped, on 1.4 M3 the bootstrap fails due to the situation described above. Manually enabling target class proxying on 1.3 results in the same problem.

    Boot can even be moved out of the picture by using this class to bootstrap the app:

    @Configuration
    @ComponentScan
    @EnableTransactionManagement(proxyTargetClass = true)
    public class TxDifferencesApplication {
    
    	@Bean
    	public DataSourceTransactionManager transactionManager() {
    		return new DataSourceTransactionManager(dataSource());
    	}
    
    	@Bean
    	public DataSource dataSource() {
    		return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build();
    	}
    
    	public static void main(String[] args) {
    		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TxDifferencesApplication.class);
    	}
    }
  2. spring-projects-issues commented on May 31, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Drotbohm commented

    I just wondered: isn't that a problem with Spring MVC controllers using @RequestMapping on interfaces, too? If a CGLib proxy is created here, how would Spring MVC find those annotations then?

  3. spring-projects-issues commented on Aug 19, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    We've enabled CGLIB proxies by default in Boot 1.4 and that broke things for our users, see #6693

  4. spring-projects-issues commented on Sep 13, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Harald Radi commented

    Oliver, it is. I just stumbled upon this issue because of that and due to spring-projects/spring-boot#5423. I don't see any better option than working around the Spring Boot autoconfiguration for now.

  5. spring-projects-issues commented on Nov 24, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Dave Syer commented

    I think the tests in #19516 show that this is not a problem for all interceptors (there's an @Aspect there which is applied to the proxy), but only apparently some (@Async in that case).

    @harald you can also work around the issue by not putting the annotations in the interface (kind of makes more sense to put them in the concrete class to me anyway).

  6. spring-projects-issues commented on Nov 24, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Drotbohm commented

    Putting them on the interface makes sense in cases where the implementation doesn't even show up in kind of documentation (e.g. due to the implementation class being held in package scope) and the annotation functionality being a crucial part of the public contract. I'd argue there's value in knowing a method will be executed transactionally if I just study the interface.

  7. spring-projects-issues commented on Nov 24, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Harald Radi commented

    In our specific case we dynamically generate client-proxies, hence i definitely need the @RequestMapping annotations on the interface. And for the @Transactional I'm with @oliver, as part of the contract it needs to go into the interface definition.

  8. spring-projects-issues commented on Feb 27, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Harald Radi commented

    @juergen, it's not just @Transactional but essentially any annotation. In our particular case it is a custom @RestRequestMapping annotation that itself is annotated with @ResponseBody.

  9. spring-projects-issues commented on Feb 27, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    We're tracking handler method annotations separately in #15682, and we got dedicated tickets for @Cacheable (#19836) and @Async (#19516) already. The latter two are interceptor-related like Ollie's original case here, which is why I've changed this ticket to focus on Ollie's @Transactional case. Those three need to be addressed in distinct places but share the basic characteristic of being triggered by an AOP pointcut. Handler methods such as @RequestMapping methods in MVC are actually quite a different case since they are being triggered by an external dispatcher which needs to be able to deal with existing proxies, and we also got the issue of retaining parameter-level annotations in an interface-declared method here (which is technically only possible on Java 8+).

    So rest assured, we are revisiting the wider issue, with this ticket as one of four work items. The general target is 5.0 RC1; we're considering backports to the 4.3.x line.

  10. spring-projects-issues commented on Feb 27, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Harald Radi commented

    Thanks for the update, and the effort!

  11. added
    in: data
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corein: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions