This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Connection to DB using JNDI in Servlets Init Method Issue  RSS feed

 
Sheikkadar Mohamedsahib
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Team,

In my application, From my JSP Iam calling servlets on POST request. In my servlets using JNDI Datasource to create a database connection (SQL) to execute INSERT query. Inside init method, I have created this connection. Problem now is that for every refresh of jsp page, init method is not executing. I knew it will be called only once. How to handle it ?



servlets-sourcecode.JPG
[Thumbnail for servlets-sourcecode.JPG]
Servlets Sourcecode
JSP-sourcecode.JPG
[Thumbnail for JSP-sourcecode.JPG]
JSP sourcecode
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not post screenshots of your code. You can copy paste your code (please do UseCodeTags)
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope your con object is a static one for connection. So, you should be good to use it in doPost method to execute the statements.

Why do you want to create a new connection on each request? If you want to do that, then you can put that logic in your doPost() method but I don't see any use of doing it for each and every request.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote:I hope your con object is a static one for connection. So, you should be good to use it in doPost method to execute the statements.

Why do you want to create a new connection on each request? If you want to do that, then you can put that logic in your doPost() method but I don't see any use of doing it for each and every request.


Actually you should not have a static connection object.
Also, don't put any instance variables in servlets (unless they are stateless proxies).

When using connections in a container, get the connection from a datasource obtained using JNDI lookup, use the connection and close it immediately after. Every interaction with the database must do that and never try to reuse a connection used by a previous task. The reasons are
1.) It's not wasteful of resources as you might think because the pooling is provided by the container so when you close a connection it's actually just returned to the pool.
2.) Leaving connections open for reuse by next tasks results in some connections never being closed because the next tasks are not always guaranteed to run.
3.) Most databases have timeouts on connections so if you keep a connection open for too long it will timeout and the next task will just fail anyway because it has a 'stale' connection.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!