java 板


LINE

0. 问题描述 - 目的: 将收到的Request网路封包资料写入资料库。 - 启动: 一切正常, 立即制造网路封包皆能正常写入资料库。 - 发生时间点: 有时当隔一段时间(不长且不固定)就会出现Communications link failure, 有时一起动之後, 大量传送封包也偶而会发生Communications link failure。 - 错误讯息: 2013-01-03 03:49:56.798 ERROR [Thread-1] -- SQLException com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 4,415,181 milliseconds ago. The last packet sent successfully to the server was 35 milliseconds ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3589) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3478) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4019) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379) at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:169) at com.apt.deliverSM.smpp.MOReceiver.MOImport(MOReceiver.java:216) at com.apt.deliverSM.smpp.MOReceiver.autowrite(MOReceiver.java:119) at com.apt.deliverSM.smpp.MOReceiver.run(MOReceiver.java:49) at java.lang.Thread.run(Thread.java:619) Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3489) ... 12 more 1. 建立连线程式码 String userName = "myUsername"; String password = "myPassword"; String url = "jdbc:mysql://localhost:3306/myDatabaseName"; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url,userName,password); System.out.println("Database connection established"); 2. 资料写入资料库程式码 PreparedStatement ps = conn.prepareStatement(sql); ps.setTimestamp(1, new Timestamp(new Date().getTime())); ps.setString(2, tokens[0]); ps.setString(3, tokens[1]); ps.setString(4, tokens[2]); ps.setString(5, tokens[3]); ps.setString(6, ""); ps.setString(7, ""); ps.setInt(8, 0); ps.execute(); ps.close(); conn.close(); 3. MySql设定档(my.cnf) [client] #password = your_password port = 3306 socket = /var/lib/mysql/mysql.sock # The MySQL server [mysqld] long_query_time = 10 log-slow-queries = /mysql_data/mysqllog/slow_query.log #log-queries-not-using-indexes = true port = 3306 socket = /var/lib/mysql/mysql.sock skip-locking key_buffer_size = 32M max_allowed_packet = 32M table_open_cache = 2048 sort_buffer_size = 16M read_buffer_size = 8M read_rnd_buffer_size = 16M myisam_sort_buffer_size = 128M thread_cache_size = 32 query_cache_size = 128M tmp_table_size = 128M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 16 innodb_thread_concurrency=16 #skip-networking init-connect='insert into accesslog.accesslog(conn_id,time,localname,matchname) values(connection_id(),now(),user(),current_user())' # Replication Master Server (default) # binary logging is required for replication #log-bin=mysql-bin log-bin=/mysql_data/mysqllog/mysql-bin.log expire_logs_days = 60 datadir=/mysql_data/mysqldata/ # required unique id between 1 and 2^32 - 1 # defaults to 1 if master-host is not set # but will not function as a master if omitted server-id = 1 # Replication Slave (comment out master section to use this) # # To configure this host as a replication slave, you can choose between # two methods : # # 1) Use the CHANGE MASTER TO command (fully described in our manual) - # the syntax is: # # CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>, # MASTER_USER=<user>, MASTER_PASSWORD=<password> ; # # where you replace <host>, <user>, <password> by quoted strings and # <port> by the master's port number (3306 by default). # # Example: # # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306, # MASTER_USER='joe', MASTER_PASSWORD='secret'; # # OR # # 2) Set the variables below. However, in case you choose this method, then # start replication for the first time (even unsuccessfully, for example # if you mistyped the password in master-password and the slave fails to # connect), the slave will create a master.info file, and any later # change in this file to the variables' values below will be ignored and # overridden by the content of the master.info file, unless you shutdown # the slave server, delete master.info and restart the slaver server. # For that reason, you may want to leave the lines below untouched # (commented) and instead use CHANGE MASTER TO (see above) # # required unique id between 2 and 2^32 - 1 # (and different from the master) # defaults to 2 if master-host is set # but will not function as a slave if omitted #server-id = 2 # # The replication master for this slave - required #master-host = <hostname> # # The username the slave will use for authentication when connecting # to the master - required #master-user = <username> # # The password the slave will authenticate with when connecting to # the master - required #master-password = <password> # # The port the master is listening on. # optional - defaults to 3306 #master-port = <port> # # binary logging - not required for slaves, but recommended #log-bin=mysql-bin # # binary logging format - mixed recommended binlog_format=mixed # Point the following paths to different dedicated disks #tmpdir = /tmp/ #log-update = /path-to-dedicated-directory/hostname # Uncomment the following if you are using InnoDB tables innodb_data_home_dir = /mysql_data/mysqldata/ innodb_data_file_path = ibdata1:10M:autoextend #innodb_log_group_home_dir = /mysql_data/mysqllog/ # You can set .._buffer_pool_size up to 50 - 80 % # of RAM but beware of setting memory usage too high innodb_buffer_pool_size = 1G #innodb_additional_mem_pool_size = 64M # Set .._log_file_size to 25 % of buffer pool size #innodb_log_file_size = 100M #innodb_log_buffer_size = 8M #innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 120 #modify by ryan optimization and tuning wait_timeout=1000 interactive_timeout=120 join_buffer_size=16M query_cache_limit=8M table_cache=128 [mysqldump] quick max_allowed_packet = 32M [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout 4. 补充说明 本来以为是不是因为每次写入资料之後, 连线没有关闭而导致Timeout, 但後来每笔资料都重新建立新连线, 一写完资料就立刻关闭连线释放资源, 但问题依然一样, 之前也google了许多类似案例, 也都没有任何帮助。 而且如果是timeout造成的问题, 应该是因为网路很慢导致断线中断, 但我的情况是才刚要建立新连线就发生无法连线, 怎麽想都应该不是timeout造成的问题, 网路上一堆文章都说是timeout设定错误, 拜托各位神人解惑, 感激不尽!! 5. 参考资料 http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.79.199.63
1F:推 PsMonkey:==.=== 直觉是套 connection pool 就可以解决 01/04 11:08
2F:→ cyclone350:你是不是同时有很多 Object 对这个 connection 写资料? 01/05 01:35
3F:→ tnsshnews:程式是一个thread, 会一直执行Listen封包 01/05 10:52
4F:→ tnsshnews:只要有新封包, 就会建立新连线, 写入之後马上关闭 01/05 10:54
5F:推 PsMonkey:马上关闭没有那麽「马上」阿 01/05 11:19
6F:推 phstudy:楼上正解。不改程式的做法,就是把max_connextions开超大 01/05 18:34
7F:→ phstudy:但治标不治本XD 01/05 18:35
8F:→ tnsshnews:所以要使用connection pool? 01/06 15:35
9F:→ PsMonkey:一开始就跟你讲了阿...... [摊手] 01/06 19:42
10F:→ qrtt1:mysql 的设定看起来很『预设』,资料长大了会後悔的xd 01/06 22:14
11F:→ swpoker:WINDOWS的TCP/IP的连线不会那麽快释放~所以用POOL比较好 01/07 09:53
12F:→ bestlong:可以只用一个connection,程式启动时建立 01/09 16:10
13F:→ bestlong:这样封包来只需要执行写入就可以了,若顶不住就得上pool了 01/09 16:14







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP