Spring Batch & Azure SQL Server : SQL Server did not return a response. The connection has been closed

Viewed 266

I have a spring batch application that uses Azure SQL server, it runs without any issues and updates the Database for a most part however I am getting the following error occasionally

enter image description here

enter image description here

what does it mean? how to handle this?

Note: this is a long running job executes for more than 2hrs. The above error reported after ~1.45hrs.

I am reading the data from CSV file using FlatfileReader & writing into Azure SQL Server using ItemWriter as mentioned below

public class StoreWriter implements ItemWriter<List<Store>> {

   Logger logger = Logger.getLogger(StoreWriter.class);

   private HibernateItemWriter<Store> hibernateItemWriter;

   public StoreWriter(HibernateItemWriter<Store> hibernateItemWriter) {
      this.hibernateItemWriter = hibernateItemWriter;
   }

   @Override
   public void write(List<? extends List<Store>> items) throws Exception {
      for (List<Store> Store : items) {
        hibernateItemWriter.write(Store);
      }
      logger.info(String.format("Store Processing Completed %s", new LocalDateTime()));
   }
}

Below is my Hibernate configuration

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" lazy-init="true">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- HikariCP Database bean -->
<bean id="demoDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean>

<!-- HikariConfig config that is fed to above dataSource -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="maximumPoolSize" value="50" />
    <property name="idleTimeout" value="30000" />
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://demo.database.windows.net:1433;database=sqldb;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;" />
    <property name="username" value="user1" />
    <property name="password" value="p@ssword1" /
</bean>
<bean class="org.springframework.batch.core.scope.StepScope" />
1 Answers

Please try using the annotation "@Transactional (propagation = Propagation.REQUIRES_NEW)" on the method where the records were committed.

Related