background image

Resource Injection

<< DataSource Objects and Connection Pools | Field-Based Injection >>
<< DataSource Objects and Connection Pools | Field-Based Injection >>

Resource Injection

A JDBC connection pool is a group of reusable connections for a particular database. Because
creating each new physical connection is time consuming, the server maintains a pool of
available connections to increase performance. When an application requests a connection, it
obtains one from the pool. When an application closes a connection, the connection is returned
to the pool.
Applications that use the Persistence API specify the DataSource object they are using in the
jta-data-source
element of the persistence.xml file.
<jta-data-source>jdbc/MyOrderDB</jta-data-source>
This is typically the only reference to a JDBC object for a persistence unit. The application code
does not refer to any JDBC objects. For more details, see
"Persistence Units" on page 701
.
Resource Injection
The javax.annotation.Resource annotation is used to declare a reference to a resource.
@Resource
can decorate a class, a field, or a method. The container will inject the resource
referred to by @Resource into the component either at runtime or when the component is
initialized, depending on whether field/method injection or class injection is used. With field
and method-based injection, the container will inject the resource when the application is
initialized. For class-based injection, the resource is looked up by the application at runtime.
@Resource
has the following elements:
name
: The JNDI name of the resource
type
: The Java language type of the resource
authenticationType
: The authentication type to use for the resource
shareable
: Indicates whether the resource can be shared
mappedName
: A non-portable, implementation-specific name to which the resource should
be mapped
description
: The description of the resource
The name element is the JNDI name of the resource, and is optional for field- and method-based
injection. For field-based injection, the default name is the field name qualified by the class
name. For method-based injection, the default name is the JavaBeans property name based on
the method qualified by the class name. The name element must be specified for class-based
injection.
Resource Injection
Chapter 34 · Resource Connections
1011