JNDI lookup

23 views
Skip to first unread message

Poor Lee A More

unread,
Dec 17, 2009, 10:11:06 PM12/17/09
to Jaxcent
Do anyone have ideal how to get this JNDI lookup work in Jaxcent :
i can get the connection in my JSP but failed in Jaxcent java.

My code like this:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/OracleDs");
Connection conn = ds.getConnection();


i had already defined the the jdbc/OracleDs in web.xml as:
<resource-ref>
<res-ref-name>jdbc/OracleDs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Mukesh

unread,
Dec 28, 2009, 12:32:41 PM12/28/09
to Jaxcent
This is a J2EE thing - if you are using Jaxcent with a J2EE container,
e.g. Tomcat, it should automatically be available. But if you are
using the Jaxcent stand-alone server, it doesn't set up jndi
contexts. Maybe this will be a useful thing to add - does anybody
else have any comments on this?

Poor Lee A More

unread,
Dec 28, 2009, 10:35:22 PM12/28/09
to jaxcen...@googlegroups.com
yes. i'm using Jaxcent with J2EE.

i modified the JaxcentServlet to lookup my required DataSource, then put it in Application Context.
don't know whether it is a decent way.


public class JaxcentServlet extends HttpServlet implements jaxcentFramework.JaxcentHttpMain
{
    JaxcentFramework framework = new JaxcentFramework( this );
    DataSource ds = null;

    public void setDataSource() throws Exception {
        if ( ds == null ) {
                Context ctx = new InitialContext();
                ds = (DataSource) ctx.lookup("jdbc/OracleDs");
                getServletContext().setAttribute( "my.DataSource", ds );
        }
    }
    
    public void init()
          throws ServletException
    {
        framework.init();
        this.setDataSource();      // lookup DS ...
    }

Now I can get the datasource from the Application Context.
and finally get the connection....

        HttpSession session = (HttpSession) this.getHttpSession();
        ServletContext ctx2 = session.getServletContext();
        if ( ds == null ) {
            ds = (DataSource) ctx.getAttribute("my.DataSource");
            conn = ds.getConnection();
        }


2009/12/29 Mukesh <mkp....@desisoft.com>
--

You received this message because you are subscribed to the Google Groups "Jaxcent" group.
To post to this group, send email to jaxcen...@googlegroups.com.
To unsubscribe from this group, send email to jaxcentgroup...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jaxcentgroup?hl=en.



Reply all
Reply to author
Forward
0 new messages