1

I have a SpringBatch project where I want to catch an exception thrown when the application cannot find the datasource. I've already by-passed this, so it will use 'in memory DAO objects' instead of tables.. but it still throws an exception when datasource is not found.
I want to catch that exception and throw my own error code, but I have no idea where the try/catch block must be placed.

Here is a piece of the error log:

2016-11-24 09:25:36.171  INFO 36770 --- [main] c.d.d.e.config.ReaderConfiguration : Configuring FlatFileItemReader for [MAP]
2016-11-24 09:25:51.664 ERROR 36770 --- [main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host [***], port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190) ~[sqljdbc4.jar:na]

This is overridden to bypass table creation. Also, I use two datasources and this class needed to be here anyway.

@Component
public class MyBatchConfigurer extends DefaultBatchConfigurer {
    //Spring batch needs this in order to allow to use more than one datasource

    @Override
    public JobRepository createJobRepository() throws Exception {
        return new MapJobRepositoryFactoryBean().getObject();
    }
}

To be noted that I even tried to place try/catch on "the main" method.. and it still throws the exception above.. and then gets to the breakpoint inside catch. Also, I tried creating the datasource manually.. but to no avail. Even more, ApplicationEvent doesn't seem to work either.

This is a log where datasource is found:

2016-10-25 16:05:13 [main] INFO  c.d.d.e.config.CompanyConfiguration - Configure FlatFileItemReader for [IW]
2016-10-25 16:05:13 [main] INFO  o.s.jdbc.datasource.init.ScriptUtils - Executing SQL script from class path resource [org/springframework/batch/core/schema-sqlserver.sql]
2016-10-25 16:05:13 [main] INFO  o.s.jdbc.datasource.init.ScriptUtils - Executed SQL script from class path resource [org/springframework/batch/core/schema-sqlserver.sql] in 49 ms.
2016-10-25 16:05:13 [main] INFO  o.s.b.f.config.PropertiesFactoryBean - Loading properties file from URL [jar:file:/home/etl/d-d/d-e-1.0.4-SNAPSHOT.jar!/lib/spring-integration-core-4.2.5.RELEASE.jar!/META-INF/spring.integration.default.properties]
Catalin Podariu
  • 148
  • 2
  • 8
  • 1
    It would be interesting to know, why you want to do that. Some remarks: 1. using the MapJobRepository is NOT intended for production. 2. It will be hard to catch this exception, since this exception is thrown when the whole springcontext ist started/initialised, something that happens automatically when using SpringBoot. And I guess, this is before any ApplicationEvents will be sent. I've made some remarks on how to use more than one datasource: http://stackoverflow.com/questions/40380982/spring-batch-resource-must-not-be-null/40395453#40395453 this could be helpful – Hansjoerg Wingeier Nov 24 '16 at 10:03
  • Indeed, it is not intended for production and it will not stay this way. But the reason I want to catch this exception is that I need to throw my own custom error code and with it a Nagios alarm (coupled with Opsview it is very helpful, for the non-technical people using the application, to see what's wrong, than going through the output log) ..hence the need to do all this. – Catalin Podariu Nov 24 '16 at 11:47
  • 1
    You are using spring boot, right? – Hansjoerg Wingeier Nov 24 '16 at 11:54
  • if yes, my answer to this question could be helpful: http://stackoverflow.com/questions/40427590/make-a-spring-batch-job-exit-with-non-zero-code-if-an-exception-is-thrown – Hansjoerg Wingeier Nov 24 '16 at 11:57
  • init() method from org.apache.tomcat.jdbc.pool.ConnectionPool logs an SQLException on line 468. So, I decided to do this: @Configuration public class DataSourceConfiguration { // Local - test @ConfigurationProperties(prefix = "test.datasource") public DataSource getTestDataSource() throws Exception { DataSource dataSource = DataSourceBuilder.create().build(); dataSource.getConnection(); return dataSource; } ..and I just catch the exception thrown, by assigning the datasource by hand. Not the best way to do it.. but, it's a middle ground. – Catalin Podariu Nov 24 '16 at 13:18
  • How do you declare your datasource and driver ? What are they ? – Asoub Nov 25 '16 at 14:50

0 Answers0