content:
What is data persistence? J2EE's Persistent Entity Bean Advantage Entity Bean Disadvantage Session Bean and JDBC Session Bean / JDBC Advantage Session Bean / JDBC Disadvantages Call Conclusion Reference About the author
related information:
The Abcs Of EJBSJ2EE Explore Series EJB Best Practice Series Advanced Database Operations with JDBCJDBC 3.0 What new features? DeveloperWorks Toolbox Subscribe
There is also a Java area:
Teaching tools and product code and components all articles practical skills
Which J2EE technology is best for your company?
Level: Intermediate
Kyle Gabhart (Kyle@gabhart.com) consultant, Gabhart Consulting, June 2003
The J2EE platform provides a rich set of options for managing enterprise data persistence, but how to choose options for your architecture? Kyle Gabhart introduces J2EE's best data persistence technology - entity beans, JDBC, and JDO - and compare them in several different environments.
Data persistence is the most difficult aspects in corporate development. A enterprise data persistence solution must provide a rapid client transaction, ensuring data integrity over time, and the data continues to exist when the system crashes and network failures occurs. In the two parts: J2EE Explorer series, we will focus on J2EE technology that helps you create a reliable data persistence solution for your business architecture. We will start this topic by briefting data persistence in an enterprise application, and then discussing a variety of technical options in more detail. In this section, we combine a combination of a SINGLE-STOP solution to a SINGLE-STOP solution to a more complex (but more robust) session bean with Java Database Connectivity, JDBC) code. In the next section, we will compare the Java Data Object, JDO and entity beans.
What is data persistence?
Data is the most important aspect of any computer application. The core of the computer application is to enable someone or another computer system to access its data. In an enterprise environment, data must not only be accessible (ie, connection with the user interface and managed by a series of business rules), but also must remain. The persistent data storage is the data store that can still exist even in the case of the server crash.
Persistent data is existing outside the application's activity memory, usually in a database or a flat file system. Although persistent data is read into instantaneous memory for use or modification, it is always written to the external data store for long-term storage. The United States National Standards and Technology Institute (see Resources) Defines three levels of persistent data:
Some persistent data is a persistent data structure that only updates the latest version. Hiring data is a data structure that retains its old version; that is, the previous version and the current version may be queried. Full lasting data is a persistent data structure that maintains all versions of their data and allows for a lasting data structure that updates these versions.
Most business applications provide at least part of persistent data. This type of persistence is easily destroyed in the middle of the transaction or even in the middle of the system failure, which will cause data incomplete and often destroyed. On the other hand, in the persistent data implementation, the system is interrupted or failed to "rollback" respond, and the data state is rolled back to the previously known good configuration. The lasting data is achieved in the enterprise architecture and database management system (DBMS). Fully lasting data is very rare. A few examples of full persistent data implementation include: logging file system, VMS file system (such as VAX and Mac OS X) and concurrent version control system (CVS). Persistence in J2EE
The information age emphasizes the use of distributed enterprise computing platforms. On this platform, you must protect the data at any cost and make it continuously, even if you face a network failure, memory leaks and server crashes. In order to maintain this persistence, the application component must be able to handle concurrency, connection management, data integrity, and synchronization. All three data management techniques of J2EE have handled these features for developers, but each technique is slightly different during processing.
Entity Bean, which provides robust data persistence. The BEAN container handles most of the data integrity, resource management, and concurrency, so that developers pay attention to business logic and data processing, not these low-level details. When using bean management persistence (BMP) entity bean, developers write persistent code and the container determines when the code is executed. When container management persistence (CMP) entity bean, the container generates persistence code and manages persistence logic. JDBC, when combined with the session bean, it provides easy EJB development and deployment with the platform, without resource usage and memory overhead like EJB technology. Like the BMP entity bean, the solution requires the developer to write a persistent code. Unlike BMP Bean, it also requires developers to write persistent logic. Thus, the developer is responsible for determining when the data is kept in the data store and when the data is stored from the data. Java data objects are the latest persistence mechanism. JDO provides object-oriented persistent data storage. Developers use Pojo (unformatted ordinary Java objects, Plain ORDINARY JAVA Object) to load and store persistent data.
We will discuss the advantages and disadvantages of the entity bean vs. session bean and JDBC combination in the rest of the article.
Advantages of entity beans
When it comes to enterprise-level data persistence, the entity bean has the following advantages:
standardization. The EJB specification defines a set of interfaces unrelated to the supplier, and the J2EE vendor can implement these interfaces to support entity beans. This standardization allows for the development of best practices and shortening the adaptation period when employing new developers. Because basic component architecture and design patterns know, it is easy to find qualified talents to achieve them. Container management services. As we discussed in the first two articles of this series, EJB container management services provide great benefits to handle corporate functions such as security, transaction, connection and resource management. Transparent persistence. The service idea of container management is further strengthened in the CMP entity bean. Here, the container also automatically manages persistence semantics. When using BMP entity beans, developers must write persistence logic and the container determine when to call the method defined by the developer. At the same time, when using CMP and BMP entity beans, the container determines when to keep the bean state and how to ensure data integrity and concurrency in the underlying data. Business support. Developers have coarse-grained control for CMP transactions (including / excluding / excluding) of the Isolation level, transactional needs, and methods. These controls have fine-grained control over BMP transactions. Implemented. In both cases, the container management transaction and determines if a given transaction should be submitted. Component-based design. Entity beans are designed to include components that are configured with deployment descriptors, which can be deployed to any J2EE application server without changing any code. The land of the entity bean However, when architect designers, developers and consultants start using entity beans, its appeal and magic begin to quickly disappear. When the EJB 1.1 specification is widely implemented, the fact that the use of entity beans must be cautious. Although they still represent the powerful components of data persistence - model architecture, they consume too much server resources are all well known. EJB 2.0 specification has eased some of these issues. Although many people still need to recover from the influence of the EJB 1.1 entity bean, the entity bean nowcomes more reliable, which is a more feasible solution compared to the past. As the local interface appears, the CMP capability has increased, and the J2EE supplier has more experience in implementing the EJB specification, and the entity bean has once again become a feasible data persistence mechanism throughout the industry. In 1999, when the J2EE and EJB specification were introduced for the first time, the entity bean was touted as a superior enterprise component, which thoroughly changed the development, maintenance and portability of the enterprise application. The industry is excited that entity beans represents a non-trouble, automatic persistence mechanism.
In short, entity Bean benefits from standardization and industry best practices, simplifies certain complexities developed and provides an eye-catching component-based design.
Disadvantages of entity beans
Although entity Bean does have a list of impressive features, this has a lot of adding, but we still have to consider their shortcomings, the shortcomings are as follows:
Designed complex. The cost of service and automatic transparent persistence of containers is high. They bring complexity to application design at several levels. First, in order to avoid network overhead and comply with business rules, the entity bean is almost always accessed through session beans. Therefore, each transaction involves at least two enterprise beans (often more). The more components involved, the more complex the architecture, coding and maintenance of the architecture. Second, there is an automatic operating cost. The container is a bit similar to a black box with magic. As long as it considers appropriate, it will call the bean callback method, which can choose to create and destroy the bean instance, activate and passivate the bean, and store its status into a persistent data storage or from it. Application code cannot control the way these things happen or time. From a positive aspect, the function of the container has reduced the number of problems to be considered when writing business logic. From a negative aspect, the response of the container on the load condition and the data request mode is unpredictable, so a large number of scheme-based load testing must be added during the development process. Longer build cycle. Due to the complexity of enterprise beans and (especially) entity beans, the time spent by one iteration (Design / Construction / Test / Integration / Test / Test / Deployment) is longer than that of the comparable Java persistence solution. three times. Response time. According to the load on the server and the relative size of the requested entity bean, the query of the entity bean may have a response time that is not up to standard. Entity Beans is limited to the particle size of the bean instance. Either you have to load the entire bean, or you can't load beans at all. This particle size will cause the architecture to become more complicated, because the only choice is to keep the bean as it is, or the data is divided into smaller entities, thereby making the system architecture more complicated. Resource usage. All enterprise beans are all resource accounts. The entity bean is probably the largest heel. Although this will change the application design mode and how the supplier effectively designs its physical Bean implementation, the entity bean still tends to consume a large number of server resources. In summary, the disadvantages of entity beans are complexity and longer build cycles, making the design and development of systems that contain their systems more difficult. In production, entity Bean is extremely slow because of the proportion of the resource and the concurrent request of the big entity.
Session bean and JDBC
The popularity of the stateless session bean is not as big as the entity bean (see the side bar). In fact, in terms of popularity and function, stateless session beans have been stable and reliable since 1999 (issued EJB specification in the year). They produce excellent performance and effective resource coexistence, and is an important work program component of the EJB series. The stability and presence of stateless session beans make them an excellent candidate for managing persistent enterprise data.
Status Session Bean is often combined with JDBC to create a reliable persistent data management solution. In the following two sections, we will measure the advantages and disadvantages of the solution, but we will not explore the details of these two technology itself. If you need to learn more about the stateful session bean or JDBC, see Resources.
How it works because session beans have no inherent data access mechanism, so they must connect to the factory using the Explorer. The resource manager is a component of the J2EE container, which manages the entire lifecycle of a particular type of resource, including connectivity, transaction support, and any necessary network protocols that make the actual connection possible. The connection plant is an object for creating a connection to the resource manager. The EJB specification defines the resource manager of JDBC, JMS, JavaMail, and JCA resources.
In a persistent architecture based on session bean and JDBC, session beans commission all access commands to the JDBC layer. When receiving the call, the session bean uses JDBC to obtain an object that implements the Javax.sql.DataSource interface. Then, the returned object acts as the resource manager factory of the Java.sql.connection object (defined by the JDBC API), which implements the connection to the database management system. Once the Connection object, the remaining persistence code and business logic (query, update, stored procedure call, result set browsing and transaction submission / rollback, etc.) are pure JDBC. Advantages of session bean / jdbc
Session Bean and JDBC have become an excellent combination of persistence of enterprise data. The most recognized advantages of this combination are as follows:
Simple design. From the perspective of architecture design, direct processing data management is much simpler than using entity beans. Fine-grained control. Because session beans are universal work program components, they allow developers to fully control the entire persistence process, including cache, persistence, concurrency, synchronization, and others. In contrast, the CMP entity bean does not allow developers to control persistence mechanisms, while BMP entity beans only enable developers to define what should occur, but cannot be defined when or what should occur. mature. JDBC has a seven years! The entity bean has only three years so far. JDBC reliability and best practices are very valuable assets for the development of J2EE persistence mechanism. speed. Because developers fully control the data access mechanism used in session beans, data access and persistence logic can be optimized for some tasks. This can generate extremely fast response time due to direct and purposeful operations.
In summary, the combination of session beans and JDBC enables developers to have fine particle size control of data management, this combination utilizes strong and mature data management technology, supports functional optimization, and contains all of them into a relatively simple component. In the architecture.
Disadvantages of Session Bean / JDBC
So far, it is talking about the advantages of meeting the session bean and JDBC combination, but it also has some shortcomings. The disadvantage is as follows:
Realize complicated. Although the architecture design of this system is quite simple, the actual session bean is often very complicated. If the application's data requirements are quite complicated, you may not be able to manage database connections, secure data integrity, and correctly handle transaction semantics. Developers often need to implement some types of caches while ensuring optimal performance. Constructing this cache mechanism makes the system development and maintenance further complicates. Non-induced transactionality. The entity bean is essentially a transactional component, which has configurable transaction semanties; and the session bean is not. When transaction semantics directly encodes the application code, developers must be careful to ensure that business rules, stream control, and transaction integrity of each function are protected and can be fault tolerant. These details are processed by the container in the development of the entity bean. Persistence is not automatic or not guaranteed. In the entity bean operation, the container processes the persistence of the BEAN state and ensures that this data is protected for later use. For session beans, maintaining data is secure, long-term data storage is the responsibility of developers.
In short, three key issues combined with JDBC: Bean is often complex; the session bean has no intrinsic transaction; the persistence mechanism is not automatic or not guaranteed.
Call
Although there is still enough, J2EE architects have begun to announce that pure stateful session beans with original JDBC calls is the safest and most respected data persistence mechanism. This is not primarily because the combination is better than entity Bean solutions (both have their own strengths), actually because it has driving. When the entity bean quickly stood out and quickly lost, the accepted level of the session bean and JDBC has stably accumulated as time. Regardless of what the current trend is, carefully weighing the advantages of entity beans and session beans and JDBC portfolios are always worthwhile. The following list identifies four critical aspects of these two data persistence solutions:
Read / write needs. The data that needs to be read frequently and the data from not change or occasionally changes is preferably handled by session beans with JDBC. Development work will be simply straight and produce excellent response time. If the data needs frequent updates and supports many concurrent requests (so there are many concurrent changes), the entity bean is an option. In the face of concurrency requests for data, it is too difficult to overcome the complexity involved in ensuring data integrity, synchronization, and frequent persistence, it is too difficult to overcome, and it is not worth spending time and effort to create it. Business support. The CMP entity bean makes developers don't have to care about the transaction environment. All transaction details are declared in the deployment descriptor of the bean. If you accept this level of control, the CMP entity bean will undoubtedly provide the easiest solution. If more control is needed, the BMP bean allows developers to define the operations that should be taken without having to write business rules for this type of operation. For maximum levels of control, session beans should be used. Session Bean manage complex transactions involving CMP and BMP entity beans, as well as a minority JDBC call. Listing time. The CMP entity bean is clearly the only listing time in all J2EE persistence mechanisms. Declare data types and names, define deployment settings, and then be responsible for the rest of the application server and supplier tools. It is difficult to say which BMP entity bean and session bean and JDBC combination can be row the second fast solution. On the one hand, BMP will be faster because the container is on behalf of Bean to provide so many lifecycle services. On the other hand, the session bean will lead because they do not have BMP, so the build / test / deployment cycle is shorter. Finally, in these three solutions for your specific items, they are sorted only part of the entire comparison process. This rating must also be targeted for the next category (resource usage). Resource usage. The entity bean is named due to a large number of resources (especially when concurrently requested concurrent requests). In contrast, the session bean and JDBC data source connections are very lightweight, only a small amount of server resources. For more information on this, please read the "J2EE Technologies for THE STATELESS NETWORK" in the first article "J2EE TECHNOLOGIES for THE STATELESS NETWORK".
Conclude
In the third part of the J2EE Explorer series, we combine the entity beans with session beans and JDBC comparisons for data persistence. The scenario discussed here does not cover all situations, but they represent some of the most common usage of entity EJB components and session EJB components.
Next month, we will continue to study the J2EE data persistence mechanism by comparing entity beans object objects (Java Data Object). I wish you a happy "exploration"!