1

In my program i am using Single Static Connection object for Multiple Threads.

this below code is regarding to DB connection,

 public static String DB_DRIVER_CLASS;
 public static String DB_URL;
 public static String DB_USERNAME;
 public static String DB_PASSWORD;
 public static Connection con;


        DB_DRIVER_CLASS = dbProperties.getProperty("db.driverClass");
        DB_URL = dbProperties.getProperty("db.url");
        DB_USERNAME = dbProperties.getProperty("db.userName");
        DB_PASSWORD = dbProperties.getProperty("db.password");

        try {
            Class.forName(DB_DRIVER_CLASS);
            con=DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

This below code is regarding Treads(I am using 10 Threads with same code)

        synchronized public void statusUpdate_lane1(){
          PreparedStatement pst=con.prepareStatement(lane_1_to_get_data);//lane_1_to_get_data is SELECT Query
          ResultSet rs = pst.executeQuery();
          if(FLAG_1 == 'Y'){//FLAG_1  is DB data
            pst.executeUpdate(lane_1_to_set_data);//lane_1_to_set_data is UPDATE Query


          }
            rs.close();
            pst.close();
       }
       public void run(){
             while(true){   
             this.statusUpdate_lane1();}

          }

what ever data i am taking from db i need to display in my JFrame UI(Like as live).So the multiple threads execute continuously by using while loop.

The application working fine without errors.

But the problem is when i am taking data from local db (localhost) the UI display data is fast. When i am taking data from another Db like in the place of localhost i am using 192.168.1.151 means i am taking data from another system DB. At that time UI display data is taking more time(Means retrieving data from db is delaying ).

=======> When i maintaining different connection objects for different Threads,The data retrieving from local DB as well as 192.168.1.151 DB is fast.

In this i have another problem I am opening connection in a while loop calling method and closing in the connection in that method only.

In That senior it showing below one

    Apr 02, 2016 12:37:49 PM lsdu_application.Lane5 statusUpdate_lane5
     SEVERE: null
      com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The driver          was unable to create a connection due to an inability to establish the client portion of a socket.

       This is usually caused by a limit on the number of sockets imposed by   the operating system. This limit is usually configurable. 

       For Unix-based platforms, see the manual page for the 'ulimit'   command. Kernel or system reconfiguration may also be required.

        For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271).
         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
         at   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor9.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at lsdu_application.Lane5.statusUpdate_lane5(Lane5.java:44)
at lsdu_application.Lane5.run(Lane5.java:182)
at java.lang.Thread.run(Thread.java:745)
  Caused by: java.net.BindException: Address already in use: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 16 more  

===>But this is not effecting my application(My application working Fine).

But in command prompt it showing like below

  C:\>netstat -a -o

   Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            DESKTOP-P18FE2O:0      LISTENING       376
  TCP    0.0.0.0:445            DESKTOP-P18FE2O:0      LISTENING       4
  TCP    0.0.0.0:2291           DESKTOP-P18FE2O:0      LISTENING       3028
  TCP    0.0.0.0:3306           DESKTOP-P18FE2O:0      LISTENING       2540
  TCP    0.0.0.0:6646           DESKTOP-P18FE2O:0      LISTENING       3680
  TCP    0.0.0.0:8092           DESKTOP-P18FE2O:0      LISTENING       4
  TCP    0.0.0.0:49664          DESKTOP-P18FE2O:0      LISTENING       768
  TCP    0.0.0.0:49665          DESKTOP-P18FE2O:0      LISTENING       1096
  TCP    0.0.0.0:49666          DESKTOP-P18FE2O:0      LISTENING       764
  TCP    0.0.0.0:49667          DESKTOP-P18FE2O:0      LISTENING       1800
  TCP    0.0.0.0:49668          DESKTOP-P18FE2O:0      LISTENING       908
  TCP    0.0.0.0:49669          DESKTOP-P18FE2O:0      LISTENING       896
  TCP    0.0.0.0:65530          DESKTOP-P18FE2O:0      LISTENING       6992
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:50114  ESTABLISHED     2540
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54752  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54768  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54804  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54805  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54808  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54810  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54832  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54835  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54844  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54874  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54876  TIME_WAIT       0
  TCP    127.0.0.1:3306         DESKTOP-P18FE2O:54882  TIME_WAIT       0

TCP 127.0.0.1:3306 DESKTOP-P18FE2O:54889 TIME_WAIT 0

====>The above line displaying keep on going, when i close the my application after some time the above line is stop to display.

what is the reason ?
What is the meaning of TIME_WAIT message ?

If i am not closing connection object in my application,what happens? 
but i  am closing my application directly by press (X) or ALT+F4.   
honey1
  • 75
  • 1
  • 14
  • You should not use a static connection, but a pool of connection if possible, see http://stackoverflow.com/questions/1039419/when-to-close-connection-statement-preparedstatement-and-resultset-in-jdbc and http://stackoverflow.com/questions/3083333/jdbc-connection-pooling-connection-reuse for example –  Apr 02 '16 at 07:43
  • what's the problem with static connection? – honey1 Apr 04 '16 at 04:30
  • what is the reason ? What is the meaning of TIME_WAIT message ? If i am not closing connection object in my application,what happens? but i am closing my application directly by press (X) or ALT+F4. – honey1 Apr 04 '16 at 04:36
  • please give me the reply – honey1 Apr 04 '16 at 05:02
  • Any one give me some idea – honey1 Apr 05 '16 at 06:08

0 Answers0