Search code examples
javaspringspring-batchspring-config

Failed to use repositories and services having 2 spring contexts


I have 1 spring context named PersistenceJPAConfig. Now I want to configure a spring batch and for that I have added a new class with @Configuration annotation and @EnableBatchProcessing. After adding new configuration class, I got error trying to use repository methods: nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress. I know this is because I have a parent spring context and a child context which means I will have 2 instance for every repository and every service. I have tried to exclude repository scanning and service scanning with:

@ComponentScan(useDefaultFilters = false,
        excludeFilters = {@ComponentScan.Filter(Repository.class), @ComponentScan.Filter(Service.class), @ComponentScan.Filter(Configuration.class)}) 

but it's not working. The only solution until now is to move all the beans from the second configuration to the first one, but I don't want that. How to solve this conflict between the contexts?

Main context:

package com.netoptics.server;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.websilicon.security.SecurityGlobals;
import com.websilicon.util.AppConfig;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories({"com.whitelist.manager.repositories", "com.wsnms", "com.websilicon"})
public class PersistenceJPAConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPackagesToScan("com.whitelist.manager", "com.wsnms", "com.websilicon");
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
        entityManagerFactoryBean.setJpaProperties(additionalProperties());

        return entityManagerFactoryBean;

    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);

        return transactionManager;
    }

    @Bean
    public DataSource dataSource() {
        String databaseDriver = AppConfig.getInstance().getString("dataDatabaseDriver", "");
        String databaseUrl = AppConfig.getInstance().getString("dataDatabaseUrl", "");
        String databaseUsername = AppConfig.getInstance().getString("dataDatabaseUsername", "");
        String dataDatabasePassword = AppConfig.getInstance().getPassword("dataDatabasePassword", SecurityGlobals.KEY, "");

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(databaseDriver);
        dataSource.setUrl(databaseUrl);
        dataSource.setUsername(databaseUsername);
        dataSource.setPassword(dataDatabasePassword);

        return dataSource;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    private Properties additionalProperties() {

        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.jdbc.batch_size", "1000");

        return properties;

    }

}

Second context for configuring spring batch:

@Configuration
@EnableBatchProcessing
@ComponentScan(useDefaultFilters = false,
        excludeFilters = {@ComponentScan.Filter(Repository.class), @ComponentScan.Filter(Service.class)})
public class SaveImsiCSVBatchConfig {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;

    @Autowired
    private Environment environment;

    @Autowired
    private WmAdminImsisResourceHelper wmAdminImsisResourceHelper;

    @Bean
    public JobRepository jobRepository() {
        MapJobRepositoryFactoryBean factoryBean = new MapJobRepositoryFactoryBean(new ResourcelessTransactionManager());
        try {
            return factoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Bean
    public JobLauncher jobLauncher(JobRepository jobRepository) {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);

        return jobLauncher;
    }

    @Bean
    @StepScope
    public JdbcCursorItemReader<WmPushedImsi> reader(@Value("#{jobParameters['sortProperty']}") String sortProperty,
            @Value("#{jobParameters['sortValue']}") String sortValue, @Value("#{jobParameters['username']}") String usernameFilter,
            @Value("#{jobParameters['imsinumber']}") String imsiNumberFilter) {
        JdbcCursorItemReader<WmPushedImsi> reader = new JdbcCursorItemReader<>();
        reader.setDataSource(dataSource);
        String sql =
                "select us.username, wp.imsinumber, wp.startdate, wp.expiredate, case when wp.failedpushbatchid is not null or wp.faileddeletebatchid is not null then 'Yes' ELSE 'No' end as dirty from\n"
                        + "wm_pushed_imsi wp INNER JOIN wm_admin_user wu on wp.userid = wu.id INNER JOIN users us on wu.userid = us.id";
        if (usernameFilter != null && imsiNumberFilter != null) {
            sql += " AND us.username LIKE '%" + usernameFilter + "%' AND wp.imsinumber LIKE '%" + imsiNumberFilter + "%'";
        } else if (usernameFilter != null) {
            sql += " AND us.username LIKE '%" + usernameFilter + "%'";
        } else if (imsiNumberFilter != null) {
            sql += " AND wp.imsinumber LIKE '%" + imsiNumberFilter + "%'";
        }
        if (sortProperty != null) {
            sql += " order by " + sortProperty + " " + sortValue;
        }
        reader.setSql(sql);
        reader.setRowMapper(new ImsiRowMapper());
        return reader;
    }


    @Bean
    public ImsiProcessor processor() {
        return new ImsiProcessor();
    }


    @Bean
    @StepScope
    public FlatFileItemWriter<WmPushedImsi> writer(@Value("#{jobParameters['currentDate']}") Date currentDate) {
        wmAdminImsisResourceHelper.createDirectoryForSavingCsv();
        String fileName = wmAdminImsisResourceHelper.createFileNameForCsv(currentDate) + environment.getProperty("CSVEXTENSION");
        String columnsTitle = Arrays.toString(new String[] {environment.getProperty("CSV_IMSINUMBER"), environment.getProperty("CSV_USERNAME"),
                environment.getProperty("CSV_STARTDATE"), environment.getProperty("CSV_EXPIREDATE"), environment.getProperty("CSV_DIRTY")});
        FlatFileItemWriter<WmPushedImsi> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(fileName));
        writer.setHeaderCallback(writerHeader -> writerHeader.write(columnsTitle.substring(1, columnsTitle.length() - 1)));
        writer.setLineAggregator(new DelimitedLineAggregator<>() {
            {
                setDelimiter(",");
                setFieldExtractor(new BeanWrapperFieldExtractor<>() {
                    {
                        setNames(new String[] {WmPushedImsi_.IMSI_NUMBER, "username", WmPushedImsi_.START_DATE, WmPushedImsi_.EXPIRE_DATE, "dirty"});
                    }
                });
            }
        });

        return writer;
    }


