Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on creating Tomcat datasource for MySQL

 
Harry Wordsworth
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I am the same person who created the recent thread on not being able to connect to an oracle DB... https://coderanch.com/t/668860/Servlets/java/connect-oracle-DB
Thanks to the help in this forum I got that to work. However the feedback in that thread was that I was using the wrong method to connect to the DB, I was using a drivermanager (more suited for stand alone applications) and not a datasource which is more useful for web applications.
So I created the datasource for that project and it worked. Although, now I am going back to a previous project I created to connect to a MySQL database from an application hosted on a Tomcat application server. I got this to work using the drivermanager way but now I want to go back and use a tomcat datasource.

It looks like creating a datasource is different in tomcat than it is in weblogic? For the weblogic project in the previous thread I mentioned, I was able to add a datasource through the admin console but with tomcat it looks like I need to do it by editing the config files?
FYI my version numbers are Tomcat 8, and using MySQL workbench version 6.3

Now, heres my problem. I searched online and there seem to be some good tutorials out there showing how to setup a datasource in tomcat. There are not that many steps. The issue is each tutorial I found has similar BUT slightly different steps. For example, here are the 4 tutorials I found and I listed the major steps for each...As you can see all start out with the same step i.e. put the driver in tomcat lib and each ends with the same step, put code to access the datasource in your servlet but in between they don't quite agree. I wanted to come here and ask this audience which of these is correct...


http://wiki.apache.org/tomcat/UsingDataSources

* install JDBC driver in Tomcat lib folder
* create resource element in Meta-inf/context.xml
* access the datasource in your servlet

_____________________________________________

http://www.oracle.com/technetwork/articles/oem/ucp-jdbc-tomcat-355431.html

* install JDBC driver in Tomcat lib folder
* add datasource to tomcat's server.xml
* add resource ref to web app's web.xml
* access the datasource in your servlet
_____________________________________________

http://theopentutorials.com/examples/java-ee/servlet/servlet-jndi-datasource-in-tomcat/

* install JDBC driver in Tomcat lib folder
* create resource element in Meta-inf/context.xml
* add resource ref to web app's web.xml
* access the datasource in your servlet
_____________________________________________

http://www.codejava.net/servers/tomcat/configuring-jndi-datasource-for-database-connection-pooling-in-tomcat

* install JDBC driver in Tomcat lib folder
* create resource element in Meta-inf/context.xml
* add resource ref to web app's web.xml
* access the datasource in your servlet



Also, for the datasource information, where would I actually find this info. for example it lists as...
   name="jdbc/UsersDB"
    auth="Container"
    type="javax.sql.DataSource"
    maxActive="100"
    maxIdle="30"
    maxWait="10000"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/usersDB"

but where would I find this info for my MySQL install to use for the datarouce?
 
Harry Wordsworth
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All, don't worry about it. I think I got it to work. With my version of tomcat it appears the tutorial in http://wiki.apache.org/tomcat/UsingDataSources was the way to go. I am sure I will have more questions later as I learn more about this.
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously they all work.

The difference is mainly where do you configure your applications datasources. 
The answer to that (as with a lot of questions in programming) is "it depends"
There are pros and cons to each approach.

Basically the difference is
- Do you define the datasource on the server
- Do you define the datasource only for your web app

Defining it on the server means that it is available to all of your web applications.  Useful if you only want to configure it once and use it multiple times.
Defining it with your web app makes deployment of your app simpler - you can do it without disturbing all of the other apps deployed in the container.




 
Dave Tolls
Ranch Hand
Posts: 2102
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
Defining it on the server means that it is available to all of your web applications.  Useful if you only want to configure it once and use it multiple times.


You also ought to do this if you have multiple apps that use the same db as otherwise you end up running the risk of overloading the db connections.
I've seen places where there were half a dozen modules (apps) deployed that all had their own set of connections to a db.
Add a new module and they would tend to go boom as the db ran out of available connections.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic