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
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
Joined: Sep 14, 2002
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.