Database access method and device

By creating database management classes and database table instances in the database system, and generating and managing access task queues, the problems of poor scalability and errors during database access are solved, achieving flexible expansion and improved security.

CN114911457BActive Publication Date: 2025-11-14BEIJING FEINNO COMM TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202110188010.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-02-08
Publication Date
2025-11-14
Estimated Expiration
2041-02-08

AI Technical Summary

Technical Problem

In existing technologies, database access processes are prone to errors. Existing technologies cannot efficiently extend database operation interfaces, cannot support multi-threaded access, have poor scalability, and are prone to access errors.

Method used

By creating database management classes and database table instances in the database system, database access tasks are generated and added to a specified task queue. The database management class reads tasks from the task queue and executes them, thus achieving serialized processing, avoiding access errors, and supporting multi-threaded reuse.

Benefits of technology

It enables flexible expansion of database operation interfaces, avoids access errors, improves access security, simplifies application logic, and saves resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114911457B_ABST
    Figure CN114911457B_ABST
Patent Text Reader

Abstract

This application discloses a database access method and apparatus. In this method, a pre-created database table instance receives database access operations sent by an application. When the database table instance receives a database access operation from the application, a database access task is generated and added to a specified task queue. A pre-encapsulated database management class reads the target access task from the specified task queue and executes the target access task according to the task type to obtain the database access result. This method allows for convenient and flexible extension of the database operation interface, avoids access errors, and improves access security. Furthermore, access tasks generated in different scenarios can reuse a single worker thread, eliminating the need to develop separate database access threads for each application. This simplifies the internal processing logic of the application, reduces application size, saves development costs, simplifies the access logic of the database system, and conserves database system resources.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of database technology, and in particular to a database access method and apparatus. Background Technology

[0002] Databases are used to store data according to certain rules and support users to access the stored data as needed, such as performing operations like querying, adding, deleting, and modifying. With the rapid development of computer technology, databases face higher requirements in terms of read / write speed, data availability, security, and scalability.

[0003] In existing technologies, to enable various applications to access databases, dedicated worker threads for database access are typically deployed in each application. When an application needs to access the database, it does so through these worker threads. For example, a chat application might deploy a first thread for querying chat history data and a second thread for querying friend data. When a user needs to retrieve chat history data, the chat application starts the first thread, which accesses the corresponding database instance using the chat history query interface provided by the database to obtain the chat history data. When a user needs to retrieve friend data, the chat application starts the second thread, which accesses the corresponding database instance using the friend data query interface provided by the database to obtain the friend data.

[0004] With the above implementation, if the application or the system containing the application has multiple worker threads accessing the database, access errors are highly likely to occur. Furthermore, if it is necessary to extend the database operation interface, existing worker threads need to be modified or new worker threads need to be developed, making it inconvenient to extend the database operation interface. Summary of the Invention

[0005] This application provides a database access method and apparatus. This method allows for convenient and flexible extension of the database operation interface and enables serial execution of database access operations, avoiding access errors and improving access security. Furthermore, various types of access operations generated in different scenarios can reuse the same working thread, thereby simplifying access logic and saving database system resources.

[0006] Firstly, this application provides a database access method applied to a database system, the method comprising:

[0007] Pre-create database management classes and database table instances;

[0008] The database table instance receives a database access operation sent by the application, generates a database access task, and adds the access task to a specified task queue. The task type of the access task corresponds to the operation type of the access operation.

[0009] The database management class reads the target access task from the specified task queue, executes the target access task according to the task type of the target access task, and obtains the database access result corresponding to the target access operation. The target access operation is the access operation corresponding to the target access task.

[0010] Secondly, this application also provides a database access device, which is applied to a database system, and the device includes:

[0011] Create a module to pre-create database management classes and database table instances;

[0012] The first processing module is used to receive database access operations sent by the application through the database table instance, generate database access tasks, and add the access tasks to a specified task queue. The task type of the access task corresponds to the operation type of the access operation.

[0013] The second processing module is used to read the target access task from the specified task queue through the database management class, execute the target access task according to the task type of the target access task, and obtain the database access result corresponding to the target access operation, wherein the target access operation is the access operation corresponding to the target access task.

[0014] As can be seen from the above technical solutions, this application provides a database access method and apparatus. The method includes: receiving a database access operation sent by an application through a pre-created database table instance; when the database table instance receives the database access operation sent by the application, generating a database access task and adding the access task to a specified task queue, wherein the task type of the access task corresponds to the operation type of the access operation; reading the access task from the specified task queue through a pre-encapsulated database management class; executing the target access task according to the task type of the read target access task to obtain the database access result corresponding to the target access operation, wherein the target access operation is the access operation corresponding to the target access task. This method can conveniently and flexibly extend the database operation interface, and can serialize database access operations, avoiding access errors and improving access security. In addition, access tasks of various types generated in different scenarios can reuse the same working thread, eliminating the need to develop separate threads for database access in each application. This simplifies the internal processing logic of the application, reduces the application size, saves development costs, simplifies the access logic of the database system, and saves database system resources. Attached Figure Description

[0015] To more clearly illustrate the technical solution of this application, the drawings used in the embodiments will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0016] Figure 1 This is a flowchart illustrating a database access method according to an exemplary embodiment of this application;

[0017] Figure 2 This is a block diagram illustrating a database access device according to an exemplary embodiment of this application. Detailed Implementation

[0018] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of this application.

[0019] This application provides a database access method, wherein the database can be SQL (Structured Query Language). This method can be applied to a database system (DBS), which typically consists of software, a database, and a data administrator. The software mainly includes an operating system, various host languages, and utilities, such as a computer operating system.

[0020] To implement the database access method provided in this application, database table instances and database management classes are pre-built in the database system.

[0021] This database table instance inherits from the database base class and provides at least one operation interface for receiving database access operations, i.e., a database operation interface. In this way, applications can communicate with the corresponding database table instance by accessing the specified database operation interface, thereby sending database access operations to the database table instance.

[0022] In some embodiments, the corresponding database interface type and database operation interface can be determined based on the operation type of the database access operation, and then a database table instance can be created based on the determined database operation interface. Specifically, when a specified database operation interface is obtained, the process of creating a database table instance may include: extending the database table base class according to the specified database operation interface, for example, using a class that implements the database operation interface as a subclass of the database table base class; and then instantiating the extended database table base class to obtain the database table instance. Here, the specified database operation interface corresponds to the specified database access operation. For example, a chat history data query interface corresponds to a chat history data query operation generated in a chat application.

[0023] It should be noted that the database access operation type mentioned in the embodiments of this application can be a broad type of operation, such as query operation, insert operation, delete operation, and modify operation, which are four different types of operations.

[0024] Furthermore, the database access operation types mentioned in the embodiments of this application can also be narrowly defined, including user-defined operation types or classifications of specific operations for specific scenarios, etc. For example, chat history query, chat history deletion, friend data query, and friend data deletion operations in a chat application scenario are defined as four different types of operations. As another example, chat history query operations in a chat application and browsing history query operations in a browser application are defined as two different types of operations.

[0025] In one implementation, a database table instance can be created for a specific type of database access operation. In this implementation, one or more specific applications involved in the relevant type of database access operation can send a database access operation to the corresponding database table instance based on the appropriate database operation interface when they need to access the database.

[0026] For example, for query operations, a database table instance 1 is created, which provides a data query interface; for insert operations, a database table instance 2 is created, which provides a data insert interface. Thus, when the application needs to query data, it sends a query operation to database table instance 1; when the application needs to insert data, it sends an insert operation to database table instance 2.

[0027] For example, to query chat history, a database table instance 1 is created, which provides a chat history query interface; to query friend data, another database table instance 2 is created, which provides a friend data query interface. Thus, when a user needs to query chat history, the chat application sends a chat history query operation to database table instance 1 based on the chat history query interface provided by database table instance 1; when a user needs to query friend data, the chat application sends a friend data query operation to database table instance 2 based on the friend data query interface provided by database table instance 2.

