Solved

JNDI DataSource unable to work !!

Posted on 2003-10-22
17
459 Views
Last Modified: 2010-04-01
Hi guys,


/usr/local/tomcat5/webapps/Advanced/WEB-INF/web.xml
===========================================================
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>mySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/BooksDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
===========================================================

/usr/local/tomcat5/conf/server.xml
===========================================================
.............

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
suffix=".txt" timestamp="true"/>
<ResourceParams name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.3.57:3306/books</value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
===========================================================

/usr/local/tomcat5/webapps/Advanced/connect.jsp
===========================================================
<html>
<head>
<title>Connection Test</title>
</head>
<body>

<%
com.wrox.library.Connect con = new com.wrox.library.Connect();
con.init();
%>

<h2>Connection Result</h2>
<%= con.getstat() %>
</body>
</html>
===========================================================

/usr/local/tomcat5/webapps/Advanced/WEB-INF/classes/com/wrox/library/Connect.java
===========================================================
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class Connect {

String stat = "Not Connected";

public void init() {
try {
stat = "1";
Context ctx = new InitialContext();
stat = "2";
if(ctx == null )
throw new Exception("Oops - No Context");
stat = "3";
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";
if (ds != null) {
Connection conn = ds.getConnection();
stat = "5";
if(conn != null) {
stat = " Got Connection "+conn.toString();
conn.close();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}

public String getstat() {
return stat;
}
}
===========================================================

When I tried to display connect.jsp, what I got display on IE6 is as follow:
=================================================
Connection Results
4
==================================================

Which means that the Connect.class unable to get the DataSource, for which the DataSource is NULL. Pls help !!
0
Comment
Question by:eexcel
  • 9
  • 4
  • 2
17 Comments
 

Author Comment

by:eexcel
ID: 9599166
pls help guys .....thanks !!
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9599601
0
 

Author Comment

by:eexcel
ID: 9603502
Hi applekanna,

    So what's wrong with my codes above?? Why can't it get connected ?
Why
ds == NULL at the code below ?

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/BooksDB");
stat = "4";


Pls help !!
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 3

Expert Comment

by:applekanna
ID: 9603531
Are u using any IDE like JBUilder or IntelliJ etc...
0
 

Author Comment

by:eexcel
ID: 9604177
Hi Apple,

    I m using mysql-4.0.14b-win.zip installed under Windows 2K Server and mysql-connector-java-3.0.8-stable-bin.jar (placed under /usr/local/tomcat5/common/lib) and I also put
1) commons-dbcp.jar
2) commons-pool.jar
3) commons-collections.jar

to /usr/local/tomcat5/common/lib

I have also modified server.xml file as follow:

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="Wrox_Advanced_log."
       suffix=".txt" timestamp="true"/>
<Resource name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/BooksDB">
<parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<parameter>
                <name>maxActive</name>
      <value>100</value>
</parameter>
<parameter>
      <name>maxIdle</name>
      <value>30</value>
</parameter>
<parameter>
      <name>maxWait</name>
      <value>10000</value>
</parameter>
<parameter>
                <name>removeAbandoned</name>
                <value>true</value>
</parameter>
<parameter>
               <name>removeAbandonedTimeout</name>
               <value>180</value>
</parameter>
<parameter>
                        <name>logAbandoned</name>
                        <value>true</value>
</parameter>
<parameter>
      <name>username</name>
      <value>booksuser</value>
</parameter>
<parameter>
      <name>password</name>
      <value>bookspass</value>
</parameter>
<parameter>
      <name>driverClassName</name>
                <value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
      <name>url</name>
      <value>jdbc:mysql://192.1.3.139:3306/books?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

and I still can't get it to connect. I still stucked at
ds == NULL

Pls..................help...................
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 63 total points
ID: 9604442
can you check your tomcat log and see if you get any error when startup?
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604484
is your sql server running now. I will try to connect
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604512
eexcel.

I used your code om my machine and it works fine. These are the differences in my system.
I use com.mysql.jdbc.Driver.
I use my db.

Other than that no changes to your code except db changes.

I suspect the following

1. you might have not granted access to your database use "bookuser"

2. Extract from MySQL website
> "Versions of MySQL and the mm.mysql JDBC driver when have been reported to work:
>
>MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha
>mm.mysql 2.0.14 (JDBC Driver)
>Please let us know if you have tested the new MySQL mm.mysql 3.0 driver."

Tyr using the older veriosn of the driver.

3. go back to
A step by step tutorial
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/printer/jndi-datasource-examples-howto.html#MySQL%20DBCP%20Example

and do that on ur machine. It will definately work

Does this help !
Cheers!


0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604517
oops i forgot
check if you have a databse called "books" in your MySQL databse
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604531
I also tried connecting to your database

I get the following error
java.sqlException. BorrowFailed. Is there a MySQLserver running on the machine.

Check if you have started the mysql server

to start mysql server

C:\mysql\bin\mysqld
   
will start the server.

Cheers!
0
 
LVL 3

Assisted Solution

by:applekanna
applekanna earned 62 total points
ID: 9604540
Becoz of the error stated above this is what I get on my JSP
I get

Connection Result
4

which I guess is what you get :)
0
 

Author Comment

by:eexcel
ID: 9604715
Yeah, it works now !!
I have changed

<parameter>
     <name>username</name>
     <value>root</value>
</parameter>
<parameter>
     <name>password</name>
     <value>13467982</value>
</parameter>

The question is I m very sure that there is booksuser/bookspass in mysql db. I suppose that as long as there is a user "booksuser" in mysql table (user), I can connect in. Why is that I used booksuser/bookspass and can't even connect ? Thanks for u guys help !! Thanks a lot !!
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9604724
>The question is I m very sure that there is booksuser/bookspass in mysql db.

is it is a user for the database or a user in the database?
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9605212
if you choose a comment for an answer. This question would move to the solved section helping all

:)
Cheers!
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9607352
always check you tomcat log, those kind of error, like user/password is not correct, should show up in you tomcat log.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
As a business owner, there are many things that keep you up at night. Profit margins, employee retention, human resource protocols, whether your product or service will remain competitive. When you own or manage a technology company that operates la…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question