“A Connection”只是a Socket 的详细信息,以及额外的详细信息(如用户名、密码等)。 每个连接都有不同的套接字连接。
例如:
连接 1:
Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]
连接 2:
Socket[addr=localhost/127.0.0.1,port=1030,localport=51246]
我在单个 JVM 进程中创建了两个连接,以演示服务器如何知道要在哪个 Socket 中发送回复。 A socket,如果我根据 UNIX 定义是用于进程间通信的special file:
srwxr-xr-x. 1 root root 0 Mar 3 19:30 /tmp/somesocket
创建套接字时(即创建此特殊套接字文件时;how to create a socket? 和 this)操作系统会创建一个指向该文件的文件描述符。服务器通过以下属性区分 Socket:
Ref.
{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}
PROTOCOL:我以postgres为例,postgres驱动中的socket连接是用SocksSocketImpl完成的,即TCP socket implementation (RFC 1928)
返回我创建的两个连接,如果您仔细查看 localport,这两个连接是不同的,因此服务器清楚地知道它必须在哪里发回回复。
现在您可以在操作系统中打开的文件(或文件描述符)的数量受到限制,因此建议不要让您的连接悬空(称为连接泄漏)
它是否在每次连接时都加载数据库架构?
回答:不,是 ResultSet 负责处理它。
当数据库架构改变时连接会发生什么
Answer: 连接和数据库架构是两个不同的东西。 Connection 只是定义了如何与另一个进程通信。数据库模式是应用程序和数据库之间的契约,应用程序可能会抛出契约被破坏的错误,或者它可能会忽略它。
如果你有兴趣深入挖掘,你应该给一个连接对象添加一个断点,下面是它的样子(见FileDescriptor)
connection = {Jdbc4Connection@777}
args = {String[0]@776}
connection = {Jdbc4Connection@777}
_clientInfo = null
rsHoldability = 2
savepointId = 0
logger = {Logger@778}
creatingURL = "dbc:postgresql://localhost:1030/postgres"
value = {char[40]@795}
hash = 0
openStackTrace = null
protoConnection = {ProtocolConnectionImpl@780}
serverVersion = "10.7"
cancelPid = 19672
cancelKey = 1633313435
standardConformingStrings = true
transactionState = 0
warnings = null
closed = false
notifications = {ArrayList@796} size = 0
pgStream = {PGStream@797}
host = "localhost"
port = 1030
_int4buf = {byte[4]@802}
_int2buf = {byte[2]@803}
connection = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
created = true
bound = true
connected = true
closed = false
closeLock = {Object@811}
shutIn = false
shutOut = false
impl = {SocksSocketImpl@812} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
server = null
serverPort = 1080
external_address = null
useV4 = false
cmdsock = null
cmdIn = null
cmdOut = null
applicationSetProxy = false
impl = {DualStackPlainSocketImpl@814} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
exclusiveBind = true
isReuseAddress = false
timeout = 0
trafficClass = 0
shut_rd = false
shut_wr = false
socketInputStream = {SocketInputStream@819}
eof = false
impl = {DualStackPlainSocketImpl@814} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
temp = null
socket = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
created = true
bound = true
connected = true
closed = false
closeLock = {Object@811}
shutIn = false
shutOut = false
impl = {SocksSocketImpl@812} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
server = null
serverPort = 1080
external_address = null
useV4 = false
cmdsock = null
cmdIn = null
cmdOut = null
applicationSetProxy = false
impl = {DualStackPlainSocketImpl@814} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
timeout = 0
trafficClass = 0
shut_rd = false
shut_wr = false
socketInputStream = null
socketOutputStream = null
fdUseCount = 0
fdLock = {Object@815}
closePending = false
CONNECTION_NOT_RESET = 0
CONNECTION_RESET_PENDING = 1
CONNECTION_RESET = 2
resetState = 0
resetLock = {Object@816}
stream = false
socket = null
serverSocket = null
fd = {FileDescriptor@817}
address = null
port = 0
localport = 0
oldImpl = false
closing = false
fd = {FileDescriptor@817}
fd = 1260
handle = -1
parent = {SocketInputStream@819}
eof = false
impl = {DualStackPlainSocketImpl@814} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
temp = null
socket = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
closing = false
fd = {FileDescriptor@817}
fd = 1260
handle = -1
parent = {SocketInputStream@819}
eof = false
impl = {DualStackPlainSocketImpl@814} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
exclusiveBind = true
isReuseAddress = false
timeout = 0
trafficClass = 0
shut_rd = false
shut_wr = false
socketInputStream = {SocketInputStream@819}
socketOutputStream = {SocketOutputStream@820}
fdUseCount = 0
fdLock = {Object@821}
closePending = false
CONNECTION_NOT_RESET = 0
CONNECTION_RESET_PENDING = 1
CONNECTION_RESET = 2
resetState = 0
resetLock = {Object@822}
stream = true
socket = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
serverSocket = null
fd = {FileDescriptor@817}
address = {Inet4Address@823} "localhost/127.0.0.1"
port = 1030
localport = 51099
temp = null
socket = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
closing = false
fd = {FileDescriptor@817}
path = null
channel = null
closeLock = {Object@826}
closed = false
otherParents = {ArrayList@833} size = 2
closed = false
path = null
channel = null
closeLock = {Object@826}
closed = false
otherParents = {ArrayList@833} size = 2
closed = false
path = null
channel = null
closeLock = {Object@826}
closed = false
socketOutputStream = {SocketOutputStream@820}
impl = {DualStackPlainSocketImpl@814} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
temp = {byte[1]@843}
socket = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
closing = false
fd = {FileDescriptor@817}
append = false
channel = null
path = null
closeLock = {Object@844}
closed = false
fdUseCount = 0
fdLock = {Object@821}
closePending = false
CONNECTION_NOT_RESET = 0
CONNECTION_RESET_PENDING = 1
CONNECTION_RESET = 2
resetState = 0
resetLock = {Object@822}
stream = true
socket = {Socket@804} "Socket[addr=localhost/127.0.0.1,port=1030,localport=51099]"
serverSocket = null
fd = {FileDescriptor@817}
address = {Inet4Address@823} "localhost/127.0.0.1"
port = 1030
localport = 51099
timeout = 0
trafficClass = 0
shut_rd = false
shut_wr = false
socketInputStream = null
socketOutputStream = null
fdUseCount = 0
fdLock = {Object@815}
closePending = false
CONNECTION_NOT_RESET = 0
CONNECTION_RESET_PENDING = 1
CONNECTION_RESET = 2
resetState = 0
resetLock = {Object@816}
stream = false
socket = null
serverSocket = null
fd = {FileDescriptor@817}
address = null
port = 0
localport = 0
oldImpl = false
pg_input = {VisibleBufferedInputStream@805}
pg_output = {BufferedOutputStream@806}
streamBuffer = null
encoding = {Encoding@807} "UTF-8"
encodingWriter = {OutputStreamWriter@808}
user = "postgres"
database = "postgres"
executor = {QueryExecutorImpl@800}
logger = {Logger@778}
compatible = "9.0"
dbVersionNumber = "10.7"
commitQuery = {SimpleQuery@783} "COMMIT"
rollbackQuery = {SimpleQuery@784} "ROLLBACK"
_typeCache = {TypeInfoCache@785}
prepareThreshold = 5
autoCommit = true
readOnly = false
bindStringAsVarchar = true
firstWarning = null
timestampUtils = {TimestampUtils@786}
typemap = null
fastpath = null
largeobject = null
metadata = null
copyManager = null