Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Object pointing to Null

 
Rajesh Vijaya
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

here is my two files:

1. web.xml

<servlet>

<servlet-name>regProcessServlet</servlet-name>
<servlet-class>RegProcessServlet</servlet-class>
<init-param>
<param-name>DRIVER</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</init-param>
<init-param>
<param-name>URL</param-name>
<param-value>jdbc dbc:mySQLDSN</param-value>
</init-param>
<init-param>
<param-name>USER</param-name>
<param-value>""</param-value>
</init-param>
<init-param>
<param-name>PASSWD</param-name>
<param-value>"" </param-value>
</init-param>
</servlet>

2.RegProcessServlet



[Edit - code tags added by Dave]
[ November 29, 2005: Message edited by: David O'Meara ]
 
Sathya Srinivas
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a servlet uses database connection, then init method is the best place where to obtain it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dema rogatkin:
If a servlet uses database connection, then init method is the best place where to obtain it.


Its not like that. I am not going into details otherwise it would become off-topic.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We normally like people to justify their opinions with some backing argument, but since this is more of a vote, I also vote for connection pools.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, not using container-managed connection pooling in a web app that uses a database is nothing short of madness.
 
dema rogatkin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adeel Ansari:


Its not like that. I am not going into details otherwise it would become off-topic.
As you can guess I disagree with you however I agree do not go in details to reduce bandwidth.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dema rogatkin:
As you can guess I disagree with you however I agree do not go in details to reduce bandwidth.


dema, you are wrong. Opening a database connection in the init() method of a servlet and then using it in the servlet is really a bad idea.

Have a look at the following thread if you want a number of reasons why:
http://www.coderanch.com/t/361654/Servlets/java/database-connection-init-servlet
 
dema rogatkin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.
[ December 01, 2005: Message edited by: Adeel Ansari ]
 
dema rogatkin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adeel Ansari:


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.

Thanks,
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.
[ December 02, 2005: Message edited by: Adeel Ansari ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic