Sorry for creating the confusion. Actually I was seeing the connection
object to be null for long time. But at last moment I made those silly
changes to present it in a better way. Sorry about it. But now I
printing the connection object and finding it to be null. I cannot
understand, the reason for connection object to be null, even when
datasource object is not null. Is it because that I am not able to
connect to database? If yes what could be the reason. If I specify the
attributes, using Direct JDBC by using Class.forName() and
DriverManager.getConnection(), I can connect to data base and read all
the values. But the problem comes when I use Connection pooling. Where
shuld I look for correction??
------------------------------------------------------------------
Part of my code and JSP o/p
-----------------------------------------------------------------
public LinkedList selectCP(){
LinkedList ll = new LinkedList();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
DataSource dataSource = null;
try{
Context init = new InitialContext();
Context ctx = (Context) init.lookup("java:comp/env");
dataSource = (DataSource)ctx.lookup("jdbc/conversion");
ll.add("dataSource object is");
ll.add(dataSource);
}
catch(NamingException e){
System.out.println("Exception thrown is"+e.getMessage());
ll.add("Naming Exception thrown");
}
finally{
if(dataSource == null){
ll.add("DataSource object is null");
return ll;
}
else{
ll.add("Data source object is not null");
}
}
try{
ll.add("Inside try.");
ll.add("con is");
ll.add(con);
if(con == null){
ll.add("Inside try. Connection object found to be
null");
ll.add(con);
return ll;
}
else{
ll.add("Connection object inside else is");
ll.add(con); }
}
}
catch(SQLException e) {
System.err.println("SQL EXCEPTION"+e.getMessage());
ll.add("SQL Exception ");
ll.add(e.getMessage());
}
catch(Exception e) {
System.err.println(" class not found
exception"+e.getMessage());
ll.add("Exception generated:");
ll.add(e.getMessage());
}
finally{
if(con == null)
ll.add("Connection object is null");
}
try{
stmt.close();
}
catch(Exception e){}
try{
rs.close();
}
catch(Exception e){}
try{
con.close();
}
catch(Exception e){}
return ll;
}
---------------------------O/P------------------------------------------
---
I am pasting the ll obtained on browser--
[dataSource object is, [EMAIL PROTECTED],
Data source object is not null, Inside try., con is, null, Inside try.
Connection object found to be null, null, Connection object is null]
-----Original Message-----
From: David Smith [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 04, 2004 1:35 PM
To: Tomcat Users List
Subject: Re: DB connection pool problem
Sorry about the last post. It's been a looonnng day and I didn't read
it very well :-(.
From what I see, you're comparing conn to null before even attempting
to get a connection. Try comparing dataSource to null instead of conn
in the second catch block. Then do dataSource.getConnection() if
dataSource != null.
--David
David Smith wrote:
> I'm a bit confused on your code:
>
> try{
> // load the driver class file
> //Class.forName("com.mysql.jdbc.Driver").newInstance();
> //establishing the connection
> if(con == null){
> ll.add("Inside 1st try. Connection object found to be
> null");
>
>
> Why are you doing a Class.forName call after getting a datasource
> object? I think you're mixing incompatible database access methods.
> Try doing dataSource.getConnection() after getting a valid datasource
> instead. You'll be happier with the result. Take a look at the
> javadocs for javax.sql.DataSource for more info.
>
> --David
>
> Arora, Avinash wrote:
>
>> Hi friends,
>> Thanks for your previous feedback. After adding several
>> logging statements in my code, I have found that connection object is
>> null. But dataSource object is not null. What can be reason for it??
>> Here is how I use it in my code--
>> public LinkedList testFunction(){
>> LinkedList ll = null; Connection con = null;
>> Statement stmt = null;
>> ResultSet rs = null;
>> DataSource dataSource = null;
>> try{
>> Context init = new InitialContext();
>> Context ctx = (Context) init.lookup("java:comp/env");
>> dataSource = (DataSource)ctx.lookup("jdbc/conversion");
>> ll.add("dataSource object is");
>> ll.add(dataSource);
>> }
>> catch(NamingException e){
>> System.out.println("Exception thrown is"+e.getMessage());
>> ll.add("Naming Exception thrown");
>> } finally{
>> if(dataSource == null){
>>
>> ll.add("DataSource object is null");
>> return ll;
>> }
>> else{
>> ll.add("Data source object is not null");
>> // return ll;
>> }
>> }
>> try{
>> // load the driver class file
>> //Class.forName("com.mysql.jdbc.Driver").newInstance();
>> //establishing the connection
>> if(con == null){
>> ll.add("Inside 1st try. Connection object found to be
>> null");
>> }
>> else{
>> ll.add("Inside 1st try. Connection object is not
nul");
>> con = dataSource.getConnection();
>> stmt = con.createStatement();
>> ....
>> }
>> finally{
>> if(con == null){
>> ll.add("Connection object is null");
>> // return ll;
>> }
>> ----------------------Here is the output that I can see on my jsp
page,
>> where I try to print out the linklist obtained from the function
above.
>>
>> elements of link list read from database are -[dataSource object is,
>> [EMAIL PROTECTED], Data source object is
>> not null, Inside 1st try. Connection object found to be null,
Connection
>> object is null]
>>
>>
>>
>>
>> Thanks.
>> Avinash Arora
>>
>>
>> -----Original Message-----
>> From: Daxin Zuo [mailto:[EMAIL PROTECTED] Sent: Monday, May 03,
>> 2004 3:34 PM
>> To: Tomcat Users List
>> Subject: RE: DB connection pool problem
>>
>> I have just created a "pool" -- I hope it is really a pool. I used
>> jtds
>> jdbc driver to MS SQL Server on Windows. You may find some thing
>> similar.
>> Holp it helps.
>>
>> 1) bellow is the text in tomcat\conf\server.xml
>> <GlobalNamingResources>
>> <Resource name="jdbc/DirectSQL" auth="Container"
>> type="javax.sql.DataSource"/>
>> <ResourceParams name="jdbc/DirectSQL">
>> <parameter>
>> <name>factory</name>
>> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
>> </parameter>
>>
>> <parameter>
>> <name>maxActive</name>
>> <value>255</value>
>> </parameter>
>> <parameter>
>> <name>maxIdle</name>
>> <value>10</value>
>> </parameter>
>> <parameter>
>> <name>maxWait</name>
>> <value>10000</value>
>> </parameter>
>> <parameter>
>> <name>username</name>
>> <value>user1</value>
>> </parameter>
>> <parameter>
>> <name>password</name>
>> <value>passwd1</value>
>> </parameter>
>> <parameter>
>> <name>driverClassName</name>
>> <value>net.sourceforge.jtds.jdbc.Driver</value>
>> </parameter>
>> <parameter>
>> <name>url</name>
>> <value>jdbc:jtds:sqlserver://serverName:1433</value>
>> </parameter>
>> </ResourceParams>
>> ...
>>
>> 2) bellow is the text in tomcat\conf\Catalina\localhost\ROOT.XML --I
>> create
>> this file by myself
>> <Context path="" docBase="ROOT" debug="0" privileged="true">
>>
>> <!-- Link to the user database using dbc:jtds:sqlserver -->
>> <ResourceLink name="jdbc/DirectSQL" global="jdbc/DirectSQL"
>> type="javax.sql.DataSource"/>
>> </Context>
>> This "name" is used in Java code. "global" is the name in xml file.
they
>> can
>> be different.
>>
>> 3) my test jsp file is in \tomcat\webapps\ROOT\zuo\
>>
>> Context ctx = new InitialContext();
>> if (ctx == null) {
>> throw new Exception("Boom - No Context");
>> }
>> DataSource ds = (DataSource)
>> ctx.lookup("java:comp/env/jdbc/DirectSQL");
>> if (ds != null)
>> con = ds.getConnection();
>> if (con != null){
>> str = "select * from myTable";
>> pStateSearch = con.prepareStatement(str,
>> ResultSet.TYPE_SCROLL_SENSITIVE,
>> ResultSet.CONCUR_READ_ONLY );
>> rs = pStateSearch.executeQuery();
>> ....
>>
>> That's all.
>>
>> -----Original Message-----
>> From: Arora, Avinash [mailto:[EMAIL PROTECTED]
>> Sent: Monday, May 03, 2004 3:10 PM
>> To: Tomcat Users List
>> Subject: DB connection pool problem
>>
>>
>> Hi,
>> I am also having the problem with the connection pools. Since I am
>> writing an application first time by using connection pools, I
created a
>> test application DBTest. I edited my server.xml and web.xml (of my
>> application in following way--
>> #added to server.xml
>> <Context path="/DBTest" docBase="DBTest" debug="0"
>> reloadable="true">
>> <ResourceParams name="jdbc/conversion">
>> <parameter>
>> <name>username</name>
>> <value>ROOT</value>
>> </parameter>
>> <parameter>
>> <name>password</name>
>> <value>PASSWORD</value>
>> </parameter>
>> <parameter>
>> <name>driverClassName</name>
>> <value>com.mysql.jdbc.Driver</value>
>> </parameter>
>> <parameter>
>> <name>url</name>
>> <value>jdbc:mysql://localhost/DB</value>
>> </parameter>
>> </ResourceParams>
>> </Context>
>>
>> #web.xml
>> <web-app>
>> <description>MySQL Test App</description>
>> <resource-ref>
>> <res-ref-name>jdbc/conversion</res-ref-name>
>> <res-type>javax.sql.DataSource</res-type>
>> <res-auth>Container</res-auth>
>> </resource-ref>
>> </web-app>
>> And I added the following statements to my Java class, (which works
>> fine, when I use the connection objects by hardcoding the username
and
>> db).--
>>
>> Context init = new InitialContext();
>> Context ctx = (Context) init.lookup("java:comp/env");
>> DataSource dataSource = (DataSource)ctx.lookup("jdbc/conversion");
>>
>> But even after, making these changes, I cannot read any thing from
>> database. I was wondering what more changes I need to made. I also
tried
>> to run my JAVA class (DAO) from command line, but I get the following
>> exception--
>>
>> Need to specify class name in environment or system property, or as
an
>> applet parameter, or in an application resource file:
>> java.naming.factory.initial
>>
>> I tried to print outs the place where exception occurs in my code and
>> found that exception occurs at
>> dataSource = (DataSource)ctx.lookup("jdbc/conversion");
>>
>> Can any body please comment on whats going on wrong? When I run from
>> command line, is tomcat used, I don't think so? So where is the
problem?
>> Any ideas??
>> Avinash Arora
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]