[0028] In another implementation, a database table instance can be created for each application. This database table instance needs to provide all the database operation interfaces required by that application. Taking a chat application as an example, assuming the chat application involves querying, inserting, and deleting chat history data, then database table instance 3 created for the chat application should provide chat history data query interfaces, chat history data insertion interfaces, and chat history data deletion interfaces. When the chat application needs to query chat history data, it sends a chat history data query operation to database table instance 3 using the chat history data query interface provided by database table instance 3; when the chat application needs to insert chat history data, it sends a chat history data insertion operation to database table instance 3 using the chat history data insertion interface provided by database table instance 3; when the chat application needs to delete chat history data, it sends a chat history data deletion operation to database table instance 3 using the chat history data deletion interface provided by database table instance 3.

[0029] As can be seen, in this implementation, when an application needs to access the database, it will connect to the corresponding database table instance based on one or more database operation interfaces.

[0030] In addition, the database table instance is also used to generate a database access task when it receives a database access operation sent by the application, and add the access task to a specified task queue. The task type of the access task corresponds to the operation type of the access operation.

[0031] It should be noted that the type of the generated access task varies depending on the type of the access operation; that is, the task type of the access task corresponds to the type of the access operation. For example, when an access operation to query chat history data is received from a chat application, a Statement execution task is generated.

[0032] In this application, the task type may include native SQL statement execution tasks, Statement execution tasks, transaction execution tasks, and related SQL statement execution tasks, etc.

[0033] In this application, the database management class encapsulates database access logic, including but not limited to instantiating the database, registering database tables, generating and maintaining a specified task queue, and starting a database worker thread. The database worker thread reads the target access task from the specified task queue and executes the target access task according to its task type to obtain the database access result corresponding to the target access operation.

[0034] It should be noted that the number of designated task queues can be one or more. In an implementation with one designated task queue, this queue is used to cache access tasks sent by all database table instances in the database system. In an implementation with multiple designated task queues, a mapping between database table instances for each task queue can be pre-established, allowing each task queue to cache access tasks sent by its corresponding database table instance. Furthermore, in an implementation with multiple designated task queues, different task queues can cache different types of access tasks. For example, the first task queue can receive raw SQL statement execution tasks, the second task queue can receive Statement execution tasks, and so on.

[0035] Furthermore, in implementations with multiple designated task queues, each corresponding to different task types, priorities can be pre-assigned to each queue. This allows database worker threads to prioritize reading and executing higher-priority access tasks. Continuing with the previous example, if the first task queue has the highest priority, the database worker thread will prioritize executing the native SQL statement execution task.

[0036] Figure 1 This is a flowchart illustrating a database access method according to an exemplary embodiment of this application, such as... Figure 1 As shown, the method may include:

[0037] S100, pre-creates database management classes and database table instances;

[0038] The database management class encapsulates database access logic, including but not limited to instantiating the database, registering database tables, generating and maintaining a specified task queue, and starting database worker threads.

[0039] This database table instance inherits from the database base class and provides at least one operation interface for receiving database access operations, i.e., a database operation interface. In this way, applications can communicate with the corresponding database table instance by accessing the specified database operation interface, thereby sending database access operations to the database table instance.

[0040] S110, receive the database access operation sent by the application through the data table instance, generate a database access task, and add the access task to a specified task queue. The task type of the access task corresponds to the operation type of the access operation.

[0041] In S110, a database table instance provides one or more types of database operation interfaces. Applications only need to send database access operations to the database table instance to initiate database access, without needing to develop separate worker threads for data access within the application. Furthermore, the database operation interface can be extended by extending the database table base class, offering high flexibility and cost savings.

[0042] S120, the target access task is read from the specified task queue through the database management class, and the target access task is executed according to the task type of the target access task to obtain the database access result corresponding to the target access operation. The target access operation is the access operation corresponding to the target access task.

[0043] In S120, based on the "first-in, first-out" principle, access tasks in the specified task queue are read and executed, so that the access operations generated in the system are serialized, which can effectively avoid access errors.

[0044] It is worth noting that when a data access operation is received, generating a database access task and adding the access task to a specified task queue can enable all access tasks to be scheduled in a unified manner, serialize all access tasks, ensure the orderliness of the access process, improve access security, and avoid data access errors.

[0045] In addition, by reading and executing access tasks in the queue through database worker threads, access tasks generated in different scenarios can reuse the same worker thread. This eliminates the need to develop separate threads for database access in each application, which simplifies the internal processing logic of the application, reduces the application size, saves development costs, simplifies the access logic of the database system, and saves database system resources.

[0046] In this embodiment, the database table instance provides at least one callback interface. The database worker thread obtains the access result corresponding to the target access operation by executing the target access task, and returns the access result to the database table instance according to the callback interface provided by the database table instance. The application then reads the access result corresponding to the access operation from the callback interface provided by the database table instance.

[0047] In one implementation, the database table instance provides two callback interfaces: `Ondo` and `Ondo2`. `Ondo2` is specifically for tasks involving the execution of join SQL statements, while `Ondo` handles other types of access tasks. Based on this, the database worker thread needs to determine the corresponding callback interface based on the task type of the target access task and return the access result to the database table instance according to the appropriate callback interface. For example, if the target access task is a join SQL statement execution task, the access result is returned according to the `Ondo2` interface; if the target access task is not a join SQL statement execution task, the access result is returned according to the `Ondo` interface. The application then needs to determine the corresponding callback interface based on the operation type of the access operation to read the access result from the corresponding callback interface.

[0048] As can be seen from the above embodiments, this application provides a database access method, which includes: when a database table instance receives a database access operation sent by an application, generating a database access task and adding the access task to a specified task queue, wherein the task type of the access task corresponds to the operation type of the access operation; reading a target access task from the specified task queue through a database management class, executing the target access task according to the task type of the read target access task, and obtaining the database access result corresponding to the target access operation, wherein the target access operation is the access operation corresponding to the target access task. This method can conveniently and flexibly extend the database operation interface, and can serialize database access operations, avoiding access errors and improving access security. In addition, access tasks of various types generated in different scenarios can reuse the same working thread, eliminating the need to develop separate threads for database access in each application, which simplifies the internal processing logic of the application, reduces the application size, saves development costs, simplifies the access logic of the database system, and saves database system resources.

[0049] Based on the database access method provided in this application, embodiments of this application also provide a database access device, such as... Figure 2 As shown, the device may include:

[0050] Create module 200 to pre-create database management classes and database table instances;

[0051] The first processing module 210 is used to receive a database access operation sent by the application through the database table instance, generate a database access task, and add the access task to a specified task queue. The task type of the access task corresponds to the operation type of the access operation.

[0052] The second processing module 220 is used to read a target access task from the specified task queue through the database management class, execute the target access task according to the task type of the target access task, and obtain the database access result corresponding to the target access operation, wherein the target access operation is the access operation corresponding to the target access task.

[0053] In some embodiments, the creation module is specifically used to: extend a database table base class according to a specified database operation interface, wherein the specified database operation interface corresponds to the operation type of a specified database access operation; and instantiate the extended database table base class to obtain the database table instance.

[0054] In some embodiments, the second processing module is specifically used to: instantiate a database using a pre-encapsulated database management class; and to start a database worker thread, sequentially read access tasks from the specified task queue using the database worker thread, execute the target access task according to the task type of the read target access task, and return the access result to the database table instance.

[0055] In some embodiments, the database table instance provides at least one callback interface, and the second processing module is specifically used to: send the access result to the callback interface through the database management class, so that the database table instance obtains the access result according to the callback interface.

[0056] In some embodiments, the number of designated task queues is multiple, one designated task queue is used to store one type of access task, and different designated task queues have different priorities;

[0057] When there are access tasks to be executed in multiple specified task queues, the second processing module is specifically used to prioritize reading the target access task from the specified task queue with the highest priority among the multiple specified task queues.

