Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
JNDI Lookups in Sun ONE

Posted by Custard (Custard), 13 November 2003
Hi, guys.

I'm using SunOne, and have created 3 mysql connection pools in the admin console. They are called.

Code:
jdbc/appDBPoolDataSource
jdbc/msgDBPoolDataSource
jdbc/rptDBPoolDataSource


In my class that gets connections, I have...

Code:
public class HorizonConnectionPoolFactory {
       private static HashMap ds=new HashMap();        // HashMap of stored DataSource objects
       private final static LogHelper LOG = new LogHelper(HorizonConnectionPoolFactory.class);
       private final static String jndiAppDataSource="jdbc/appDBPoolDataSource";               // JNDI ref to app Datasource
       private final static String jndiMsgDataSource="jdbc/msgDBPoolDataSource";               // JNDI ref to message Datasource
       private final static String jndiRptDataSource="jdbc/rptDBPoolDataSource";               // JNDI ref to reporting Datasource

       /** Get a Connection from the App Connection Pool.
               @return Connection
       */
       public static Connection getAppConnection() throws NamingException,SQLException {
               return getDataSource( jndiAppDataSource ).getConnection();
       }

       /** Get a Connection from the Reporting Connection Pool.
               @return Connection
       */
       public static Connection getReportingConnection() throws NamingException,SQLException {
               return getDataSource( jndiRptDataSource ).getConnection();
       }

       /** Get a Connection from the Message Connection Pool.
               @return Connection
       */
       public static Connection getMessageConnection() throws NamingException,SQLException {
               return getDataSource( jndiMsgDataSource ).getConnection();
       }

       /** Get a DataSource object from jndi lookup.
               @return DataSource
       */
       private static DataSource getDataSource( String jndiDataSource ) throws NamingException {
               // Get a DataSource object and store it in a  static HashMap
               // keyed by jndiDataSource. Because JNDI lookups are expensive.
               try {
                       if (ds.get(jndiDataSource)==null) {
                               InitialContext ctx = new InitialContext();
                               ds.put(jndiDataSource, (DataSource) ctx.lookup( jndiDataSource ));
                       }
               } catch( NamingException e ) {
                       LOG.error( "getDataSource: NamingException while trying to retrieve DataSource from jndi with name "+
                                       jndiDataSource
                       );
                       throw( e );             // Escalate the exception
               }
               return (DataSource) ds.get(jndiDataSource);     // Return the datasource object from the HashMap
       }
}


And I get the following exception.

Code:
Root Cause
javax.naming.NameNotFoundException: No object bound to name jdbc/appDBPoolDataSource
     at com.sun.enterprise.naming.NamingManagerImpl.lookup(NamingManagerImpl.java:796)
     at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:107)
     at javax.naming.InitialContext.lookup(InitialContext.java:347)
     at com.comtelco.central.reporting.data.HorizonConnectionPoolFactory.getDataSource(HorizonConnectionPoolFactory.java:71)
     at com.comtelco.central.reporting.data.HorizonConnectionPoolFactory.getAppConnection(HorizonConnectionPoolFactory.java:45)
     at com.comtelco.central.reporting.model.UserGroupSession.getUserGroupDataBean(UserGroupSession.java:123)


And I'm not sure why. The names are the same because I cut & pasted them.

I'm also not sure where the JNDI resources live in SunOne if you just use InitialContext() with no params.

From what I've read, theres supposed to be a jndi.properties file somewhere, but I can't find it (after extensive searching).

Any Ideas, or 'spot the obvious bug' ideas?

B

Posted by Custard (Custard), 13 November 2003
Ok, I have the answer to the JNDI bit..

1st, The jndi refs in the code need java:comp/env/ in front of them.

2nd, You need a resource-refs section in sun-web.xml,
and a similar resource refs section in web.xml.

3rd, The resource-refs section in web.xml has to be after the taglibs section, but before the security roles section to avoid an xml parsing error. (These appservers are picky!)

I'll post better examples soon, as I don't have them to hand.

I'm assuming (because it's like pulling teeth to find this stuff out sometimes) that the entries in web.xml tie the entries in the appserver jndi table to entries looked up by java:comp/env.

Sometimes I wonder why setting up web apps on appservers is so   painful when they are supposed to make life easier..

B

Posted by admin (Graham Ellis), 14 November 2003
Opinion ....

Sun and the rest of the Java community have tried so hard to include the flexibility to do anything in their language / classes / application servers that it's sometimes not obvious / easy to find out how to do the simple things.   Further, the Java philosophy of being a real "maiden aunt" of a language that rejects things that aren't exactly right is great for long term robustness is fantastic for long term reliability but is so frustrating when you're learning in to a new part of the subject.

Thanks for posting your solution, Custard; the thread will be archived and anyone searching on a similar problem in the future may happen upon your experience.

Posted by Custard (Custard), 17 November 2003
You're right, it's like pulling teeth to get answers to the simple stuff sometimes.

Here's some cfg files for your delectation and delight.

This ones an extract from web.xml

Code:
      <resource-ref>
          <description>Application DataSource Reference</description>
          <res-ref-name>jdbc/appDBPoolDataSource</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
       </resource-ref>

       <resource-ref>
          <description>Messaging DataSource Reference</description>
          <res-ref-name>jdbc/msgDBPoolDataSource</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
       </resource-ref>

       <resource-ref>
          <description>Reporting DataSource Reference</description>
          <res-ref-name>jdbc/rptDBPoolDataSource</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
       </resource-ref>



Note the ordering of resource-ref tags in the file. They must come after taglibs and before security roles. At least in my file they do. The ordering IS important.

Here's my sun-web.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sun-web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 7.0 Servlet 2.3//EN' 'http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_3- 0.dtd'>

<sun-web-app>

  <session-config>
     <session-manager/>
  </session-config>

  <resource-ref>
     <res-ref-name>jdbc/appDBPoolDataSource</res-ref-name>
     <jndi-name>jdbc/appDBPoolDataSource</jndi-name>
  </resource-ref>

  <resource-ref>
     <res-ref-name>jdbc/rptDBPoolDataSource</res-ref-name>
     <jndi-name>jdbc/rptDBPoolDataSource</jndi-name>
  </resource-ref>

  <resource-ref>
     <res-ref-name>jdbc/msgDBPoolDataSource</res-ref-name>
     <jndi-name>jdbc/msgDBPoolDataSource</jndi-name>
  </resource-ref>

  <jsp-config/>

</sun-web-app>



The app can then refer to the jndi names by:-

Code:
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup( "java:comp/env/jdbc/rptDBPoolDataSource" ));


Hope this helps..



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho