You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Lars Nielsen Lind <mo...@worldonline.dk> on 2005/04/20 10:20:26 UTC

Tomcat 5.5.9 Connection Pool / JNDI / DBCP

Hi.

I have some problems with Tomcat 5.5.9 Connection Pooling / JNDI / DBCP

When running my java component (se below) I receive this NamingException:

/NE: 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/

If I add this code:

System.setProperty("java.naming.factory.initial",  
"org.apache.naming.java.javaURLContextFactory");
System.setProperty("java.naming.factory.pkgs",  "org.apache.naming");

to the constructor in the java component, I receive this exception:

/NE: Name java: is not bound in this Context/

What is wrong and what is the solution to the problem?

Thanks
Lars Nielsen Lind



I have copied the PostgreSQL driver, commons-pool.jar, 
commons-collections.jar and commons-dbcp.jar  to the 
$CATALINA_HOME/common/lib folder.

I have done as specified in the JNDI Datasource HOW-TO.

*Server.xml*:

<Context path="/" debug="1" 
docBase="/opt/jakarta-tomcat-5.5.9/webapps/application/test">
        <!-- PostgreSQL / JDBC / JNDI - ConnectionPooling -->
        <Resource name="jdbc/testdatabase" auth="Container" 
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" 
url="jdbc:postgresql://localhost/testdatabase" username="dbmanager" 
password="123456" maxActive="20" maxIdle="10" maxWait="-1" 
removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" />
</Context>


*Application Web.xml*:

<resource-ref>
    <description>PostgreSQL DataSource</description>
    <res-ref-name>jdbc/testdatabase</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>


*Java component*:

import java.sql.*;
import java.util.*;

import javax.naming.*;
import javax.sql.*;

public class ConnectionPool {
   
    private Connection conn;

    public ConnectionPool() {}

