Summary of hierarchical structure of database operations

Source: Internet
Author: User

In the read and write operation of persistent data, it often involves the operation of database and cache, and the transaction operation of multiple tables is often necessary because of the business need. The idea of hierarchical design based on structure we often need to design this series of operations in a layered format.

The main responsibilities of each layer, as well as how to deal with the exception, is to continue to throw up, or in the layer to do the exception conversion processing, as well as transactions in the exception of the cache processing needs some thinking.

Take personal experience as an example:
Persistent operations are often divided into 3 layers: DAO layer, manager layer, service layer
which
DAO Layer: Direct Database read and write operations.
When an exception occurs, the caller can find the cause of the DB release error (SQL syntax error or repeated insertion of the same primary key, etc.) based on the captured exception information.

publicqueryListthrows DAOException;

Manager layer: Changes the database to read and write with the cache.
The way I do it

insert:     insertDB(something)    //直接操作dbquery:     something = getSomethingFromCache   //首先读缓存,不存在时读db     ifNULL) {         something = getSomethingFromDB         putSomethingToDB(something)     }     return somethingupdate:    updateDB(something)    //更新db,同时失效缓存中 脏数据    invalidCache(something)delete:     deleteDB(something)   //删除db中数据,同时失效缓存中 脏数据     invalidCache(something)

When an exception occurs in this layer, it is thrown out and not processed. My previous practice, in this layer multiple exception processing, such as when an exception I return null, the disadvantage is that the caller to NULL can not make a further distinction (whether the exception or the DB and the cache does not exist resulting in null), so that fault tolerance performance degradation, for example, if can be caused by an exception, Multiple attempts can be made, and when the data does not exist, it needs to be returned immediately for processing. After all, the manager layer is relatively low-level, said to the upper callers to provide as much information as possible to do fault-tolerant error.

Service layer: In this layer, the transaction operations of the table are often done according to the needs of the business.

update:            doInTransaction(TransactionStatus status) {                try {                    Long tableId1 = table1.insert(something1);                    Long tableId2 = table2.insert(something2);                } catch (Exception e) {                    status.setRollbackOnly();                }            }

When an exception occurs in this layer, it is often converted according to business requirements, such as the failure of the query to return the details of the failure to the caller for troubleshooting purposes.

queryResult.isSuccessfalse;queryResult.msg = “db error”;queryResult.msg = “不存在”;

Summary of hierarchical structure of database operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.