Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Datasource question

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
Masoud Kalali
Author
Ranch Hand
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic