users@glassfish.java.net

Re: NPE - java.lang.reflect.UndeclaredThrowableException

From: Sahoo <sanjeeb.sahoo_at_oracle.com>
Date: Fri, 03 Feb 2012 19:23:32 +0530

See response inline...

On Friday 03 February 2012 05:10 PM, forums_at_java.net wrote:
> deployed example with correct JNDI name
This is definitely working because of existence of following line...
> [#|2012-02-03T13:12:38.008+0200|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=91;_ThreadName=Thread-2;|JDBCTestBundleActivator:
>
> com.sun.gjc.spi.jdbc40.ConnectionWrapper40_at_31888179|#]

> deployed example with wrong JNDI name ----------------
This is also working as you realized yourself because of the missing line:
> This is missing: com.sun.gjc.spi.jdbc40.ConnectionWrapper40_at_31888179
>
> So this example works.

>
> my example with correct JNDI name -------------------------
The following output confirms that you have correctly setup datasource
in GlassFish.
> I suppose that the
> error is somewhere else.
Indeed. As I told you in one of my previous email, you are not setting
the datasource in SessionHandleImpl object. You perhaps misunderstood
that comment. You are registering the service in your activator.start()
like this:
         bc.registerService(SessionHandle.class.getName(), new
SessionHandleImpl(), new Properties());

Who will set datasource field in this object that you are registering as
service? So change SessionHandleApp this way:

     public void start(final BundleContext bc) throws Exception {
         debug("Activator started");
         // don't register the service here. Set it inside the
serviceTracker.serviceAdded().

             @Override
             public Object addingService(ServiceReference reference)
{
                 DataSource ds = (DataSource)
bc.getService(reference);
                 try {
                     debug(ds.getConnection().toString());
*SessionHandle sh = new SessionHandleImpl();
                  sh.ds = ds; // set the ds field.*
*bc.registerService(SessionHandle.class.getName(), sh, null);*
                 } catch (SQLException e) {
                 }
                 return super.addingService(reference);
             }
             @Override
             public void removedService(ServiceReference reference,
                     Object service) {
                 super.removedService(reference, service);
             }
         };


You can remove @Resource annotation from the ds field in SessionHandleImpl.

HTH,
Sahoo