This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Datasource question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Datasource question" Watch "Datasource question" New topic
Author

Datasource question

Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Sorry, I am unable to make a short descriptive title.

Well, i have made datasource in tomcat server.xml file. But now i am confuse how to get the connection ...

is it the correct syntax


I am unable to get connnection using this.

Now second question, if i got the connection what will be method that i release it. i.e suppose if a class need a connection for DB, i'll call the above CORRECTED code, the class will do the manipulation and then what? should i have to explicitly release the connection or not?


The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Look at the JavaDocs for java.sql.DataSource. You just need to call the getConnection() method. Assuming your DataSource is configured correctly that will get you your connection.


should i have to explicitly release the connection or not?

Yes. Once you are finished with it close the connection in a finally block.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

Please check that whether you can see your defined datasource in tomcat admin console or not.
It is better to use admin console for configuring server , not to add them manually.
your code looks correct and it should return a connection from a datasource with jdbc/DB name in tomcat JNDI .

to make sure that you have configured the correct datasource check it in tomcat admin console.


Masoud Kalali
Software Engineer - My Weblog - GlassFish Security
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
thanks Paul

One more question.
How is the working of close method in this case? i.e will the connection is returned back to the tomcat container?

calling the Datasource.getConnection() will return a connection object from the Container of the tomcat? does this mean on server startup, the tomcat creates a pool of connections and we reuse these connection throughout our code?
 
jQuery in Action, 2nd edition
 
subject: Datasource question
 
Similar Threads
Singleton Pattern
Connection Error WSAD to SQL Server
whether connection pool will available at the time of starting the server
MySQL, JNDI refactoring
Problem connecting to DB after DB restart