Spring data mongodb not closing mongodb connections

vivex

I am using spring-data-mongodb (1.7.0.RELEASE) with spring-webmvc framework for my web application. I am using basic CRUD functions using mongoRepository but i am not closing mongo connections in my code cause i thought that spring-data-mongodb will close it by itself, But it keeps on opening new connections and not closing them. These too many connections ares crashing my application and i have to restart tomcat again and again (twice a day) to overcome this.

Note: Spring Application & mongod is on same server. This is log just after crashing -

    2015-07-17T01:31:20.068-0400 I NETWORK  [conn3645] end connection 127.0.0.1:55302 (2583 connections now open)
    2015-07-17T01:31:20.071-0400 I NETWORK  [conn1713] end connection 127.0.0.1:48174 (2352 connections now open)
    2015-07-17T01:31:20.072-0400 I NETWORK  [conn2250] end connection 127.0.0.1:51017 (2325 connections now open)
    2015-07-17T01:31:20.072-0400 I NETWORK  [conn2149] end connection 127.0.0.1:50670 (2320 connections now open)

This is log after restarting tomcat

2015-07-17T01:31:29.994-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53599 #3984 (1 connection now open)
2015-07-17T01:31:33.263-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53740 #3985 (2 connections now open)
2015-07-17T01:31:33.580-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53750 #3986 (3 connections now open)
2015-07-17T02:10:06.477-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50086 #3987 (4 connections now open)
2015-07-17T02:10:06.590-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50090 #3988 (5 connections now open)
2015-07-17T02:10:11.682-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50242 #3989 (6 connections now open)
2015-07-17T02:10:11.780-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50244 #3990 (7 connections now open)
2015-07-17T02:10:12.545-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50255 #3991 (8 connections now open)
2015-07-17T02:10:12.605-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50258 #3992 (9 connections now open)
2015-07-17T02:10:13.413-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:50299 #3993 (10 connections now open)

it increase whenever i sends request to the app.

And this is the tomcat log just after crash -

Jul 16, 2015 3:59:57 PM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:404)
    at java.net.ServerSocket.implAccept(ServerSocket.java:545)
    at java.net.ServerSocket.accept(ServerSocket.java:513)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)
    at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:222)
    at java.lang.Thread.null(Unknown Source)

its a development server, it have traffic lesser than 10 call per minute.

Someone please suggest how should i close these connections ?

Umesh Mishra

The MongoClient maintains a connection pool,You open a Db connection once with MongoClient and reuse it across your application because setting up a new TCP connection is EXPENSIVE timewise and memory wise that's why you reuse connections. Also a new connection will cause a new Thread to be created on MongoDB using memory on the Db as well.

  • point to be noted that there is a race condition in the connectToMongo method. You need to synchronize access to that method to ensure that at most one instance of MongoClient is ever created.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related