    @Bean
    public Step stepToCreateCsvFile() {
        return stepBuilderFactory.get(Objects.requireNonNull(environment.getProperty("CSV_STEP_CREATE_FILE"))).<WmPushedImsi, WmPushedImsi>chunk(50000)
                .reader(reader("", "", "", "")).processor(processor()).writer(writer(null)).build();

    }

    @Bean
    public Step stepToDeleteFileAndCreateArchive() {
        FileArchiveAndDeletingTasklet task = new FileArchiveAndDeletingTasklet();
        task.setWmAdminImsisResourceHelper(wmAdminImsisResourceHelper);
        task.setDateString(environment.getProperty("CSV_DATE"));
        return stepBuilderFactory.get(Objects.requireNonNull(environment.getProperty("CSV_STEP_CREATE_ARCHIVE"))).tasklet(task).build();
    }

    @Bean
    public Job exportImsiCSVJob() {
        return jobBuilderFactory.get(Objects.requireNonNull(environment.getProperty("CSV_EXPORT_JOB"))).incrementer(new RunIdIncrementer())
                .start(stepToCreateCsvFile()).next(stepToDeleteFileAndCreateArchive()).build();
    }

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
        JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
        jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
        return jobRegistryBeanPostProcessor;
    }

    public class ImsiRowMapper implements RowMapper<WmPushedImsi> {

        @Override
        public WmPushedImsi mapRow(ResultSet rs, int rowNum) throws SQLException {
            WmPushedImsi wmPushedImsi = new WmPushedImsi();
            wmPushedImsi.setImsiNumber(rs.getString(WmPushedImsi_.IMSI_NUMBER));
            wmPushedImsi.setUsername(rs.getString("username"));
            wmPushedImsi.setStartDate(rs.getDate(WmPushedImsi_.START_DATE));
            wmPushedImsi.setExpireDate(rs.getDate(WmPushedImsi_.EXPIRE_DATE));
            wmPushedImsi.setDirty(rs.getString("dirty"));

            return wmPushedImsi;
        }

    }


}

Solution

  • You are using the MapJobRepository with a ResourcelessTransactionManager. With this configuration, there are no transactions on Spring Batch side. Hence the error no transaction is in progress.

    You need to configure a JobRepository with the transaction manager you defined in your PersistenceJPAConfig. To do this, you have to define a bean of type BatchConfigurer and override getTransactionManager. Here is an example:

    @Bean
    public BatchConfigurer batchConfigurer() {
        return new DefaultBatchConfigurer() {
                @Override
                public PlatformTransactionManager getTransactionManager() {
                        return new MyTransactionManager();
                }
        };
    }
    

    For more details, please check the Java config section of the reference documentation. Please note that this requires Spring Batch v4.1+.