I know that javax.servlet.ServletContextListener is called when the application started(after deployment)
My question is :when i have huge application which use it more then 100 users at the same time,I can make the connection of DataBase in the servletContextListener or
it will be better if I make it when the user login and put it in session scope?which improve the performance ?
Please answer me
Thanks and best regards
You should have one and only one connection to the database and funnel your application's data needs through that pipe. See Separation of Concerns. Your users should log into your application but you should not have one database connection per user. Neither should you have one database connection per session.
As Roger says, both of your options are bad choices. The typical design is to have a connection pool; when your application needs to access data from the database it does this:
1. Get a connection from the pool
2. Use that connection to get the data your application requires
3. Return the connection to the pool
Your code should not keep the connection any longer than necessary.
Start with a connection pool of ONE connection. Make that work well. Then increase the number of connections in the pool as needed. For 100 users, I don't see you needing more than one, but...