Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a JDBC Connection in HttpSession

I've recently inherited some code, within which I've found a JDBC connection being initialized in a filter and added the the HttpSession for each user. That connection is then reused throughout various portions of the web application for the user. This immediately stood out to me as a code smell. I'd like to go back to the developer who wrote it and explain why he shouldn't do that... But maybe I'm not so sure myself...

Besides taking up unnecessary space in memory and potentially limiting the available connections to the database, are there any other reasons why you wouldn't store a JDBC Connection in an session?

like image 251
jconlin Avatar asked Jul 15 '10 06:07

jconlin


People also ask

Is JDBC connection TCP or UDP?

JDBC Type 3 and Type 4 drivers use a network protocol to communicate to their back-ends. This usually implies a TCP/IP connection; this will either be a straight TCP/IP socket, but if the driver supports it, it can be a Secure Socket Layer (SSL) connection.

How secure is JDBC connection?

It is reasonably secure in as much as the sign on protocol is not vulnerable to network sniffing, and, it is very, very difficult to inject anything into the network traffic. However JDBC merely transports your SQL to the database and returns the resulting dataset.

Can we store and retrieve images using JDBC?

Note: You can store and retrieve only . gif or . jpeg or . png type of images using JDBC program.


1 Answers

You have mentioned the obvious, one user to one database connection is not scalable. Your users should be completely decoupled from access to the data model. Here are a couple of issues you will run into.

  • What happens if the connection becomes stale? That user will not be able to do anything useful without a database connection so they will likely have to log out and then back in to get a new connection. crazy.
  • JDBC Connections are not thread safe. If a user decides to run a couple things at once, all hell will break loose.
like image 198
krock Avatar answered Oct 19 '22 23:10

krock