ejb@glassfish.java.net

Re[2]: Defining DataSource JNDI name in ejb-jar.xml

From: AKostylev <akostylev_at_ipian.kazan.ru>
Date: Thu, 31 Aug 2006 10:05:13 +0400

Yes, I've create a global jndi name (in appserver) 'jdbc/testIP'. So
when I use it this way:
        @Resource(name = "jdbc/testIP") protected DataSource ds;

        all works fine.
        But when I try to use ejb-jar.xml field injection I get
        NullPointerException (as I described before).
        What could be the problem?

        I've submitted a bug about this:
https://glassfish.dev.java.net/issues/show_bug.cgi?id=1032
        


> Yes, you can use descriptor alone to inject into a field/property. That
> would save you the code doing lookup.

> What's your sun-ejb-jar.xml like? There normally should be a
> <resource-ref> element to map the local name (jdbc/testIP) to its global
> jndi name (the one you created in appserver). If the global jndi name
> is the same as resource reference name (jdbc/testIP), then you may skip
> this mapping.

> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application
> Server 9.0 EJB 3.0//EN"
> "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
> <sun-ejb-jar>
> <enterprise-beans>
> <unique-id>0</unique-id>
> <ejb>
> <ejb-name>xxx</ejb-name>
> <jndi-name>xxx</jndi-name>
> <resource-ref>
> <res-ref-name>xxx</res-ref-name>
> <jndi-name>xxx</jndi-name>
> </resource-ref>

> Cheng




> AKostylev wrote:

>>But as I understand I can use injection without using annotation,
>>defining it in ejb-jar.xml.
>>Here is the sample:
>>
>>@Stateless
>>public class FacadeBean implements Facade
>>{
>> private DataSource ds;
>> public void testDataSource()
>> {
>> try
>> {
>> ds.getConnection();
>> }
>> catch (SQLException e)
>> {
>> throw new EJBException(e);
>> }
>> }
>>...
>>}
>>
>><?xml version="1.0" encoding="UTF-8"?>
>><ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
>>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>>http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
>> <description>J2EE5 test application </description>
>> <display-name>Facade</display-name>
>> <enterprise-beans>
>> <session>
>> <ejb-name>Facade</ejb-name>
>> <remote>ru.amfitel.test.bean.Facade</remote>
>> <ejb-class>ru.amfitel.test.bean.FacadeBean</ejb-class>
>> <session-type>Stateless</session-type>
>> <resource-ref>
>> <res-ref-name>jdbc/testIP</res-ref-name>
>> <res-type>javax.sql.DataSource</res-type>
>> <injection-target>
>> <injection-target-class>ru.amfitel.test.bean.FacadeBean</injection-target-class>
>> <injection-target-name>ds</injection-target-name>
>> </injection-target>
>> </resource-ref>
>> </session>
>> </enterprise-beans>
>></ejb-jar>
>>
>>Should this work due to EJB 3.0 specs?
>>At this time I get a NullPointerException (ds is not initialized):
>>Caused by: java.lang.NullPointerException
>> at ru.amfitel.test.bean.FacadeBean.testDataSource(FacadeBean.java:90)
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
>>java:39)
>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
>>sorImpl.java:25)
>> at java.lang.reflect.Method.invoke(Method.java:585)
>> at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(
>>EJBSecurityManager.java:1050)
>> at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:165
>>)
>> at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseConta
>>iner.java:2788)
>> at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:387
>>0)
>> at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInv
>>ocationHandler.java:190)
>>
>>What is wrong?
>>Thank you.
>>
>>
>>
>>
>>
>>>If you need to declare a resource-ref in ejb-jar.xml, then annotation in
>>>java code doesn't simplify anything. In that case, I would suggest you
>>>not use @Resource in bean class.
>>>
>>>
>>
>>
>>
>>>To use <resource-ref> in ejb-jar.xml, it's the same as in previous
>>>versions of ejb. Your FacadeBean will need to lookup the declared
>>>resource:
>>>
>>>
>>
>>
>>
>>>DataSource ds = (DataSource) sessionContext.lookup("jdbc/my-ds");
>>>
>>>
>>
>>
>>
>>><?xml version="1.0" encoding="UTF-8"?>
>>><ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
>>>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
>>>xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>>>http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
>>><enterprise-beans>
>>> <session>
>>> <ejb-name>FacadeBean</ejb-name>
>>> <business-remote>com.xxx.FacadeRemote</business-remote>
>>> <ejb-class>com.xxx.FacadeBean</ejb-class>
>>> <session-type>Stateless</session-type>
>>> <transaction-type>Container</transaction-type>
>>> <resource-ref>
>>> <description>description</description>
>>> <res-ref-name>jdbc/my-ds</res-ref-name>
>>> <res-type>javax.sql.DataSource</res-type>
>>> <res-auth>Container</res-auth>
>>> <res-sharing-scope>Shareable</res-sharing-scope>
>>> </resource-ref>
>>> </session>
>>></enterprise-beans>
>>>
>>>
>>
>>
>>
>>
>>>Cheng
>>>
>>>
>>
>>
>>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ejb-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: ejb-help_at_glassfish.dev.java.net



-- 
С уважением,
 AKostylev                          mailto:akostylev_at_ipian.kazan.ru