Do each connection in connection pool of an JDBC driver open the same socket port on client side in order to connect to DB server? If this is the case, it means that multiple thread access the same port simultaneously, right? Thanks in advance.
-
I do not think it is possible to open the same open socket. It will fail. Also what would the advantage be? One Server socket, Many client sockets.– Scary WombatSep 6, 2013 at 8:52
-
Your question does not really make sense... What problem are you really facing? What is the underlying cause of asking this question?– DariuszSep 6, 2013 at 11:47
-
I need to clear my concerns when working with jdbc. No real problem. Is it OK?– bnguyen82Sep 6, 2013 at 13:06
3 Answers
That's not true. Fundamentally, an instance of client socket will use only one port and it is not shared. From this article,
each requested client socket is assigned a unique port number whereas the server port number is always the same. If any of these numbers is different, the socket is different
So to answer your question, each JDBC connection uses a different client socket to connect to server.
-
That means I'm wrong to think that each connection uses the same port to connect to a server. Btw, I see that socket server listens to the one port and accepts many client requests. Sep 6, 2013 at 13:09
Do each connection in connection pool of an JDBC driver open the same socket port on client side in order to connect to DB server?
No. This is impossible.
If this is the case
It isn't.
it means that multiple thread access the same port simultaneously, right?
Wrong, and it wouldn't mean anything anyway, as connections are distinct.
Your question doesn't make any sense. What's the real problem you're trying to solve?
First of all you are asking about a general behavior where no such generality exists. It’s up to the specific driver to decide how to setup. It does not even have to create network sockets at all.
Second you can answer your question about port sharing without thinking about JDBC but only networking. It’s not possible to open a port a second time. That would not make any sense as the port is the way to differentiate between the clients (receiver of a packet) on the same machine. Two instances within the same JDBC could share a port but not open a port again. Sharing a port would require implementing another protocol to identify the associated connection for each data packet. But that’s exactly what ports are for.
Third whether multiple threads access the same port simultaneously, again, is up to the specific driver as it is the driver deciding whether to use multiple threads or not. Nobody says that the thread accessing a JDBC connection has to be the same thread accessing the network port (in case the driver does use network connections).
-
The port is not 'the way to distinguish between clients'. A TCP connexion is a 5-tuple. The client port is only one of those five elements. 'Share a port but not open a port again' is meaningless. It isn't up to the driver to decide about threads, it is up to the application. -1 Sep 6, 2013 at 9:45
-
I added a clarification. The question already gave the context of a single machine assuming the same IP address. Of course the port is the only differentiation between clients (i.e. JDBC connection instances) on the same machine. Respect the context. And of course the JDBC driver will decide which threads access the TCP port if there is one. The application has no direct access to the TCP port.– HolgerSep 6, 2013 at 10:07
-
The words 'single machine' do not appear in the question. The JDBC driver doesn't care about threads. It's just an API. If you have evidence to the contrary please produce it to support your assertion. Sep 6, 2013 at 10:16
-
The question was about two connection instances using the same port. How should they ever do that if they weren’t on the same machine? Further you are mixing up API and implementation. JDBC is an API. The JDBC driver is an implementation. And this implementation is responsible for being either thread-safe or not and for using it’s own threads or just the caller threads. And it’s the JDBC driver which might maintain TCP connections or not.– HolgerSep 6, 2013 at 10:34