如何在Java中设置oracle的CONNECTION_PROPERTY_NET_KEEPALIVE属性

bogh5gae  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(190)

当连接空闲了一段时间后,我的java应用程序中的连接突然下降。

IO Error: Connection reset; nested exception is java.sql.SQLRecoverableException

发现如下:https://docs.oracle.com/en/database/oracle/oracle-database/21/jajdb/oracle/jdbc/OracleConnection.html
如何在Java中设置保活属性?

0lvr5msh

0lvr5msh1#

这可能对您“有用”,但不幸的是Linux内核默认值并不实用。

# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200

发送第一个穿刺针之前的延迟- 2小时。注:CISCO阿萨的“connection timeout idle”的默认值仅为1小时!!!因此,在发送第一个keepalive探测之前,连接将被终止。不幸的是,您需要root用户才能更改此内核参数。Java目前还不支持每个TCP连接的ioctl命令来设置keepalive参数。
当您更改此内核参数时,您应该看到类似以下内容:

# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
Interval between probes.

# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9
The number of "lost" probes indicating that the connection is dead.

在Oracle端,默认情况下Oracle TCP Keepalive对于Oracle连接不活动。可以通过设置连接字符串中DESCRIPTION元素下的(ENABLE=BROKEN)或使用-Doracle.net.keepAlive=true来启用它
您可以使用tcpdump Connect验证您的设置:

(DESCRIPTION=
    (ENABLE=BROKEN)
    (TRANSPORT_CONNECT_TIMEOUT=90)
    (RETRY_COUNT=20)
    (FAILOVER=ON)
    (ADDRESS=(PROTOCOL=TCP)(HOST=rac-cluster-scan)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ACTEST))
)

[~]# nslookup rac-cluster-scan 
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   rac-cluster-scan
Address: 192.168.8.209

[~]# ip route get 192.168.8.209
192.168.8.209 dev ens224 src 192.168.8.200
    cache

[~]# tcpdump -pni ens224 -v "tcp port 1521 and ( tcp[tcpflags] & tcp-ack != 0 and ( (ip[2:2] - ((ip[0]&0xf)<<2) ) - ((tcp[12]&0xf0)>>2) ) == 0 )"
tcpdump: listening on ens224, link-type EN10MB (Ethernet), capture size 262144 bytes
14:56:49.994675 IP (tos 0x0, ttl 64, id 12096, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.8.200.37196 > 192.168.8.212.ncube-lm: Flags [.], cksum 0x9313 (incorrect -> 0x4b94), ack 2263270845, win 359, options [nop,nop,TS val 78140944 ecr 7133273], length 0
14:56:49.994829 IP (tos 0x0, ttl 64, id 55327, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.8.212.ncube-lm > 192.168.8.200.37196: Flags [.], cksum 0xadcf (correct), ack 1, win 207, options [nop,nop,TS val 7138281 ecr 78110940], length 0
14:56:55.002668 IP (tos 0x0, ttl 64, id 12097, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.8.200.37196 > 192.168.8.212.ncube-lm: Flags [.], cksum 0x9313 (incorrect -> 0x2474), ack 1, win 359, options [nop,nop,TS val 78145952 ecr 7138281], length 0
14:56:55.002809 IP (tos 0x0, ttl 64, id 55328, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.8.212.ncube-lm > 192.168.8.200.37196: Flags [.], cksum 0x9a3f (correct), ack 1, win 207, options [nop,nop,TS val 7143289 ecr 78110940], length 0
14:57:00.010680 IP (tos 0x0, ttl 64, id 12098, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.8.200.37196 > 192.168.8.212.ncube-lm: Flags [.], cksum 0x9313 (incorrect -> 0xfd53), ack 1, win 359, options [nop,nop,TS val 78150960 ecr 7143289], length 0
14:57:00.011296 IP (tos 0x0, ttl 64, id 55329, offset 0, flags [DF], proto TCP (6), length 52)
    192.168.8.212.ncube-lm > 192.168.8.200.37196: Flags [.], cksum 0x86af (correct), ack 1, win 207, options [nop,nop,TS val 7148297 ecr 78110940], length 0

6 packets captured
6 packets received by filter
0 packets dropped by kernel

当您在内核级别更改keepalive参数时,此tcpdump过滤器应显示按请求的时间间隔发送的探测。
PS:TCP keepalive发送仅由Linux内核处理。Oracle的客户端软件只能在打开连接时打开此选项。

相关问题