Develop data access applications
We can use data access applications to manipulate data from outside sources for use within the application serving environment.
We can access data in various ways:
- using standard or extended APIs
- using container-managed persistence beans
- using bean-managed persistence beans, session beans, or web components.
- using Service Data Objects (SDO)
- Decide how to implement data access.
The EJB programming model provides several distinct server-side component types: entity, session, and message-driven beans, and servlets. Of these types, entity beans are typically used to model business components in an application. Entity beans have both state and behavior.
The state of entity beans is persistent and is stored in a database. As changes are made to an entity bean, its state is kept in synchronization with the database record representing the bean. There are two types of entity beans provided by the EJB model and these two types differ in the mechanism used to provide persistence. These two types of entity beans are container-managed persistence (CMP) beans and bean-managed persistence (BMP) beans.
- With BMP beans, the developer manually produces code to manage the persistent state of the bean.
- With CMP beans, the EJB container manages the persistent state of the bean. Persistent state management is a complex and difficult task; using CMP beans allows the developer to concentrate on business logic by delegating persistence behavior to the container.
Typical examples of CMP beans are Customer, Account, and so on. Because CMP beans are objects, their data (state) is accessed using field accessors. For example, a Customer entity bean is likely to have fields such as name and phoneNumber. These pieces of data are accessed using the accessor methods getName()/setName() and getPhoneNumber()/setPhoneNumber(). As a developer, you are not concerned with how this data is eventually stored and retrieved from the backend database and can assume that the integrity of the data is maintained by the container.
See the topic, Develop enterprise beans for information on developing entity beans.
Tips:
- (dist)(zos) To maximize the efficiency of application requests to relational databases, consider using Structured Query Language in Java (SQLJ) when developing BMP and CMP beans. This option is available for applications that use the DB2 JDBC Universal Driver to access DB2 databases.
(zos) The only exception to this driver requirement applies to SQLJ-backed BMP beans that access DB2 for z/OS ; this schema requires the DB2 for z/OS Legacy Driver ( required for the DB2 for z/OS Local JDBC Provider RRS).
- (dist)(zos) Also consider using cursor holdability for potential performance gains; see the topic, JDBC application cursor holdability support, for details.
- (iseries) Consider using cursor holdability to maximize the efficiency of application requests to relational databases;
An alternative to developing entity beans is using the Service Data Objects (SDO) framework, which is a unified framework for data application development. With SDO, we do not need to be familiar with a technology-specific API in order to access and utilize data. You need to know only one API, the SDO API, which lets you work with data from multiple sources, including relational databases, entity EJB components, XML pages, web services, the Java Connector Architecture, JSP, and more.
- Look up a data source or connection factory using a resource reference. See the topic, Looking up data sources with resource references for relational access for more information. Do not perform this step if you work with CMP beans. The EJB container handles this process for CMP beans.
To run applications on WebSphere Application Server, the code must use resource references to logically named data sources or connection factories. Mapping the resource references to actual resources is usually done at assembly time. The Application Server administrator configures those resources.
- For relational database access, administrators configure a JDBC provider and associated data sources, which work with the embedded WebSphere Relational Resource Adapter.
- For non-relational database access, administrators install a Java EE Connector Architecture (JCA) resource adapter onto an application server and configure associated connection factories.
Generic work context implementation provides a mechanism for a resource adapter to control the contexts in which instances of work submitted by the resource adapter to the product work manager for execution are executed. By submitting a work instance that implements the WorkContextProvider interface, the resource adapter can propagate various types of context to the WAS. The application server then, if it supports the propagated context type, sets the provided context as the execution context of the work instance during its execution.
- Get a connection to a data source or a connection factory. See the "Getting connections" section of the topic, Connection life cycle for details.) Do not perform this step if you work with CMP beans. The EJB container handles this process for CMP beans.
The connection management architecture for both relational and procedural access to enterprise information systems (EIS) is based on the Java EE Connector Architecture (JCA) specification. The Connection Manager (CM), which pools and manages connections within an application server, is capable of managing connections obtained through both resource adapters (RAs) defined by the JCA specification, and data sources defined by the JDBC Extensions Specification.
- (zos) Use thread identity to assign an owner to the connection. See the topic, Using thread identity, support for more information.
Subtopics
- (zos) Use thread identity support
The thread identity function allows us to assign a thread identifier as an owner of a connection when you first obtain the connection. This function only applies to Java EE Connector Architecture (JCA) resource adapters and Relational Resource Adapter (RRA) wrappered JDBC providers that support the use of thread identity for connection ownership.
- Porting a Liberty application that uses MongoDB to the full profile
We can port a Liberty application that uses MongoDB to run on the WAS full profile.
- Use Bean Validation in RAR modules
WebSphere Application Server validates resource archive (RAR) JavaBeans constraints in compliance with the Java Connector Architecture (JCA) version 1.6 specification.
- JCA 1.6 support for annotations in RAR modules
The Java Connector Architecture (JCA) Version 1.6 specification adds support for Java annotations in resource archive (RAR) modules. Annotations are a means of specifying metadata, or configuration data, for a RAR module in the class files that make up the module.
- Extensions to data access APIs
If a single data access API does not provide a complete solution for the applications, use WebSphere Application Server extensions to achieve interoperability between both the JCA and JDBC APIs.
- Recreating database tables from the exported table data definition language
When the WAS deployment tooling deploys an EJB jar file containing container-managed persistence (CMP) enterprise beans, it selects the target database and creates a corresponding Table.ddl file. This file contains the SQL statement necessary to generate the database table for the CMP beans.
- Container managed persistence bean associated technologies
WebSphere Application Server delivers container-managed persistence (CMP) services beyond the standards set by the EJB specification.
- Manipulating synchronization of entity beans and datastores
We can indicate that a particular EJB type should not synchronize its state to persistent storage prior to each findBy invocation by using environment variables or a marker interface.
- Avoiding ejbStore invocations on non-modified EntityBean instances
We can configure the EntityBean instances to bypass an invocation of the ejbStore method if they have not been modified during the current transaction.
- Resource reference benefits
WebSphere Application Server requires your code to reference application server resources (such as data sources or J2C connection factories) through logical names, rather than access the resources directly in the JNDI name space. These logical names are called resource references.
- Access data using Java EE Connector Architecture connectors
To access data from a Java EE Connector Architecture (JCA) compliant application in WebSphere Application Server, you configure and use resource adapters and connection factories.
- (zos) JDBC application development tips
By using best practices to help maximize the efficiency of JDBC queries, we can potentially increase application performance.
- JDBC application cursor holdability support
The cursor holdability feature can reduce the overhead of JDBC interaction with the relational database, thereby helping to increase application performance.
- Data access bean types
For easy data access programming, WebSphere Application Server provides a special class library that implements many methods of the JDBC API for you. The library is essentially a set of Service Data Objects (SDO).
- Access data from application clients
To access a database directly from a Java EE application client, you retrieve a javax.sql.DataSource object from a resource reference configured in the client deployment descriptor. This resource reference is configured as part of the deployment descriptor for the client application, and provides a reference to a pre-configured data source object.
- (dist)(zos) Service Data Objects version 2.1.1
Service Data Objects (SDO) is a framework for data application development that provides an architecture and APIs. The product includes an implementation of SDO 2.1.1 interfaces. After the product is installed and SDO is enabled, SDO 2.1.1 becomes the default SDO implementation when programming with SCA or SDO interfaces.
- Data access with Service DataObjects, API versions 1.0 and 2.01
The Service Data Objects (SDO) framework is a data-centric, disconnected, XML-integrated, data access mechanism that provides a source-independent result set.
- Use the Java Database Connectivity data mediator service for data access
The following steps demonstrate how to create the metadata for a JDBC data mediator service (DMS), as well as how to instantiate the DMS dataGraph.
- Use the EJB data mediator service for data access
The following steps use code samples to describe a simple instance of how to create the EJB data mediator service (DMS) metadata.
- Develop a custom DataStoreHelper class
Apply the WebSphere extension, GenericDataStoreHelper class, to create our own data store helper for data sources that the application server does not support. With this helper class, your JDBC configuration can use database-specific functions during transactions.
Related concepts
(zos) Connection thread identity
Exceptions pertaining to data access Connection life cycle
Related tasks
Establishing custom finder SQL dynamic enhancement server-wide Establishing custom finder SQL dynamic enhancement on a set of beans Establishing custom finder SQL dynamic enhancement for specific custom finders Disable custom finder SQL dynamic enhancement for custom finders on a specific bean (dist)(zos) Deploy SQLJ applications
Change the error detection model to use the Exception Checking Model Develop enterprise beans
CMP connection factories collection