Skip to main content

How to use @resource with a connector

2 replies [Last post]
qwaihd
Offline
Joined: 2005-09-07

I have written a JCA 1.5 connector and have successfully deployed it.

How do i use the @Resource annotation to load the connectionfactory in a web application?

Below are some code snippets of config files and the sevlet.

I have tried @Resource, @Resource(name="EISconnectionFactory"), and @Resource(name="eis/myconnector") but always get null for cf.

eis/myconnector is the JNDI name of the connector resource
protocol.ra.MyConnectionFactory is the Connection Definition of the connection pool

<br />
-- test.java --<br />
...<br />
@Resource<br />
private MyConnectionFactory cf;<br />
...<br />

<br />
-- web.xml --<br />
...</p>
<p>  EISconnectionFactory</p>
<p>    protocol.ra.MyConnectionFactory</p>
<p>  Container<br />
  Shareable</p>
<p>...<br />

<br />
-- sun-web.xml --<br />
...</p>
<p>  EISconnectionFactory<br />
  eis/myconnector</p>
<p>...<br />

Any help will be appreciated.
Thanks
Stephen

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ksak
Offline
Joined: 2005-05-20

Hi Stephen,

If you declare it via @Resource, you don't need to define a resource-ref
in web.xml. The name() attribute is the logical name relative to the
web application component namespace (java:comp/env), which is
*different* from the global JNDI name of the target connection factory.

You can either map the dependency using the mappedName() attribute
as follows :

@Resource(name="myeisref", mappedName="eis/myconnector")

or via sun-web.xml as follows :

@Resource(name="myeisref")


myeisref
eis/myconnector

If you don't specify the name() attribute when declaring
@Resource, it defaults to
/
e.g. com.acme.test/cf

For that reason, if you declared

@Resource
private MyConnectionFactory cf;

You would need the following sun-web.xml to perform the mapping
to global JNDI name.


com.acme.test/cf
eis/myconnector

--ken

qwaihd
Offline
Joined: 2005-09-07

Thanks