You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Joshua McCulloch <co...@yahoo.com> on 2002/06/27 21:00:44 UTC

J2EE Datasources

Hello,

I'd like to know if anyone is using J2EE defined
datasources in Cocoon? 
I'm trying unsucessfully to connect to a MySQL
database using the Orion J2EE server. I've defined a
datasource in Orion:

<data-source 
		name="Mysql" 
		class="com.evermind.sql.DriverManagerDataSource"
		schema="database-schemas/mysql.xml" 
		location="jdbc/MysqlDS" 
		xa-location="jdbc/xa/MysqlXADS" 
		ejb-location="jdbc/MysqlDS" 
		connection-driver="org.gjt.mm.mysql.Driver" 
		username="root" 
		password="" 
		url="jdbc:mysql://localhost/myhms" 
		inactivity-timeout="30" 
	/>

I can get a javax.sql.DataSource using the following
code from within a custom class called by a custom
Transformer:

DataSource ds = (DataSource) new
InitialContext().lookup("jdbc/MysqlDS");


I then defined this datasource in cocoon.xconf:

<j2ee name="myhms-datasource">
      <dbname>MysqlDS</dbname>
</j2ee>

When Cocoon starts, this occurs in the error.log:

ERROR   (2002-06-27) 11:58.24:441  
[core.datasources.myhms-datasource] (Unknown-URI)
Unknown-thread/LogKitLogger: Problem with JNDI lookup
of datasource
javax.naming.NameNotFoundException: jdbc/MysqlDS not
found in Cocoon2 Demo
	at com.evermind._lj.lookup(.:49)
	at com.evermind._bm._es(.:121)
	at com.evermind._bm.lookup(.:63)
	at
javax.naming.InitialContext.lookup(InitialContext.java:345)
	at
org.apache.avalon.excalibur.datasource.J2eeDataSource.configure(J2eeDataSource.java:63)
	at
org.apache.avalon.excalibur.component.DefaultComponentFactory.newInstance(DefaultComponentFactory.java:172)
	at
org.apache.avalon.excalibur.component.ThreadSafeComponentHandler.initialize(ThreadSafeComponentHandler.java:84)
	at
org.apache.avalon.excalibur.component.ExcaliburComponentSelector.addComponent(ExcaliburComponentSelector.java:467)
	at
org.apache.avalon.excalibur.component.ExcaliburComponentSelector.configure(ExcaliburComponentSelector.java:354)
	at
org.apache.avalon.excalibur.component.DefaultComponentFactory.newInstance(DefaultComponentFactory.java:172)
	at
org.apache.avalon.excalibur.component.ThreadSafeComponentHandler.initialize(ThreadSafeComponentHandler.java:84)
	at
org.apache.avalon.excalibur.component.ExcaliburComponentManager.initialize(ExcaliburComponentManager.java:167)
	at
org.apache.cocoon.Cocoon.initialize(Cocoon.java:269)
	at
org.apache.cocoon.servlet.CocoonServlet.createCocoon(CocoonServlet.java:1212)
	at
org.apache.cocoon.servlet.CocoonServlet.init(CocoonServlet.java:407)
	

I haven't worked with JNDI before. What am I missing?
I looked at J2eeDataSource.java and found it was
calling InitialContext.lookup( "java:comp/env/jdbc/" +
"MysqlDS")

I tried changing the datasource location attribute to
"java:comp/env/jdbc/MysqlDS" which didn't do anything,
not that I was really expecting it to...

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>


Re: J2EE Datasources

Posted by Russell Castagnaro <ru...@castagnaro.com>.
Hello Joshua,

You might try looking up:

myhms-datasource

or
jdbc/myhms-datasource

instead of

jdbc/MysqlDS


The documentation from Orion should help you on this.  I know I did
the same thing using weblogic, but it took a couple of tries to get it
to work.

