Skip to content

Add support for @Transactional in native images #28717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhalbritter opened this issue Jun 28, 2022 · 3 comments
Closed

Add support for @Transactional in native images #28717

mhalbritter opened this issue Jun 28, 2022 · 3 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) theme: aot An issue related to Ahead-of-time processing
Milestone

Comments

@mhalbritter
Copy link
Contributor

mhalbritter commented Jun 28, 2022

When running the jdbc-tx sample in sb-3.0.x branch from spring-native, it fails with:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:769) ~[jdbc-tx:3.0.0-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:750) ~[jdbc-tx:3.0.0-SNAPSHOT]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[jdbc-tx:3.0.0-SNAPSHOT]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[jdbc-tx:3.0.0-SNAPSHOT]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[jdbc-tx:3.0.0-SNAPSHOT]
        at app.main.SampleApplication.main(SampleApplication.java:23) ~[jdbc-tx:0.0.1.BUILD-SNAPSHOT]
Caused by: java.lang.IllegalArgumentException: Expected transaction
        at org.springframework.util.Assert.isTrue(Assert.java:121) ~[na:na]
        at app.main.Runner.run(Runner.java:53) ~[jdbc-tx:0.0.1.BUILD-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:766) ~[jdbc-tx:3.0.0-SNAPSHOT]
        ... 5 common frames omitted

This is the run method:

	@Override
	@Transactional
	public void run(String... args) throws Exception {
		Assert.isTrue(TransactionSynchronizationManager.isActualTransactionActive(), "Expected transaction");
		try {
			find(1L);
		}
		catch (EmptyResultDataAccessException e) {
			entities.update(ADD_FOO, 1L, "Hello");
		}
	}

I guess that @Transactional doesn't work in native-image.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 28, 2022
@sdeleuze sdeleuze added in: data Issues in data modules (jdbc, orm, oxm, tx) theme: aot An issue related to Ahead-of-time processing and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 28, 2022
@sdeleuze
Copy link
Contributor

Related to #28688.

@sdeleuze sdeleuze self-assigned this Jun 28, 2022
@sdeleuze sdeleuze added this to the 6.0.0-M5 milestone Jul 6, 2022
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Jul 7, 2022
This commit annotates @transactional with @Reflective
and registers proxy hints for SpringProxy.

Injection of proxied beans via their interfaces still fails
in native images with a
"No qualifying bean of type MyInterface" error.

See spring-projectsgh-28717
@sdeleuze
Copy link
Contributor

sdeleuze commented Jul 7, 2022

I have improved the support for @Transactional with this commit that makes @Transactional annotated with @Reflective and registers proxy hints for SpringProxy but the jdbc-tx sample here still fails when compiled as a native executable with error creating bean with name 'userEndpoints': Unsatisfied dependency expressed through method 'userEndpoints' parameter 0: No qualifying bean of type 'app.main.Finder<app.main.model.Foo>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} error, like if proxied beans can't be injected in native. Notice here we have @Component public class Runner implements CommandLineRunner, Finder<Foo> { ... } that needs to be injected in @Bean public RouterFunction<?> userEndpoints(Finder<Foo> entities) { ... }.This samples works correctly on JVM + AOT so this issue is native specific.

@sdeleuze
Copy link
Contributor

sdeleuze commented Jul 7, 2022

As discussed with @sbrannen, we need to replace the SpringProxyRuntimeHintsRegistrar by an AOT processor that will register dynamically the required proxies like here CommandLineRunner.class, Finder.class, SpringProxy.class, Advised.class, DecoratingProxy.class + reflection entries for proxied interfaces with declared methods.

sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Jul 7, 2022
A dynamic mechanism to register proxy and reflection
hints for classes annotated by @transactional is needed
instead.

See spring-projectsgh-28717
@sdeleuze sdeleuze changed the title Add support for Transactional in native-image Add support for @Transactional in native-image Jul 8, 2022
@sdeleuze sdeleuze changed the title Add support for @Transactional in native-image Add support for @Transactional in native images Jul 8, 2022
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Jul 8, 2022
This commit introduces a TransactionBeanRegistrationAotProcessor
in charge of creating the required proxy and reflection hints
when @transactional is detected on beans.

It also refines DefaultAopProxyFactory to throw an exception
when a subclass-based proxy is created in native images
since that's unsupported for now (see spring-projectsgh-28115 related issue).

Closes spring-projectsgh-28717
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) theme: aot An issue related to Ahead-of-time processing
Projects
None yet
Development

No branches or pull requests

3 participants