File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Datasource question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Datasource question" Watch "Datasource question" New topic

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

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
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?
I agree. Here's the link:
subject: Datasource question
It's not a secret anymore!