在我的程序中,我使用单线程静态连接对象进行多线程。
以下代码与数据库连接有关,
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();
}
以下代码是关于Treads(我使用10个具有相同代码的线程)
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();}
}
我从db获取的数据我需要在我的JFrame UI中显示(就像现场一样)。因此,使用while循环连续执行多个线程。
应用程序正常运行没有错误。
但问题是当我从本地db(localhost)获取数据时,UI显示数据很快。 当我从另一个Db获取数据时,就像在localhost的地方我使用192.168.1.151意味着我从另一个系统DB获取数据。那时UI显示数据需要更多时间(从db中检索数据意味着延迟)。
=======> 当我为不同的线程维护不同的连接对象时,从本地DB和192.168.1.151 DB检索的数据很快。
在此我有另一个问题,我在while循环调用方法中打开连接,并仅在该方法中关闭连接。
在那个大四中,它显示在一个
之下 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
===&gt;但这不会影响我的申请(我的申请工作正常)。
但在命令提示符下显示如下
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
====&gt;上面一行显示继续,当我关闭我的应用程序一段时间后,上面一行停止显示。
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.