Also keep in mind that datasources can be webapp specific, so if you
haven't set up a datasource in the webapp itself, you'll never find it
using the "java:comp/env/jdbc/".


I'd also recommend making getting a list of the names of all of the
objects in your JNDI tree for future reference (after reading the
Orion docs) :)

Here's some code to help:

package com.synctank.labs.jndi;

import javax.naming.*;

public class JNDILister {
        public static void main(String _arg[]){
                Object o = null;  InitialContext ctx= null;
                String start = "foo.bar";
                if (_arg.length > 0) start = _arg[0];
                try{
                        ctx = getInitialContext();
                        Context c = (Context)ctx.lookup(start);
                        list(c);
                        
                } catch (ClassCastException e) {
                        System.out.println("Found a "+o.getClass().getName() +": "+o.toString() );
                } catch (NamingException ne) {
                        System.out.println("We have a problem!");
                        ne.printStackTrace();
                } finally {
                        try {  ctx.close(); }catch (Exception e ) {}
                }
        }

        public static void list(Context _ctx) 
        {
                StringBuffer sb = new StringBuffer();
                try {
                        NamingEnumeration enum = _ctx.listBindings("");
                        while (enum.hasMore()) {
                                javax.naming.Binding binding = (javax.naming.Binding)enum.next();
                                Object obj = (Object)binding.getObject();
                                if (obj instanceof Context) 
                                {
                                        System.out.print("---> ");
                                        System.out.print(binding.getName());
                                        System.out.print(".");
                                        list((Context)obj);
                                } else {        
                                        String name = binding.getName();
                                        System.out.print("LEAF: "+name);// + " is "+ obj.getClass().getName()) ;
                                                
                                }
                        }
                } catch (NamingException e) {
                        System.out.println(e);
                }
        }
        public static InitialContext getInitialContext() throws NamingException {
                // use the factory from your provider, this is an LDAP
                provider
                String factory = "com.sun.jndi.ldap.LdapCtxFactory";
                // use the url of
                String url = "ldap://ldap.bigfoot.com:389";
                String user = null; //the user name, if any
                String password = null; // password if any;
                
                java.util.Hashtable p = new java.util.Properties();
                p.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                p.put(Context.PROVIDER_URL, url);
        
                if (user != null && password != null ) {
                        System.out.println("user: " + user);
                        p.put(Context.SECURITY_PRINCIPAL, user);
                        p.put(Context.SECURITY_CREDENTIALS, password);
                }
                
                return new InitialContext(p);
        }
        

}

Thursday, June 27, 2002, 9:00:44 AM, you wrote:

JM> Hello,

JM> I'd like to know if anyone is using J2EE defined
JM> datasources in Cocoon? 
JM> I'm trying unsucessfully to connect to a MySQL
JM> database using the Orion J2EE server. I've defined a
JM> datasource in Orion:

JM> <data-source 
JM>                 name="Mysql" 
JM>                 class="com.evermind.sql.DriverManagerDataSource"
JM>                 schema="database-schemas/mysql.xml" 
JM>                 location="jdbc/MysqlDS" 
JM>                 xa-location="jdbc/xa/MysqlXADS" 
JM>                 ejb-location="jdbc/MysqlDS" 
JM>                 connection-driver="org.gjt.mm.mysql.Driver" 
JM>                 username="root" 
JM>                 password="" 
JM>                 url="jdbc:mysql://localhost/myhms" 
JM>                 inactivity-timeout="30" 
JM>         />

JM> I can get a javax.sql.DataSource using the following
JM> code from within a custom class called by a custom
JM> Transformer:

JM> DataSource ds = (DataSource) new
JM> InitialContext().lookup("jdbc/MysqlDS");


JM> I then defined this datasource in cocoon.xconf:

JM> <j2ee name="myhms-datasource">
JM>       <dbname>MysqlDS</dbname>
JM> </j2ee>





-- 
Best regards,
 Russell                            mailto:russell@castagnaro.com


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>