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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to