• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Datasource question

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 531
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic