# Elasticsearch automatically closes connection as soon as I run my Java Springboot program

**URL:** <https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069>\
**Category:** Elasticsearch\
**Created:** [April 23, 2022, 8:03am UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069 "2022-04-23T08:03:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [April 23, 2022, 8:03am UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/1 "2022-04-23T08:03:57Z")

</div>

`*[WARN][o.e.x.c.s.t.n.SecurityNetty4Transport] [DESKTOP-ADQ0I0S] received plaintext traffic on an encrypted channel, closing connection Netty4TcpChannel{localAddress=/127.0.0.1:9300, remoteAddress=/127.0.0.1:59255, profile=default*`

I have asked this question in a descriptive manner on stackoverflow here : [https://stackoverflow.com/questions/71971672/elasticsearch-stops-working-after-i-run-the-springboot-java-program-and-elastics](https://stackoverflow.com/questions/71971672/elasticsearch-stops-working-after-i-run-the-springboot-java-program-and-elastics) .Im getting issues on transport layer I guess.  
Please refer to my Stackoverflow question or let me know if I should post the question here too.

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [April 23, 2022, 9:29am UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/2 "2022-04-23T09:29:32Z")

</div>

Your node is using SSL  
Enable SSL in spring boot side

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [April 23, 2022, 8:01pm UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/3 "2022-04-23T20:01:42Z")

</div>

Getting the same error after enabling SSL in Springboot

After running the program I get this error

```auto

  . _______ _ _
 /\\ / ___'___ _ _(_)_ ____ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  ' | ____| .__ |_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot :: (v2.1.1.RELEASE)

2022-04-24 01:24:37.421 INFO 8036 --- [restartedMain] pringBootElasticsearchExampleApplication : Starting SpringBootElasticsearchExampleApplication on DESKTOP-ADQ0I0S with PID 8036 (started by Hp in D:\STS Project\demo)
2022-04-24 01:24:37.428 INFO 8036 --- [restartedMain] pringBootElasticsearchExampleApplication : No active profile set, falling back to default profiles: default
2022-04-24 01:24:37.550 INFO 8036 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-04-24 01:24:37.577 INFO 8036 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-04-24 01:24:38.697 INFO 8036 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2022-04-24 01:24:38.780 INFO 8036 --- [restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 75ms. Found 1 repository interfaces.
2022-04-24 01:24:39.864 INFO 8036 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8443 (https)
2022-04-24 01:24:39.892 INFO 8036 --- [restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-24 01:24:39.893 INFO 8036 --- [restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2022-04-24 01:24:39.915 INFO 8036 --- [restartedMain] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\STS\sts-4.13.0.RELEASE\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.1.v20211116-1657\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/STS/sts-4.13.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.1.v20211116-1657/jre/bin/server;D:/STS/sts-4.13.0.RELEASE//plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.1.v20211116-1657/jre/bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_202\bin;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\MySQL\MySQL Shell 8.0\bin\;C:\Users\Hp\AppData\Local\Microsoft\WindowsApps;;C:\Users\Hp\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Hp\AppData\Roaming\npm;C:\Program Files\Azure Data Studio\bin;C:\Program Files\Azure Data Studio\bin;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1\bin;;D:\STS\sts-4.13.0.RELEASE;;.]
2022-04-24 01:24:40.082 INFO 8036 --- [restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-24 01:24:40.082 INFO 8036 --- [restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2500 ms
2022-04-24 01:24:40.451 INFO 8036 --- [restartedMain] o.elasticsearch.plugins.PluginsService : no modules loaded
2022-04-24 01:24:40.452 INFO 8036 --- [restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2022-04-24 01:24:40.452 INFO 8036 --- [restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2022-04-24 01:24:40.452 INFO 8036 --- [restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2022-04-24 01:24:40.453 INFO 8036 --- [restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2022-04-24 01:24:40.453 INFO 8036 --- [restartedMain] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2022-04-24 01:24:41.852 INFO 8036 --- [restartedMain] o.s.d.e.c.TransportClientFactoryBean : Adding transport node : 127.0.0.1:9300
2022-04-24 01:24:42.252 INFO 8036 --- [restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-04-24 01:24:42.280 ERROR 8036 --- [restartedMain] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{Y2r-zQdGTsOzunZYoM4C4A}{localhost}{127.0.0.1:9300}]
2022-04-24 01:24:42.662 INFO 8036 --- [restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-04-24 01:24:43.903 INFO 8036 --- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8443 (https) with context path ''
2022-04-24 01:24:43.928 INFO 8036 --- [restartedMain] pringBootElasticsearchExampleApplication : Started SpringBootElasticsearchExampleApplication in 7.372 seconds (JVM running for 8.427)

```

As soon as I run the program Elasticsearch closes connection by giving this error

```auto
[2022-04-24T01:27:52,345][WARN][o.e.x.c.s.t.n.SecurityNetty4Transport] [DESKTOP-ADQ0I0S] received plaintext traffic on an encrypted channel, closing connection Netty4TcpChannel{localAddress=/127.0.0.1:9300, remoteAddress=/127.0.0.1:60477, profile=default}

```

modified application.properties and added the below code

```auto
#SSL Configuration
server.ssl.key-alias=selfsigned
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:mykeystore.jks
server.ssl.key-store-password=keypass
server.port=8443

```

created @configuration @Bean class

```auto
package Config;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MySecurityConfig {

	  @Bean
	    public ServletWebServerFactory servletContainer() {
	        // Enabling SSL traffic
	        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory() {
	            @Override
	            protected void postProcessContext(Context context) {
	                SecurityConstraint securityConstraint = new SecurityConstraint();
	                securityConstraint.setUserConstraint("CONFIDENTIAL");
	                SecurityCollection securityCollection = new SecurityCollection();
	                securityCollection.addPattern("/*");
	                securityConstraint.addCollection(securityCollection);
	                context.addConstraint(securityConstraint);
	            }
	        };

	        // Adding HTTP to HTTPS redirect
	        tomcatServletWebServerFactory.addAdditionalTomcatConnectors(httpToHttpsRedirectConnector());

	        return tomcatServletWebServerFactory;
	    }

	    /*
	     If any request comes with port 8080 needs to be redirected to HTTPS on 8443.
	     */
	    private Connector httpToHttpsRedirectConnector() {
	        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
	        connector.setScheme("http");
	        connector.setPort(8080);
	        connector.setSecure(false);
	        connector.setRedirectPort(8443);
	        return connector;
	    }
}

```

Idk what im doing wrong, it's been 4 days now im facing this issue, springboot program is not connection with Elasticsearch ☹

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [April 23, 2022, 8:03pm UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/4 "2022-04-23T20:03:57Z")

</div>

Nothing changed after enabling SSL in springboot

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 24, 2022, 12:32am UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/5 "2022-04-24T00:32:13Z")

</div>

Did you set port 9300.... Can you try 9200

`spring.data.elasticsearch.cluster-nodes=localhost:9200`

You can not connect to the 9300 transport client port in Elasticsearch 7.x+

Looks like you are running Elasticsearch 8.1.3 ... Definitely can not connect to the transport client.

[https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.transport](https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.transport)

What version of spring data Elasticsearch

[https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/](https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/)

Your going to need to use the high level rest client

[https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest](https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest)

Which I think is already deprecated see

> **[Elasticsearch Java API Client \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html)**

> **[Java REST Client \[7.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html)**

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [April 24, 2022, 12:56pm UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/6 "2022-04-24T12:56:55Z")

</div>

Yes, Im using Elasticsearch 8.1.3. Thanks, I will check it out and let you know.

---

<div class="post-metadata">

**Author:** ![Tasdiq\_Shaikh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tasdiq_shaikh/32/76982_2.png) [@Tasdiq\_Shaikh](https://discuss.elastic.co/u/Tasdiq_Shaikh)\
**Post date:** [April 26, 2022, 3:25pm UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/7 "2022-04-26T15:25:32Z")

</div>

Thanks, I tried using the Java API Client but got some issues and there aren't enough toutorials on internet related to Java API client, I'm new to Springboot and Elasticsearch, so now I have installed Elasticsearch 7.12 and my springboot application is running fine but got "_null pointer exception_" after calling the rest method, it seems it is not able to connect to the Elasticsearch repository.  
Thank you, you have answered my question.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 26, 2022, 3:35pm UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/8 "2022-04-26T15:35:58Z")

</div>

And you have it pointing to port 9200 that is the rest endpoint

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 24, 2022, 3:36pm UTC](https://discuss.elastic.co/t/elasticsearch-automatically-closes-connection-as-soon-as-i-run-my-java-springboot-program/303069/9 "2022-05-24T15:36:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
