I'm trying to read the intialization parameters from web.xml and try to connect to the database in my servlet init(ServeltConfig config) method but my connection object is pointing to null in my doPost method:
But when i'm ttrying to connect to DB without reading from web.xml its working fien.You could also see the commented code in that. Any help in this regard is greatly appreciable.
[Edit - code tags added by Dave] [ November 29, 2005: Message edited by: David O'Meara ]
Rajesh <br /> <br />SCJP1.4 SCWCD1.4 SCBCD 1.3 ,SCDJWS(Preparing..)<br /> <br />There is no free will.It is the phenomenon bound by cause and effect.But there is something behind will which is free---Swami Vivekananda...
Sathya Srinivas
Greenhorn
Joined: Nov 29, 2005
Posts: 1
posted
0
Hi change the following code <param-name>USER</param-name> <param-value>""</param-value> </init-param> <init-param> <param-name>PASSWD</param-name> <param-value>"" </param-value>
as follows <param-name>USER</param-name> <param-value></param-value> </init-param> <init-param> <param-name>PASSWD</param-name> <param-value></param-value>
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
posted
0
Getting DB connection or Database operations should not be done in servlet. Servlet, on the other hand, should only be used as a controller.
dema rogatkin
Ranch Hand
Joined: Oct 09, 2002
Posts: 294
posted
0
If a servlet uses database connection, then init method is the best place where to obtain it.
Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Are you using MySQL? Why are you using the JDBC-ODBC bridge driver? It's much better to use the MySQL JDBC driver.
The JDBC-ODBC bridge driver is only meant for educational purposes, it is not suited for real world applications. For more info see: JDBC-ODBC Bridge. [ November 30, 2005: Message edited by: Jesper de Jong ]
Originally posted by dema rogatkin: If a servlet uses database connection, then init method is the best place where to obtain it.
This is generally considered a bad idea. If you obtain your connection in the init method then all requests will have to share it. The connection will become a bottleneck at best and a threading nightmare at worst. There is also the danger of the connection timing out before the servlet is destroyed.
Most conventional wisdom, these days, instructs us to use connection pooling for anything but the most trivial apps.
Jesper, I see what you meant. Just curious who were your educators, or those just yours ideas? No need to answer, I simple trying to understand who is behind modern software engineering.
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
posted
0
Originally posted by dema rogatkin: As you can guess I disagree with you however I agree do not go in details to reduce bandwidth.
As you have already got lots of explaination about this idea and why it is considered bad. I didn't go for reasoning because the thread topic is not about where to get connection or whether its a good idea to get connection in init() method. My intend was not to hijack the main thread rather than saving band-width. But Ben, and other did well. Because it was really necessary to tell you about your idea. Otherwise you might be screwed in future.
As you have already got lots of explaination about this idea and why it is considered bad. I didn't go for reasoning because the thread topic is not about where to get connection or whether its a good idea to get connection in init() method. My intend was not to hijack the main thread rather than saving band-width. But Ben, and other did well. Because it was really necessary to tell you about your idea. Otherwise you might be screwed in future.
Thanks.
[ December 01, 2005: Message edited by: Adeel Ansari ]
Bad thing is that younger developers reading explanation having no any conjuction with real things will gain wrong experience and certainly will be screwed in future. So I'd suggext to stop confuse people.
But we have a strong tradition at the JavaRanch of encouraging people to use stable solutions from the beginning, rather than teaching lessons they will have to unlearn in the future.
If this topic is closed we should let it rest, otherwise I will have to delete the last half dozen posts or so. People asking questions have the right have their questions answered without side conversations.
Comments on Connection Pooling, please.
Dave
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
posted
0
Originally posted by dema rogatkin: Bad thing is that younger developers reading explanation having no any conjuction with real things will gain wrong experience and certainly will be screwed in future. So I'd suggext to stop confuse people.
Actually, I am a professional web app developer. I have built quite a no. of web apps that are flying in the real world. It was not just my guess. I knew the reasons and could provide you all in a good way.
My intend was not to confuse anybody here, as I already said my intention. I have noticed that just because of this explaination, David thought that this thread is about connection pooling, however it is not.
I have learnt many things here at Ranch. Some of those are:
- Be nice - Never steer the thread in other direction, because it would irritate the O.P.(Original Poster) - Dig the person to whom you would like to learn, but in a nicer manner.
I get many emails, PMs from the folks here at Ranch ragarding their problems. I don't mind those. I can explain the things other than topic if O.P. wants me to do that. Hence, it was just a suggestion. If O.P. or someone else has some confusions wiht it and need more explainations then he/she can always PM me, email me, or start a new topic. Moreover, I have already admitted that here I should have given the explaination, as some folks did, in order to provide a detailed reason of my view, or you can say my guess.
Originally posted by dema rogatkin: Jesper, I see what you meant. Just curious who were your educators, or those just yours ideas? No need to answer, I simple trying to understand who is behind modern software engineering.
I am a professional Java / J2EE developer and I've been writing web applications for a number of different companies for the last six years. I learned most of it by doing Java and J2EE courses, reading books, from colleagues, learning and experimenting in my own time and ofcourse experience in projects for my clients.
Using something like a connection pool isn't my own idea or invention. It's a well-known technique which is used in almost any serious real-world web application. You can find it in any good book about developing web applications.