0

I'm using spring-boot-starter-data-jdbc and infobip-spring-data-jdbc-querydsl-boot-starter(5.4.2) together. In my tests with org.springframework.transaction.annotation.Transactional annotation I have the following code:

@Transactional
void test() {
    repository.save(ReportList.builder().userName("username").build());

    var reports = repository.query(query -> query
            .select(entityProjection())
            .from(QReportList.reportList)
            .fetch());
}

Expected result: variable reports is not empty Actual result: variable reports is empty

If I remove @Transactional (or use default findAll method from QuerydslJdbcRepository) it starts to return results. Am I doing something wrong?

user3435425
  • 192
  • 1
  • 18

1 Answers1

1

After some time of digging I found the following lines of code:

public class SQLQueryFactory extends AbstractSQLQueryFactory<SQLQuery<?>> {

    static class DataSourceProvider implements Provider<Connection> {

        @Override
        public Connection get() {
            try {
                return ds.getConnection();
            } catch (SQLException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }

ds.getConnection() returns new connection instead of existing one (with transaction).

I added querydsl-sql-spring with the following configuration and my code start to work as expected:

@Bean
public SQLQueryFactory queryFactory(com.querydsl.sql.Configuration configuration, DataSource dataSource) {
   var provider = new SpringConnectionProvider(dataSource);
   return new SQLQueryFactory(configuration, provider);
}
user3435425
  • 192
  • 1
  • 18