lukas-krecan / ShedLock

Distributed lock for your scheduled tasks
Apache License 2.0
3.66k stars 518 forks source link

Rollback Transaction not working in SpringBootTest when i include the ShedLock library in the pom dependency #328

Open ursankar opened 4 years ago

ursankar commented 4 years ago

Rollback Transaction not working in SpringBootTest when i include the ShedLock library in the pom dependency:

HI @lukas-krecan Can you please help with this issue. I was able to test the Shedlock successfully running in multiple pod instances in kubernetes using MySql/Aurora database. However, i have noticed now, running into a problem with Test class which used to have Transaction Rollback function working but it is not working now after i include the Shedlock in pom dependency. . Here are the annotations i have in the test class;

@SpringBootTest @RunWith(value = SpringRunner.class) @Transactional public class MyTest {

}

When i run the test, i don't see the rollback transaction info which i used to see when i did not include the "ShedLock libraries in the pom file" .

**Rollback transaction log below that i can see if i comment out ShedLock in POM.xml ***** contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6572421, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@6d763516, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@8cc191ca, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5b8dfcc1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@145f66e3], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@69a294d8]; rollback [true] 1 11:32:41.716 [main][tid=test_1234] INFO o.s.t.c.t.TransactionContext - Rolled back transaction for test:


Below is jpa info in my application.yml config and also the properties for my two datasource. I am using the mysql datasource

spring: jpa: database-platform: org.hibernate.dialect.H2Dialect show-sql: true

mysql: datasource: jdbc-url: jdbc:mysql://localhost:3307/shedlockdb username: admin password: *** driverClassName: com.mysql.cj.jdbc.Driver h2: datasource: jdbc-url: jdbc:h2:mem:testdb username: driverClassName: org.h2.Driver


This is the bean in my startup class to configure Shedlock datasource. @Bean public LockProvider lockProvider(@Qualifier("sqlDataSource") DataSource dataSource) { return new JdbcTemplateLockProvider(builder() .withJdbcTemplate(new JdbcTemplate(dataSource)) .usingDbTime() .build()); }


This is my DatabaseConfiguration.java

@Slf4j @Configuration @EntityScan( basePackageClasses = AbstractEntity.class) @EnableJpaAuditing @EnableJpaRepositories( basePackageClasses = AbstractEntityRepository.class) @EnableTransactionManagement public class DatabaseConfiguration {

@Value("${mysql.datasource.jdbc-url}") private String mySqlHost; @Value("${mysql.datasource.username}") private String mySqlUser; @Value("${mysql.datasource.password}") private String mySqlPass; @Value("${mysql.datasource.driverClassName}") private String mySqlDriver;

@Value("${h2.datasource.jdbc-url}") private String h2Host; @Value("${h2.datasource.username}") private String h2User; @Value("${h2.datasource.driverClassName}") private String h2Driver;

@Bean(name = "sqlDataSource") public DataSource getMysqlDataSource() { log.info("getMysqlDataSource; mySqlHost={}; mySqlUser={}; mySqlDriver={}",mySqlHost,mySqlUser,mySqlDriver); DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); dataSourceBuilder.url(mySqlHost); dataSourceBuilder.username(mySqlUser); dataSourceBuilder.password(mySqlPass); dataSourceBuilder.driverClassName(mySqlDriver); DataSource ds = dataSourceBuilder.build(); log.info("sqlDataSource; dataSource=={}; ",ds.toString()); return ds; }

@Primary @Bean(name = "h2DataSource") public DataSource getH2DataSource() { log.info("getH2DataSource; h2Host={}; h2User={}; h2Driver={}",h2Host,h2User,h2Driver); DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create(); dataSourceBuilder.url(h2Host); dataSourceBuilder.username(h2User); dataSourceBuilder.driverClassName(h2Driver); DataSource ds = dataSourceBuilder.build(); log.info("h2DataSource; dataSource=={}",ds.toString()); return ds; } }

lukas-krecan commented 4 years ago

Hi, thanks for the report. Does the tested method call ShedLock annotated method or does it happen for unralated tests too?

zhahaoyu commented 3 years ago

Any updates?

bratwurzt commented 9 months ago

Hi, thanks for the report. Does the tested method call ShedLock annotated method or does it happen for unralated tests too?

For me, it happens for unrelated tests, too.