1
votes

I have a spring boot application which connects to two data sources using mybatis. Spring boot does not load on server startup, I have checked the logs during the server startup and I do not see any spring boot related messages in it. The application status is displayed as Started in the WAS console after the server startup.

The application does not work until I restart it, and during the application restart, I can see the spring boot related messages in the logs. The application works fine after its restarted.

Server version: IBM WebSphere Application Server Network Deployment (8.5.5.14) Java version: 8.0.2.10 (IBM WebSphere SDK Java Technology Edition)

@SpringBootApplication
@ComponentScan(basePackages="org.sample.solve.*")
@MapperScan("org.sample.solve.sa.db.mappers")
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

Configuration :

package org.sample.solve.sa;

import .....

@Configuration
public class MybatisDBConfig  {

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    public static final String SQL_SESSION_FACTORY_NAME_1 = "sqlSessionFactory1";
    public static final String SQL_SESSION_FACTORY_NAME_2 = "sqlSessionFactory2";

    public static final String MAPPERS_PACKAGE_TSC      = "org.sample.solve.sa.db.mappers"; // mapper interface package
    public static final String MAPPERS_PACKAGE_SERVICES_DB  = "org.sample.solve.sa.db.services_db.mappers"; // mapper interface package

    @Profile("local")
    @Bean(name = "tscDB")
    @Primary
    @ConfigurationProperties(prefix = "sp.firstDatasource")
    public DataSource dataSource1() {
        DataSource dataSource = null;
        try {
            dataSource = DataSourceBuilder.create().build();
            log.info("tsc datasource");
        }catch(Exception excep) {
            excep.printStackTrace();
        }
        return dataSource;
    }

    @Profile("local")
    @Bean(name = "servicesDB")
    @ConfigurationProperties(prefix = "sp.secondDatasource")
    public DataSource dataSource2() {
        DataSource dataSource = null;
        try {
            dataSource = DataSourceBuilder.create().build();
            log.info("services datasource");
        }catch(Exception excep) {
            excep.printStackTrace();
        }
        return dataSource;
    }


    @Profile({"dev", "fvt", "uat", "prod"})
    @Bean(name = "tscDB")
    @Primary
    public DataSource primaryDataSource() {
        System.out.println("sacpDS_JNDI : jdbc/QAServiceSACP");
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource("jdbc/QAServiceSACP");
        return dataSource;
    }

    @Profile({"dev", "fvt", "uat", "prod"})
    @Bean(name = "servicesDB")
    public DataSource secondaryDataSource() {
        System.out.println("servicesDS_JNDI : jdbc/servicesDS");
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource("jdbc/servicesDS");
        return dataSource;
    }

    @Bean(name = SQL_SESSION_FACTORY_NAME_1)
    @Primary
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("tscDB") DataSource dataSource1) throws Exception {
        SqlSessionFactory sqlSessionFactory = null;
        try {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setTypeAliasesPackage("org.sample.solve.sa.pojo");
            Resource[] mapperLocations = new Resource[] { new ClassPathResource("SASolveMapper.xml") };
            sqlSessionFactoryBean.setMapperLocations(mapperLocations);
            sqlSessionFactoryBean.setDataSource(dataSource1);
            sqlSessionFactory = sqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        }catch(Exception excep) {
            excep.printStackTrace();
            throw new Exception(excep.getMessage());
        }
        return sqlSessionFactory;
    }

    @Bean(name = SQL_SESSION_FACTORY_NAME_2)
    public SqlSessionFactory sqlSessionFactory2(@Qualifier("servicesDB") DataSource dataSource2) throws Exception {
        SqlSessionFactory sqlSessionFactory = null;
        try {
            SqlSessionFactoryBean diSqlSessionFactoryBean = new SqlSessionFactoryBean();
            diSqlSessionFactoryBean.setTypeAliasesPackage("org.sample.solve.sa.pojo");
            Resource[] mapperLocations = new Resource[] {new ClassPathResource("ServicesDBMapper.xml")};
            diSqlSessionFactoryBean.setMapperLocations(mapperLocations);
            diSqlSessionFactoryBean.setDataSource(dataSource2);
            sqlSessionFactory = diSqlSessionFactoryBean.getObject();
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        }catch(Exception excep) {
            excep.printStackTrace();
            throw new Exception(excep.getMessage());
        }
        return sqlSessionFactory;
    }

    @Bean
    @Primary
    public MapperScannerConfigurer mapperScannerConfigurer1() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(MAPPERS_PACKAGE_TSC);
        configurer.setSqlSessionFactoryBeanName(SQL_SESSION_FACTORY_NAME_1);
        return configurer;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer2() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(MAPPERS_PACKAGE_SERVICES_DB);
        configurer.setSqlSessionFactoryBeanName(SQL_SESSION_FACTORY_NAME_2);
        return configurer;
    }

}

And I am setting the active profile using a servlet initializer.

1

1 Answers

-1
votes

I've noticed the same when I tried to create a scheduled job (spring boot app in was server). The job did not start until I poked the application by calling rest service or do something else. Unfortunately I don't know what to do to make it work.