J2EE project data persistence layer design
Data Persistence Layer design goal is to provide a high level throughout the project, unity, security, and concurrent data persistence mechanism. Achieve the various data persistence programming job, and for the system to provide services Business Logic Tier. Data persistence layer provides the data access methods, so that other programmers can avoid manual programming to access the data persistence layer (Persistene layer), its focus on business logic development, and be able to reuse in different projects mapping framework, greatly simplifies the data Zeng, delete, change, search and other functions of the development process without the loss of the natural advantages of multi-layer structure, inheritance unique extension of J2EE scalability and scalability.
Data Persistence Layer 1 and ORM mapping framework
The author engaged in the project the data in the persistence layer is based on J2EE architecture and uses Hibernate as a framework for lasting mapping.
Hibernate is a new ORM mapping tools, are lightweight JDBC object package. Hibernate can be used in JDBC can be used for any occasion, such as Java applications, database access code, DAO interface implementation class, or even access the database inside the BMP code. Hibernate not only provides data from the Java type mapping between tables, also provides data query and restore mechanism. Vis-à-vis the use of JDBC and SQL to the database manually, use Hibernate, could greatly reduce the workload of database operations.
Hibernate and JDBC are a closely related, independent object persistence layer framework, can be used with a variety of App Server, Web Server, EJB Container common use, Hibernate compatibility only with the JDBC driver, the underlying database product must have the relationship, but and the use of its Java procedures, App Server has no relationship, there was no compatibility problem. Hibernate can also show that the truth and a variety of Web server or application server, a good integration, and now support almost all popular database servers (up to 16 kinds).
In the more commonly used program data persistence, Hibernate is undoubtedly the most excellent, the following are the various programs lasting comparison.
¨ popular data persistence layer architecture:
Business Layer <-> Session Bean <-> Entity Bean <-> DB
¨ In order to address barriers to alternative performance architecture:
Business Layer <-> DAO <-> JDBC <-> DB
¨ use Hibernate to improve the efficiency of the above architecture the development of the architecture:
Business Layer <-> DAO <-> Hibernate <-> DB
Us on the above three architecture to make the following analysis.
(1) memory consumption: use of the JDBC architecture is undoubtedly the most memory Province, Hibernate Architecture of times, EB worst of architecture.
(2) operating efficiency: If the JDBC code written in a very optimized, then the JDBC architecture to run the most efficient, but the actual project, it is almost impossible, which required a very proficient programmer JDBC, use Batch language Batch adjust the PreapredStatement Size and Fetch Size parameters, and where necessary the use of result set cache and so on. And under normal circumstances are not programmers this. Hibernate Architecture therefore show the fastest operating efficiency. EB's efficiency would be bad architecture very much.
(3) the development of efficiency: there is at Eclipse, JBuilder and other development tools support, for simple projects, EB to develop the most efficient architecture, JDBC times, Hibernate worst. However, in large projects, especially the relationship between persistence layer mapping very complex circumstances, Hibernate efficient amazing, JDBC times, while the EB architecture are likely to fail.
Data Persistence Layer 2 Design
The complexity of application development process are the most headache problem. Whenever an application at an additional feature, it was usually the complexity of the geometric class growth. This complexity often leads to the development process can no longer continue. This is also why many applications are now only Beta version and there is no formal version of the reasons.
Application development experts will have the complexity of the process is divided into two categories, namely, unessential (accidental) and the nature of (essential). The nature of the complexity of the objectives are to address the problem inherent in the complexity, the complexity of unessential are inappropriate because of the selection of development tools and design tools resulting complexity. For a function to determine the procedures, the nature of complexity are identified, rather than the complexity of nature is not unlimited. Therefore, the development of an application in order to more successfully to succeed, it required as much as possible to reduce the complexity of unessential.
Design patterns so that people can more easily reuse successful design and architecture. Would have confirmed the technical interpretation into design mode, there will be new systems developers to more easily understand their design ideas.
A system to measure whether or not a key factor in quality, in addition to be able to meet user demand have the following aspects: First of all, flexibility. Flexibility means that such a structure or model is not dependent on any practical application, it should be with the operating system, the application has nothing to do. Provides an independent structure, can provide maximum reuse. Followed by scalability. With the business expansion, new business increased business logic of natural increase, the system is bound to modify or add the corresponding function module. Again are configurable. Finally, there is security.
Data Persistence Layer Design to adopt a variety of design patterns to maximize the reduction of the system of the module, the coupling between subsystems, making the system relatively easy to extend, and be able to change course, to ensure that the Business Logic Tier persistence layer relative stability, basic Do not need because of the persistence layer to adjust to changes in the logic layer changes.
The author in the project using the following design pattern.
2.1 Overall Architecture - MVC model (Model - View - Controller)
¨ Model (Model): finish mission model contains all the required behavior and data. In the data persistence layer, the model is the value of the object as well as Data Access Objects.
¨ View (View): data persistence layer, the view is the persistence layer with other layers of the value of the exchange of data objects (Transfer Object) and the view helper objects.
¨ controller (Controller): persistence layer needed to control relatively easy, so integrated into the agent in control.
Persistence Layer overall use of MVC pattern, making the entire data persistence layer of the implementation part of the project business logic part of separate, implementation of the interface can make big changes without the need for the appropriate model be modified. In addition, the persistence layer of a sub-system changes, will not affect the other subsystems. Conducive to improving the system stability, maintainability.
2.2 value of the object model (Value Object Pattern)
Value object used to package business object. The corresponding method calls are set up (getter) and retrieval (setter) value of the object. It is arbitrary serializable Java object-oriented, when the client requests for business data Bean, the Bean value object can be constructed, property values used to fill, and put it in accordance with the values passed to the client.
Development projects at the author's data persistence layer architecture, the main value object used in inter-subsystem transfer, exchange of data (Transfer Object) and two aspects of mapping data table (Persistent Object).
Between subsystems in the data transmission and data exchange, use the value object pattern reduces the system be able to maximize data transmission costs. In such a strategy are being targeted delivery is no longer a meaningful data, making the system during the expansion, modification, all part of data transfer between subsystems will not be affected, because each subsystem is only concerned about whether or not required there is value object is passed, and not to care about what is in the end delivery of data.
In the mapping database table, the value object class and subclass posed by tree structure is used to map a database table, the succession of trees through the XML configuration file corresponds to a single database table, which makes the bottom of the relational database table structure can hide the object-oriented models, In addition, because of object-oriented design methods inheritable class, using inheritance tree corresponds to a table of strategy makes it easy to extend the mapping strategy, and can be a complex transformation of data tables value into a number of simple objects that enhance the system's maintainability and modifiability.
2.3 Data Access Objects (DAO)
According to different data sources, data access are also different. According to the type of storage (relational databases, object-oriented database, etc.) and the supplier is different from persistent storage (eg database) access varies widely. When the business component or components that need to access a data source, they can use the API to obtain the appropriate connectivity, as well as the operation of the data source. However, these components are included in the connectivity and data access code will be introduced to these components and data sources of the tight coupling between the implementation. Medium-dependent components such code so that applications from some data source to move to other types of data sources will become very troublesome and difficult, when the data source changes, the components also need to change in order to be able to deal with new types of data source.
The author projects the data persistence layer using Data Access Objects (DAO) to abstract and package all of the data source visit. DAO manages the connection with the data source in order to retrieve and store data, DAO implementation of the data source used to operate the access mechanism, the internal package of Hibenernate data manipulation, transaction processing, session management API package. The outside world depends on the DAO component for its business clients using DAO provides a simpler interface, DAO entirely to the client to hide the implementation details of data sources. Because when the low-level data source implementation changes, DAO provided to the client interface will not change, the use of the DAO design pattern permit adjust to a different storage model, and will not affect their clients or business components, even if the future is no longer used Hibernate Relational Mapping as the framework, the upper-end customer will not be affected in any way. In addition, DAO also serve as a data source components and between the adapter role.
Data persistence layer by adjusting the abstract Factory (Abstract Factory) and the Factory Method pattern (Factory Method) model (which is the creation of 2-type model of the implementation details, see GOF <>),), design patterns so that DAO mode to achieve a high flexibility degrees.
When the bottom of storage with the implementation and change, the strategy can by using the abstract factory pattern implementation. Abstract Factory Factory methods can be based on implementation and the creation, implementation and use of Factory methods. The strategy to provide an abstract DAO factory object, one of the object can be constructed various types of concrete DAO factories, each factory to support a different type of persistent storage implementation. Once you access a particular concrete DAO Factory implementation, you can use it to generate the support and implementation in the implementation of the DAO.
2.4 connection pool, application-level cache and Flyweight mode (to enhance system performance)
¨ Cache (Cache)
For the database, the vendor is often open up in memory to store the corresponding region may be repeatedly accessed data and possible implementation of the statement was repeated in order to make these data at the next visit do not have to be re-submitted to the DBMS request and those statements do not have to next time it is run the compiler again.
Similarly, the data persistence layer to save the use of cache technology has been retrieved from the database out of some commonly used data. Client Access durable layer, persistence layer will first visit the cache that if hit will be able to directly extract data from the cache, otherwise revert to the database to send the command to extract data. This design can significantly improve the speed of data access.
¨ connection pool (Connection Pool)
Chi is a very general concept, and buffer storage has a mechanism similar places, are to reduce the aspect of the visit, but it is more focused on the sharing of resources.
For access to the database is set up to connect the cost of more expensive, therefore, the data persistence layer set up a "Connection Pool" in order to improve access performance. Data persistence layer as the object put to connect the entire system is activated, the first connection pool set up a number of connections have been required to visit the region to connect with the database are changed and the tank connected to the provisional allocation of connection pool for access to use the results back after the visit will be connected to the return. This design eliminates the set up JDBC data sources and connection delay, at the same time application-level data source provides concurrent access.
¨ Flyweight pattern (Flyweight)
The principle of object-oriented language is everything is an object, but if you really use, and sometimes the number of possible objects is very large, such as, database records, each record as if an object, extracting thousands of records, the target number of is a few thousand, this is undoubtedly a very expensive memory. Data Persistence Layer Flyweight pattern design based on a number of element type, packaging type can be shared. This design strategy significantly reduces memory consumption.
2.5 a variety of objects to create patterns - Factory Method (Factory Method)
Factory Method Pattern example of the work will be created with the use of examples of the work separately, that is to say, so that the creation of a large number of examples needed to initialize the job of the constructor from the easy to separate. Only necessary to invoke a unified approach can be the basis of need to create a variety of examples of objects, object creation methods are no longer in use coding to process module, but at the factory to prepare a unified category. This expansion of modifications in the system, the system of Change is the only factory class itself, and absolutely will not affect other objects.
Tags: business logic (RSS), data query (RSS), type mapping (RSS), implementation class (RSS), persistence layer (RSS), java type (RSS), mapping framework (RSS), database operations (RSS), object persistence (RSS), programming job (RSS), compatibility problem (RSS), interface implementation (RSS), layer structure (RSS), persistence mechanism (RSS), project unity (RSS), logic development (RSS), manual programming (RSS), concurrent data (RSS), database product (RSS), applications database (RSS)
Permalink: http://www.codeweblog.com/j2ee-project-data-persistence-layer-design/






















This article is very badly written. Also the article "12 The most important J2EE best practices (transfer)" is horribly written. To improve your site usage, please do not accept such badly written articles.