Storing JDBC Connection in HttpSession

I recently inherited some code in which I found that a JDBC connection is initialized in a filter and an HttpSession is added for each user. This connection is then reused in various parts of the web application for the user. It immediately seemed to me the smell of code. I would like to return to the developer who wrote it and explained why he should not do this ... But maybe I'm not so confident in myself ...

Besides using extra storage space and potentially limiting available database connections, are there other reasons why you won’t keep the JDBC connection in the session?

+5
source share
3 answers

You mentioned the obvious: one user does not scale to one database connection. Your users should be completely separated from access to the data model. Here are a few problems you will encounter.

  • What happens if the connection becomes obsolete? This user will not be able to use anything without connecting to the database, so they may have to log out and then return to get a new connection. crazy.
  • JDBC communications are not thread safe. If the user decides to launch a couple of things at once, all hell will break.
+5
source

JDBC , HttpSession JDBC . . .

, , .

JDBC , , , .

, , , . ( , ).

+2

, , , , -. , ; , "" .

, 2- , . , , . ...

" " . Tomcat , -. , . .

Another advantage of the connection pool is that after the pool is “warmed up”, that is, the number of connections is used and initialized, even the “first requests for a user session” quickly receive a connection. Thus, the overall throughput will be improved compared to the current situation.

+1
source

All Articles