Why use EJB?
Board bridge http://www.jdon.com 2004/03/08
First of all, we must be clear, why do you want to use J2EE? What is the advantage of J2EE? The main reason for using J2EE is multilayer structure, and traditional two-layer C / S structures are difficult to maintain, the stability is extremely poor, and the interface code and database code are confused together, and the multi-layer structure makes the interface and the database completely separated, and There is a technique such as the middleware, as shown below:
Web EJB can make a real multi-layer structure
Why use EJB I just think this is not a topic of discussion, because EJB is an important part of J2EE, which can be said that there is no EJB J2EE is just a web system, such a system is very easy to lose most of the multi-layer structure (carefully Think about what is the difference between JavaBeans and traditional two-layer structure?).
Of course, it is possible to take hierarchical division between Javabeans, such as Hibernate calculated data persistence, some Javabeans are business core layers, but because they are ordinary javabeans, this division does not have a mandatory and obvious iconic, so The system has replaced the main crew or designer, and it may be very confusing by the new programmers.
Let's first take a look at how the J2EE system containing EJB clearly expresses levels. As shown below:
The web is completely only an implementation of the MVC mode. The key business core is implemented in the service layer of EJB. The advantage is that the Web is only responsible for the interface-related part, because if it is a smart client, such as swing or J2ME, do not need It is easy to replace if any business core is modified. Similarly, providing Web Services feature, is only modified in web layer, and does not involve EJB modifications, which also guarantees the stability of the system, ensuring system upgrade and future scalability.
If you do not use EJB, the business core implemented in the EJB service layer will be implemented by ordinary JavaBeans, and what architecture or design can ensure that JavaBeans responsible for MVC is clearly separated, and how to ensure that new programmers will not Destroy and disrupt your carefully layout JavaBeans architecture?
EJB provides performance optimization support
The most important thing is performance problems. Since some people in the country, some people are bending EJB, it is considered that EJB performance is low. In fact, this is a very superficial understanding, we first look at how to improve performance in the general Java environment.
Assume a JavaBeans as A, then usually use this javabeans command as follows:
A a = new a ();
However, in a high visual environment, New A () is actually consumes system performance at a time, so some objects can be prepared in advance when the software system is started, so that the system is running, from these already generated objects One of the pools, so that there is no need to perform new, saving overhead, improving performance, and therefore, the real mature performance solution requires support for object pools.
In a pure web structure (that is, only in the Tomat environment), such as Struts Hibernate, etc., unless you do it, it is generally not subject to the technical support, so their performance can only be considered Demo. The performance of the demo version is not able to withstand a lot of capacity, and cannot be called a mature system. Therefore, we have studied the mature open source web systems such as Jive, OFbize, Liferay, etc., they all have their own objects in the Web. And the cache pool. Is the object pool and cache mechanism be a J2EE? Of course, what must be made by all mature systems, how will the Windows system be getting off?
It is not a simple thing to develop objects and caching mechanisms that need to be a simple matter, which needs to have a deep level of multithreading and synchronous locks, which is also a very deep Java research branch, so you can throw away Your customer is anxious, carefully study its own object pool and cache pool.
However, the EJB container (such as JBoss) has provided a target pool and a cache mechanism, so the performance of the stateless session bean without the transaction mechanism must be stronger than normal javabeans. The EJB container not only provides objects and caches in the stand-alone, but can implement dynamic load balancing across servers, these do not need developers to develop any software code, the structure is as follows:
EJB components provide real reusable frameworks
Each JAR package represents an EJB component, and a system can consist of multiple reusable EJB components, such as: tree structure EJB components; self-adder EJB components; user data EJB components, etc., such EJB components can be like building blocks Matching in most applications, the development efficiency of the system is improved to ensure the quality of development.
The following figure is an EJB component diagram applied to a new specific system. In this new application, since the previously reusable EJB components used, new development work is basically focused on interface design and process arrangements:
EJB provides a transaction mechanism
The transaction mechanism is very important for some key transactions, such as ATM machine withdraw, withdrawal has multiple actions: modify the database and count money, if there is any one of these links, then other implementation must be restored, otherwise It will appear, the payee did not get the money, but the incredible thing that had been deducted on the card.
The transaction mechanism provided by EJB is very comprehensive, but the shortcomings brought by the transaction mechanism are the performance of performance, so some people think that EJB is very heavy, because in practical applications, some user systems may not require a transaction mechanism, just need EJB to provide Performance optimization mechanism, so, if you use EJB, it is like a person to make things, and he is in addition to what I want, there is something I don't want.
Unless you are a perfectism, in general enterprise applications or database system applications, EJB will not make a heavy burden for you.
CMP unique advantages
Open source and some database persistent layer technology worshipers, always slamming CMP, thinking that CMP is slowly useless, the actual biggest problem is their design and use issues.
Since the EJB container (such as JBoss) implements a transaction mechanism for the CMP, CMP is particularly suitable for multiple users to update the same data source at the same time, and the strict transaction integrity of CMP guarantees multiple users to operate a data simultaneously. When recording, the performance optimization and data integrity can be guaranteed. If this data record is a status flag of a software system, its status affects a lot of links in the system, and the importance of state changes is self-evident. If there is no transaction integrity support, your software system becomes larger, it will become a variety of impossible logic errors. The program logic is correct, then where is the problem? Out of data integrity.
Since each CMP has a cache in the memory, in practical applications, if you use the CMP batch read database data, tens of thousands of queries are filled, and the memory is full of tens of thousands of CMP caches. If you are not properly set at this time your EJB container (If used by JBoss default configuration), then JVM's garbage collection mechanism will start frequently, causing your system to slow or even crash, this is why some people attack CMP slow reasons, in fact, they use improper use, or not properly configured EJB container CMP cache.
For this case, the DAO JDBC mode is recommended according to J2EE core mode.
summary
Unless you are very deeply designed to design the design mode, you can fix the JavaBeans in your own system to fix the layers, and you are tireless to develop a target pool, and you can submit a transaction mechanism such as JTA. You can choose no EJB. The web structure is like JIVE, OFBIZ. Of course, there is a premise, the boss does not understand or very challenging (company and technologies with IBM Sun Microsoft).
Don't be misleaded by TSS's fanatic opening sources, they have time to ensure what they like, as a professional J2EE programmer, learn from J2EE standard, do not think, as long as it uses J2EE Such as JSP or JavaBeans is aware of peace, think that your own system is J2EE.
Of course, I don't say that the pure web system does not implement multi-layer structure, but at least in many ways, there is no Web EJB structure is perfect and clear, so EJB is not part of J2EE to ignore, but the main important part, important business functions The core is packaged in EJB, and the opposite web layer is a secondary, and the interface-related level.