23 Nov 2009 @ 9:45 AM 

Firstly, create a method to get a connection from the pool

    private static Connection getJNDIConnection(){
        String DATASOURCE_CONTEXT = "java:comp/env/jdbc/airtime";
 
        Connection result = null;
        try {
          Context initialContext = new InitialContext();
          if ( initialContext == null){
            errorMsg = "ERROR: JNDI problem. Cannot get InitialContext.";
            return null;
          }
          DataSource datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
          if (datasource != null) {
            result = datasource.getConnection();
          }
          else {
            errorMsg = "ERROR: Failed to lookup datasource.";
            return null;
          }
        }
        catch ( Exception ex ) {
          errorMsg = "ERROR: Cannot get connection: " + ex;
          return null;
        }
        return result;
    }

Secondly, in your “sun-web.xml” and “web.xml” deployment descriptors, add a reference to the resource

sun-web.xml :

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

web.xml :

    <resource-ref>
        <res-ref-name>jdbc/airtime</res-ref-name> 
        <res-type>javax.sql.DataSource</res-type> 
          <res-auth>Container</res-auth> 
          <res-sharing-scope>Shareable</res-sharing-scope> 
    </resource-ref>
Posted By: Zayin
Last Edit: 23 Nov 2009 @ 12:18 PM

EmailPermalink
Tags
Tags: , ,
Categories: glassfish, mysql


 

Responses to this post » (2 Total)

 
  1. Jim says:

    Doesn’t appear to work. When the …lookup(DATASOURCE_CONTEXT) line executes, it throws this error:

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

  2. Zayin says:

    heres the method i created to get a connection

        private Connection getConnection()
        {
            DataSource datasource = null;
            try
            {
                String DATASOURCE_CONTEXT = "java:comp/env/jdbc/airtime";
                javax.naming.Context initialContext = new InitialContext();
                if ( initialContext == null)
                {
                    errorMsg = em.MakeErrorMessage(101, null, "");
                    return null;
                }
                datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
                return datasource.getConnection();
            }
            catch ( Exception e )
            {
                errorMsg = em.MakeErrorMessage(102, e, "");
                return null;
            }
        }
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 38
  • Comments » 61
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Links



    No Child Pages.

Portfolio



    No Child Pages.