[0058] As can be seen from the above embodiments, this application provides a database access device, which includes: a creation module, a first processing module, and a second processing module. When the first processing module receives a database access operation sent by an application through the database table instance, it generates a database access task and adds the access task to a specified task queue. The task type of the access task corresponds to the operation type of the access operation. The second processing module is used to read the target access task from the specified task queue through the database management class, execute the target access task according to the task type of the target access task, and obtain the database access result corresponding to the target access operation. The target access operation is the access operation corresponding to the target access task. This device can conveniently and flexibly extend the database operation interface and can serialize database access operations, avoiding access errors and improving access security. In addition, access tasks of various types generated in different scenarios can reuse the same working thread, eliminating the need to develop separate threads for database access in each application. This simplifies the internal processing logic of the application, reduces the application size, saves development costs, simplifies the access logic of the database system, and saves database system resources.

[0059] In a specific implementation, the present invention also provides a computer storage medium, wherein the computer storage medium may store a program, and the program, when executed, may include some or all of the steps of the various embodiments of the database access method provided by the present invention. The storage medium may be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.

[0060] Those skilled in the art will clearly understand that the techniques in the embodiments of the present invention can be implemented using software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solutions in the embodiments of the present invention, or the parts that contribute to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in various embodiments or certain parts of the embodiments of the present invention.

[0061] The same or similar parts between the various embodiments in this specification can be referred to mutually. In particular, the device embodiments are basically similar to the method embodiments, so the description is relatively simple, and the relevant parts can be referred to the description in the method embodiments.

[0062] The embodiments of the present invention described above do not constitute a limitation on the scope of protection of the present invention.

Claims

1. A database access method, characterized in that, Applied to a database system, the method includes: A database management class and a database table instance are created in advance; wherein, the creation of the database table instance includes: extending the database table base class according to a specified database operation interface, wherein the specified database operation interface corresponds to a specified database access operation; and instantiating the extended database table base class to obtain the database table instance; The database table instance receives database access operations sent by the application, generates database access tasks, and adds the access tasks to a specified task queue. The task type of the access task corresponds to the operation type of the access operation. The database management class reads the target access task from the specified task queue, executes the target access task according to the task type of the target access task, and obtains the database access result corresponding to the target access operation. The target access operation is the access operation corresponding to the target access task. The number of designated task queues is multiple, each designated task queue is used to store one type of access task, and different designated task queues have different priorities; When there are access tasks to be executed in multiple specified task queues, the database management class prioritizes reading the target access task from the specified task queue with the highest priority among the multiple specified task queues; The database is instantiated using the database management class. In addition, a database worker thread is started, which sequentially reads the target access task from the specified task queue, executes the target access task according to the task type of the target access task, and returns the access result to the database table instance. The database table instance provides at least one callback interface. Returning the access result to the database table instance includes: sending the access result to the callback interface so that the database table instance obtains the access result according to the callback interface.

2. A database access device, characterized in that, The apparatus is used in a database system, and the apparatus includes: A creation module is used to pre-create database management classes and database table instances; specifically, the creation module is used to: extend the database table base class according to a specified database operation interface, wherein the specified database operation interface corresponds to a specified database access operation; and instantiate the extended database table base class to obtain the database table instance; The first processing module is used to receive database access operations sent by the application through the database table instance, generate database access tasks, and add the access tasks to a specified task queue. The task type of the access task corresponds to the operation type of the access operation. The second processing module is used to read the target access task from the specified task queue through the database management class, execute the target access task according to the task type of the target access task, and obtain the database access result corresponding to the target access operation, wherein the target access operation is the access operation corresponding to the target task. The number of designated task queues is multiple, each designated task queue is used to store one type of access task, and different designated task queues have different priorities; When there are access tasks to be executed in multiple specified task queues, the second processing module is specifically used to prioritize reading the target access task from the specified task queue with the highest priority among the multiple specified task queues; and to instantiate the database through a pre-encapsulated database management class; In addition, a database worker thread is started, which sequentially reads access tasks from the specified task queue, executes the target access task according to the task type of the read target access task, and returns the access result to the database table instance. The database table instance provides at least one callback interface, and the second processing module is specifically used for: The access result is sent to the callback interface through the database management class, so that the database table instance can obtain the access result according to the callback interface.

Citation Information

Patent Citations

  • Database operation logic verification method, device, equipment and readable storage medium

    CN110515924A

  • Database access method, apparatus and device

    CN110795485A