title: Spring boot application: CommandLine runner exception
body: <p>I am working on an application using spring data jpa. I am streaming ~1000000 fields from a Mysql database that work fine inside a Junit test container. But when I run the same logical code with the CommandLineRunner, the application hangs after returning ~10,000 fields with the following stacktrace:</p>
  
  <pre><code>07 Mar 2017 10:49:24  INFO MysqlApplication - Field number: 12172
  2017-03-07 10:49:58.340 ERROR 22072 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed
  
  java.lang.IllegalStateException: Failed to execute CommandLineRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      at org.cohort.alive.data.facade.mysql.MysqlApplication.main(MysqlApplication.java:21) [classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
      at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.8.RELEASE.jar:1.3.8.RELEASE]
  Caused by: java.lang.NullPointerException: null
      at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1045) ~[mysql-connector-java-5.1.39.jar:5.1.39]
      at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5202) ~[mysql-connector-java-5.1.39.jar:5.1.39]
      at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:73) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1696) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1628) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.loader.Loader.getRow(Loader.java:1515) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:726) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:696) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.loader.Loader.loadSingleRow(Loader.java:392) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.internal.ScrollableResultsImpl.prepareCurrentRow(ScrollableResultsImpl.java:237) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:121) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
      at org.springframework.data.jpa.provider.PersistenceProvider$HibernateScrollableResultsIterator.hasNext(PersistenceProvider.java:454) ~[spring-data-jpa-1.9.5.RELEASE.jar:na]
      at java.util.Iterator.forEachRemaining(Iterator.java:115) ~[na:1.8.0_111]
      at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_111]
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_111]
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_111]
      at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) ~[na:1.8.0_111]
      at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) ~[na:1.8.0_111]
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_111]
      at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) ~[na:1.8.0_111]
      at org.cohort.alive.data.facade.mysql.MysqlApplication.run(MysqlApplication.java:35) [classes/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
      ... 11 common frames omitted
  
  2017-03-07 10:49:58.343  INFO 22072 --- [  restartedMain] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/N:/git/clinical-pipeline/cognitive-nlu/target/classes/]
  2017-03-07 10:49:58.343  INFO 22072 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 
  
  Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
  
  
  2017-03-07 10:49:58.343  INFO 22072 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@10086c99: startup date [Tue Mar 07 10:49:16 PST 2017]; root of context hierarchy
  2017-03-07 10:49:58.350  INFO 22072 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
  2017-03-07 10:49:58.352  INFO 22072 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
  </code></pre>
  
  <p>I am stumped and cannot figure it out. Really appreciate if someone can giving me any pointers</p>
  
  <p>EDIT (MysqlApplication.java)</p>
  
  <pre><code>public class MysqlApplication implements CommandLineRunner{
  
      public static void main(String[] args) {
          SpringApplication.run(MysqlApplication.class, args);
      }
  
      @Autowired
      BusinessRepository businessRepository;
  
      final static private Logger LOGGER = Logger.getLogger("MysqlApplication");
  
      public void run(String... args) throws Exception {
  
          LOGGER.info("Streaming All....");
           Stream&lt;business&gt; concepts = businessRepository.streamAll();
           int [] j ={0};
           concepts.forEach(i -&gt; {
               LOGGER.info("Field Number: "j[0]++");
       }
  }
  </code></pre>
  
  <p>EDIT 2: I think I solved the problem by annotating the application class with transactional. Although I do not really understand why that makes a difference. Would be great if someone can shed some light on this.</p>