    public Connection getConnectionFromPool() {
        try {
            InitialContext initContext = null;
           
            try {
                initContext = new InitialContext();
            } catch (Exception ex) {
                System.out.println("IC: " + ex);
            }

            try {
                DataSource ds = 
(DataSource)initContext.lookup("java:/comp/env/jdbc/testdatabase");
                conn = ds.getConnection();
            } catch (SQLException se) {
                System.out.println("DS: " + se);
            }
        } catch (NamingException ne) {
            System.out.println("NE: " + ne.getMessage());
        }
       
        return conn;
    }
   

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Tomcat 5.5.9 Connection Pool / JNDI / DBCP

Posted by Lars Nielsen Lind <mo...@worldonline.dk>.
I still can't make it work.

My /opt/jakarta-tomcat-5.5.9/logs/catalina.out display this message

2005-04-23 08:48:21 org.apache.commons.modeler.Registry registerComponent
SEVERE: Null component 
Catalina:type=DataSource,path=/,host=192.168.1.251,class=javax.sql.DataSource,name="jdbc/testdatabase"

And maybe that is the problem.

I have seen somewhere else that the Context part with the Resource is 
moved to a file called <applicationname>.xml. Some say to place in 
<application>/META-INF, some say to place it in 
$CATALINA_HOME/conf/Catalina/localhost. I have tried both but it does 
not work for me.

When I remove the Context from my server.xml to a separate file - I see 
this when running Tomcat-Apache:    No Host matches server name 
192.168.1.251.

Thanks
Lars Nielsen Lind



David Smith wrote:

> You should be doing this in your code:
>
> Context initContext = null;
> try {
>  initContext = new InitialContext() ;
> } catch ( Exception e1 ) { //... do something with the trapped 
> exception }
> try {
>  DataSource ds = 
> (DataSource)initContext.lookup("java:/comp/env/jdbc/testdatabase");
> } catch ( Exception e2) { //... do something -- log it }
> // Store a reference to ds someplace where it can be used over and 
> over and over.
>
> Note the only thing I really changed is to make the type of 
> initContext Context instead of InitialContext.  This is mostly 
> straight from the Tomcat 5.5 JNDI docs located here:
>
> http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-resources-howto.html
>
> --David
>
>
> Lars Nielsen Lind wrote:
>
>> Hi.
>>
>> I have some problems with Tomcat 5.5.9 Connection Pooling / JNDI / DBCP
>>
>> When running my java component (se below) I receive this 
>> NamingException:
>>
>> /NE: 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/
>>
>> If I add this code:
>>
>> System.setProperty("java.naming.factory.initial",  
>> "org.apache.naming.java.javaURLContextFactory");
>> System.setProperty("java.naming.factory.pkgs",  "org.apache.naming");
>>
>> to the constructor in the java component, I receive this exception:
>>
>> /NE: Name java: is not bound in this Context/
>>
>> What is wrong and what is the solution to the problem?
>>
>> Thanks
>> Lars Nielsen Lind
>>
>>
>>
>> I have copied the PostgreSQL driver, commons-pool.jar, 
>> commons-collections.jar and commons-dbcp.jar  to the 
>> $CATALINA_HOME/common/lib folder.
>>
>> I have done as specified in the JNDI Datasource HOW-TO.
>>
>> *Server.xml*:
>>
>> <Context path="/" debug="1" 
>> docBase="/opt/jakarta-tomcat-5.5.9/webapps/application/test">
>>        <!-- PostgreSQL / JDBC / JNDI - ConnectionPooling -->
>>        <Resource name="jdbc/testdatabase" auth="Container" 
>> type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" 
>> url="jdbc:postgresql://localhost/testdatabase" username="dbmanager" 
>> password="123456" maxActive="20" maxIdle="10" maxWait="-1" 
>> removeAbandoned="true" removeAbandonedTimeout="60" 
>> logAbandoned="true" />
>> </Context>
>>
>>
>> *Application Web.xml*:
>>
>> <resource-ref>
>>    <description>PostgreSQL DataSource</description>
>>    <res-ref-name>jdbc/testdatabase</res-ref-name>
>>    <res-type>javax.sql.DataSource</res-type>
>>    <res-auth>Container</res-auth>
>> </resource-ref>
>>
>>
>> *Java component*:
>>
>> import java.sql.*;
>> import java.util.*;
>>
>> import javax.naming.*;
>> import javax.sql.*;
>>
>> public class ConnectionPool {
>>      private Connection conn;
>>
>>    public ConnectionPool() {}
>>
>>    public Connection getConnectionFromPool() {
>>        try {
>>            InitialContext initContext = null;
>>                      try {
>>                initContext = new InitialContext();
>>            } catch (Exception ex) {
>>                System.out.println("IC: " + ex);
>>            }
>>
>>            try {
>>                DataSource ds = 
>> (DataSource)initContext.lookup("java:/comp/env/jdbc/testdatabase");
>>                conn = ds.getConnection();
>>            } catch (SQLException se) {
>>                System.out.println("DS: " + se);
>>            }
>>        } catch (NamingException ne) {
>>            System.out.println("NE: " + ne.getMessage());
>>        }
>>              return conn;
>>    }
>>  
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Tomcat 5.5.9 Connection Pool / JNDI / DBCP

Posted by David Smith <dn...@cornell.edu>.
You should be doing this in your code:

Context initContext = null;
try {
  initContext = new InitialContext() ;
} catch ( Exception e1 ) { //... do something with the trapped exception }
try {
  DataSource ds = 
(DataSource)initContext.lookup("java:/comp/env/jdbc/testdatabase");
} catch ( Exception e2) { //... do something -- log it }
// Store a reference to ds someplace where it can be used over and over 
and over.

Note the only thing I really changed is to make the type of initContext 
Context instead of InitialContext.  This is mostly straight from the 
Tomcat 5.5 JNDI docs located here:

http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-resources-howto.html

--David


Lars Nielsen Lind wrote:

> Hi.
>
> I have some problems with Tomcat 5.5.9 Connection Pooling / JNDI / DBCP
>
> When running my java component (se below) I receive this NamingException:
>
> /NE: 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/
>
> If I add this code:
>
> System.setProperty("java.naming.factory.initial",  
> "org.apache.naming.java.javaURLContextFactory");
> System.setProperty("java.naming.factory.pkgs",  "org.apache.naming");
>
> to the constructor in the java component, I receive this exception:
>
> /NE: Name java: is not bound in this Context/
>
> What is wrong and what is the solution to the problem?
>
> Thanks
> Lars Nielsen Lind
>
>
>
> I have copied the PostgreSQL driver, commons-pool.jar, 
> commons-collections.jar and commons-dbcp.jar  to the 
> $CATALINA_HOME/common/lib folder.
>
> I have done as specified in the JNDI Datasource HOW-TO.
>
> *Server.xml*:
>
> <Context path="/" debug="1" 
> docBase="/opt/jakarta-tomcat-5.5.9/webapps/application/test">
>        <!-- PostgreSQL / JDBC / JNDI - ConnectionPooling -->
>        <Resource name="jdbc/testdatabase" auth="Container" 
> type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" 
> url="jdbc:postgresql://localhost/testdatabase" username="dbmanager" 
> password="123456" maxActive="20" maxIdle="10" maxWait="-1" 
> removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" />
> </Context>
>
>
> *Application Web.xml*:
>
> <resource-ref>
>    <description>PostgreSQL DataSource</description>
>    <res-ref-name>jdbc/testdatabase</res-ref-name>
>    <res-type>javax.sql.DataSource</res-type>
>    <res-auth>Container</res-auth>
> </resource-ref>
>
>
> *Java component*:
>
> import java.sql.*;
> import java.util.*;
>
> import javax.naming.*;
> import javax.sql.*;
>
> public class ConnectionPool {
>      private Connection conn;
>
>    public ConnectionPool() {}
>
>    public Connection getConnectionFromPool() {
>        try {
>            InitialContext initContext = null;
>                      try {
>                initContext = new InitialContext();
>            } catch (Exception ex) {
>                System.out.println("IC: " + ex);
>            }
>
>            try {
>                DataSource ds = 
> (DataSource)initContext.lookup("java:/comp/env/jdbc/testdatabase");
>                conn = ds.getConnection();
>            } catch (SQLException se) {
>                System.out.println("DS: " + se);
>            }
>        } catch (NamingException ne) {
>            System.out.println("NE: " + ne.getMessage());
>        }
>              return conn;
>    }
>  
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org