Originally posted by saikrishna cinux:
ya i am using class variable for creating the connection and i am creating the connection in service method
I would read Charles Lyon's (as well as all the other's) post very carefully and use those tips as a starting point toward cleaning up this code.
Once you've done that, your business and database logic should be a lot easier to debug.
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by vini guru:
Why don't you create your connection in init() method;
Originally posted by dema rogatkin:
Dave,
I do not know who you are by occupation, but believe me I worked as a universtity professor for long time. So teaching is something different than you meet in your profession as a software engineer. You need to concentrate attention a student on something relevant to current topic and keep in shade other details which will be learned after. So, here is a perfect example, if he/she learns servlet, then thread safety can be other topic. On this stage of education adding synchronized is just perfect solution. Later in other courses students can learn how to get thread safety without possible compromisimg performance. Again, without sufficient information about requirements we can't recommend any thread safety solution. BTW If you are work near Oracle we can get lunch together and do more discussion.
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by Martin Simons:
The first thing I see is that session itself is also an instance variable.
If this is still the case, this should also be locally declared and passed.
It, unfortunately, does not help much to create the connection locally and
store it in the session, when the session itself is "global". Global is a
bad word, but when speaking of servlets it is a good analogy, as all
instance variables are essentially "global", at least from the perspective
of the servlet itself, meaning all users see the same one. So the problem
I see here, is that the session variable is getting redefined each time a
user accesses the page affecting all users. Don't worry that this means
that the user will get a new session each time the page is accessed. This
is not the case. The server (Tomcat) stores the session for that user, and
your variable is only using it (the getSession call retrieves the session
from the server).
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by Nikhil Goel:
My 2 cents .
1)Making doGet()/doPost() methods synchronized is a bad idea and practice . As inherently all the servlets are multithreaded and there lifecycle is controlled by container . I fully agree with those members that have defended the container indicating that problem lies with the code .
But on the contrary you need synchronization please then go for SingleThreaded Model i.e you need to implement SingleThread Interface(pls check the name form Servlet API) . This will make all the call to your servlet as synchronized .
2)After looking into the code it looks like you need to be very clear about Servlet life cycle methods . What it means is that put all the getConnection() logic in init() method and put the reference of this Connection object in a static variable so that it can be reused again on other Servlet calls . Put the connection destruction logic in destroy() method . Also individual JDBC DML Statement/PreparedStatements can be closed and resultsets closed in individual methods finally block .
3) What DB are u using in ur app . Did u googled the error . Reason why if you are using Oracle then thay have indicated the BUG in there implementation of JDBC ... Here is the link http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/readme_10201.html . Go checkout yourself . After opening the link search for "java.sql.SQLException: Closed Connection java.sql.SQLException: Fail to convert to internal representation" you will find the information ..
Hope it helps .
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by saikrishna cinux:
Mr nikhil there is no difference between implementing hte SingleThreadModel interface and synchronizing the doget method
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|