This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a problem with my database web application... The parent servlet has a database Connection object and a db statment object which are member variables. They are instantiated in the servlets init() method. What I wanted was for all the child servlets to use these connection objects in synchronized methods. But this does not seem to be happening as the 'children' are creating their own connection and statement objects. Is there a way to prevent the child servlets from doing this? Many thanks
Firstly, you don't want to do this because the Connection object would not be thread safe. If the single Connection is shared by all servlets that extend the base servlet, their queries will overlap and get... confused. Otherwise you would be able to share them by making them static. I find that extending servlets doesn't really work. There are reasons to do it and ways to do it, but in practice it would be better to look at 'action' style classes like the patterns that turn up in the Struts framework.