There is a difference between Tomcat and JBoss descriptors about
Datasources... It is the source of a lot of mess in web applications.

Tomcat: java:comp/env/jdbc/<NAME>
JBoss:  java:<NAME>

A common solution is to include the full environment string into a
separated properties file (avoiding the web.xml auto loading by the
container):

            Properties config = new Properties();
            config.load("yourConfigFile.properties");
            String name = config.getProperty("datasource");
            InitialContext context = new InitialContext();
            DataSource dbSource = (DataSource) context
                    .lookup(name);

Note: I“m working on an Open Source project, which contains the
complete source code to access MySql database through Tomcat - if you
are interested, you can check the code at:
https://cejug-classifieds.dev.java.net/source/browse/cejug-classifieds/web-app/

best regards,

   Felipe Gaścho


On Wed, 09 Feb 2005 21:03:30 -0500, Kris Schneider <[EMAIL PROTECTED]> wrote:
> Try an <env-entry> instead of a <context-param>:
> 
> <env-entry>
>   <env-entry-name>dataSource</env-entry-name>
>   <env-entry-value>jdbc/RestaurantDS</env-entry-value>
>   <env-entry-type>java.lang.String</env-entry-type>
> </env-entry>
> 
> String envBase = "java:comp/env/";
> Context ctx = new InitialContext();
> String dataSourceName = (String)ctx.lookup(envBase + "dataSource");
> DataSource ds = (DataSource)ctx.lookup(envBase + dataSourceName);
> 
> That way you don't need access to a ServletContext instance.
> 
> Jack Lauman wrote:
> > Schalk:
> >
> > I need to use JDNI... this is a JBoss DataSource.  Any idea on how this
> > might be done?
> >
> > Thanks,
> >
> > Jack
> >
> > Schalk Neethling wrote:
> >
> >> Jack
> >>
> >> This is obviously if you are not using JNDI to lookup a datasource.
> >>
> >> Schalk Neethling wrote:
> >>
> >>> Jack
> >>>
> >>> Within a servlet I use the following:
> >>>
> >>> public class activateArticle extends HttpServlet {
> >>>
> >>>    public String DRIVER, URL, USER, PASS, message;
> >>>              public void init() throws ServletException {
> >>>        ServletContext context = getServletContext();
> >>>        DRIVER = context.getInitParameter("DRIVER");
> >>>        URL = context.getInitParameter("URL");
> >>>        USER = context.getInitParameter("USER");
> >>>        PASS = context.getInitParameter("PASS");
> >>>        }
> >>>
> >>> HTH
> >>>
> >>> Jack Lauman wrote:
> >>>
> >>>> If I have a datasource in the context-param area of the web.xml
> >>>> file, how can it be called?
> >>>>
> >>>> <context-param>
> >>>>    <param-name>jdbcDataSource</param-name>
> >>>>    <param-value>java:comp/env/jdbc/RestaurantDS</param-value>
> >>>> </context-param>
> >>>>
> >>>>
> >>>> pageContext.
> >>>> getServletContext().getInitParameter("insert-context-param-name-here");
> >>>>
> >>>> Doesn't work here...
> >>>>
> >>>> ....
> >>>> private void initialize()
> >>>> {
> >>>>    try {
> >>>>        Context ctx = null;
> >>>>        DataSource ds = null;
> >>>>        Connection conn = null;
> >>>>        Result result = null;
> >>>>        try {
> >>>>            ctx = new InitialContext();
> >>>>            ds = (DataSource)
> >>>>                 ctx.lookup("java:comp/env/jdbc/RestaurantDS");
> >>>>        } catch (Exception e) {
> >>>>        System.out.println("DataSource context lookup failed: " + e);
> >>>>    }
> >>>>    try {
> >>>>        conn = ds.getConnection();
> >>>>        } catch (Exception e) {
> >>>>        System.out.println("DataSource getConnection failed: " + e);
> >>>>          e.printStackTrace();
> >>>>    }
> 
> --
> Kris Schneider <mailto:[EMAIL PROTECTED]>
> D.O.Tech       <http://www.dotech.com/>
> 
> ---------------------------------------------------------------------
> 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