I try to use Spring transactions whith AspectJ
**My Project: build.config
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id "io.freefair.aspectj.post-compile-weaving" version "5.1.0"
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
project.ext {
aspectjVersion = "1.8.2"
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework:spring-jdbc'
implementation 'org.springframework:spring-tx'
implementation 'org.postgresql:postgresql'
implementation 'org.aspectj:aspectjrt'
implementation 'org.aspectj:aspectjweaver'
implementation 'org.aspectj:aspectjtools'
implementation 'org.springframework:spring-aspects:5.3.2'
implementation 'org.springframework:spring-instrument:5.3.2'
}
test {
useJUnitPlatform()
}
DataConfig.class
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
public class DataConfig {
@Bean
public DataSource postgres() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres?serverTimezone=UTC");
dataSource.setUsername("postgres");
dataSource.setPassword("admin");
dataSource.setSchema("public");
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(postgres());
}
}
MainDAO.class
@Repository
public class MainDAO {
private final JdbcTemplate jdbcTemplate;
public MainDAO(DataSource postgres) {
this.jdbcTemplate = new JdbcTemplate(postgres);
}
public Integer getSoundsCount() {
return jdbcTemplate.queryForObject(
"SELECT COUNT(*) FROM Sound", Integer.class);
}
@Transactional(propagation = Propagation.MANDATORY)
public void insertSound() {
insertAuthor();
jdbcTemplate.update(
"INSERT INTO Sound (author, name, id) VALUES (?,?,?)",
0, "Spring", 0);
}
}
When I call method insertSound from my service it run without exception. But it should throw exception becouse method insertSound have propogation.MANDATORY.
If i change Advice mode of EnableTransactionManegment to mode=Advice.PROXY then i get exception
But with mode=Advice.ASPECTJ, transaction dont work.
I also try run application with annotation EnableLoadTimeWeaving and set library spring-instrument as java agent but it also transaction dont work:
What should i do for the transaction to work with mode=Advice.ASPECTJ ?