What I am doing currently is:
I have a servlet and I am writing all the DB related operations in one class named "DBOperations"
Servlet will create the object of the DBOperations to call the methods in it and DBOperations is not a singleton
I declared the Connection object as an instance veriable but not static
declared instance veriable as "private Connection dbConnection"
instantiating that veriable in the constructor
and using the connection in all the methods used in the DBOperations with out creating the connection in each and every method, my requirement has multiple db operations per request so my intention was to use only one connection per request instead of getting connection for each db operation
Is there any problem if I declare the connection as an instance veriable.
Also Wanted to know will the servlet refered classes also should be built as a thread safe.
I am not creating the instance veriable in the servlet directly.
It is created in one of the class whose object is created by the servlet in get or post methods so that class object is also not an instance veriable in servlet.
If in the above case also if the servlet shares the same DBOperations object(Even though the object is not refered as an instance veriable in servlet) to all the requests? In that case do we need to design the classes refered by the servlet in a thread safe manner as well