aspose file tools*
The moose likes JDBC and the fly likes Query in : JNDI look up of DataSource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Query in : JNDI look up of DataSource " Watch "Query in : JNDI look up of DataSource " New topic
Author

Query in : JNDI look up of DataSource

Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Hi all,
I am using Tomcat 5.5 /JDK 1.5 ,I created a database in MySQL 4.2 called "javatest" with a Jndi look up "jdbc/TestDB".
To use the dataSource I modified the server.xml present in Catalina_home/conf directory ,and added the below context element inside the host element present in server.xml :

<Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true">

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</Context>

The code is working fine with this inclusion .
My query is first is it the right way to access DataSource in a web application,what changes could be made to improve the code.

Second ,if I add the code:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
in my web.xml ,there are again no errors and the code is still running fine.

Hence my query is that what is the significance of the code above in web.xml ,should we include it in web.xml
even when the thing works fine without it .



SCJP5 ,SCWCD5
dariyoosh za
Greenhorn

Joined: Apr 12, 2009
Posts: 20
Hello there,

Unlike what is widely said on the web that you should modify the server.xml file, there is absolutely no need to modify this file.
I have created DataSource with JNDI by using both Tomcat 5.5 and Tomcat 6.0.18. You just work with your Web application
Context file (META-INF/context.xml) and your project web.xml (WEB-INF/web.xml)

Here is a thread (you just need to look the last two comments) explaining how to proceed. the database used is Oracle, well,
that's not a problem because you just need to modify type, url, driverClass and factory fields.

http://www.coderanch.com/t/441677/JDBC/java/about-creating-Connection-pool-uses


Regards,
Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Dariyoosh,
Got it working fine with the <context> element in /META-INF/context.xml .the code looked like :
<Context path="/JNDI" docBase="JNDI" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/JNDI" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" />
</Context>
much better than changing server.xml or conf/context.xml.Thanks for the tip mate .Officially too
Tomcat recommends placing the <context>element in there here for tomcats view .
But still one part is not clear to me that is what is the use of :
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/JNDI</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
inside web.xml ,the application is working fine without it and with it.
dariyoosh za
Greenhorn

Joined: Apr 12, 2009
Posts: 20
Abhijit Rai wrote:
...
But still one part is not clear to me that is what is the use of :
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/JNDI</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
inside web.xml ,the application is working fine without it and with it.


There are many reasons that according to the specification, one should also specify the <resource-ref> in web.xml. One of the
most important reasons is the scope of the DataSource being used.



You can read more about that in the following thread

http://www.coderanch.com/forums/posts/watch/0/159428

Actually, I also managed to run my application without <resource-ref> and it worked. However according to both Sun's servlet
specification and Apache tomcat tutorials and also all the tutorials and HOWTO that I found on the web, it is recommended to
edit properly the web.xml file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query in : JNDI look up of DataSource