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.