• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

saving to ServletContext

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am currently building a web app using tomcat and the MVC design pattern - controllerservlet, beans and jsp's.
I get a connection to the database in my init method in the servlet and then I would like to save it to the servlet context for access by my beans (that do the actual sql queries).
However, I cannot seem to get it to work. I am using getServletContext().setAttribute("connection",conn); to try to save it to the context.
I know my connection code works, and I know my beans work. But when I put it all together I cannot save the connection to the Context. This will be much more efficent than getting every bean to connect/disconnect each time. And there is no need to a ConnectionPool as the system will be used only once a day by one person.
Am I going about it the right way ?? I cannot think of another way to approach it........
thanks in advance Chris
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This won't work the way you planned to do it, because Session data has to be serializable in a way and a JDBC-Connection is not.
(See this JGuru-FAQ entry for further information about this).
What you can do is: write a static getConnection() method in your Servlet and let this Method do the work for you. On request this Method returns the static connection (if it is not null because it expired) or creates a new one.
You see... even if you don't plan to use connection pooling (at least not now...) you have to think a litte about the connection handling if you don't want to create redundancy in your code.
So I would recommend you just create a small connection pool. It's quite easy with this code and you won't have any more trouble.
enjoy it
Hartmut
 
peter piper
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That for the reply, it was the problem with serialization that i hadn't considered. I will try and use a small pool option - thought I use JNDI lookups I am sure I can work it out !!
thanks again.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic