A method, device and medium for using session temporary table in connection pool

By using the context class to manage connection objects in the connection pool, the problem of unavailability of temporary table resources caused by connection uncertainty is solved. The method of using session temporary tables in the connection pool is implemented, which improves work efficiency and server service capabilities.

CN113568964BActive Publication Date: 2024-09-27INSPUR GENERSOFT CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110864080.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-07-29
Publication Date
2024-09-27
Estimated Expiration
2041-07-29

AI Technical Summary

Technical Problem

When using a session temporary table in a connection pool, the application cannot use the temporary table resources within the same session because the obtained connection is uncertain and the consistency of the connection cannot be guaranteed.

Method used

Obtain the connection object from the connection pool through the execution startup method of the context class and store it in the thread private class to ensure that the application can use the fixed connection object to establish a connection with the session temporary table and transfer the connection object when the business session ends. Return to the connection pool.

Benefits of technology

It enables the use of temporary table resources within the same session while using connection pool technology, improving work efficiency in business scenarios with large amounts of data involving multiple tables and DML operations, simplifying the programming model, and supporting more Customer Service.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113568964B
    Figure CN113568964B
Patent Text Reader

Abstract

The present application discloses a method, device and medium for using a session temporary table in a connection pool. The method includes: determining that a business session is started, executing a startup method in a context class, and the context class uses a thread private class to store data; obtaining a corresponding connection object in a connection pool through a first method of the context class, and storing the connection object in a thread private class; receiving a connection application, obtaining a connection object from a thread private class through a calling method in the context class, so that an application can establish a connection with a session temporary table through the connection object. By providing a context class, the life cycle of a fixed connection used in a business session is controlled. Connection acquisition and return methods are also provided, so that users can obtain fixed connection objects. Basic connection operation methods are also encapsulated to facilitate users to call temporary table related methods.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of software development, and in particular to a method, device and medium for using a session temporary table in a connection pool. Background Art

[0002] In order to reduce the overhead of frequent creation and destruction of database connections and realize connection reuse, connection pool technology is widely used in network projects of various sizes. When the application starts, the connection pool will initialize a certain number of physical connections and put them into the pool. When the application needs database operations, it can dynamically apply for connections from the connection pool and release them after use. For concurrent requests that exceed the number of connections in the connection pool, they should be queued in the request queue. And the application can dynamically increase or decrease the number of connections in the connection pool according to the usage rate of the connections in the pool. Connection pool technology reuses as many memory-consuming resources as possible, greatly saves memory, improves the service efficiency of the server, and can support more customer services. At the same time, it also simplifies the programming mode, so that programmers do not have to pay too much attention to the life cycle of the connection, and focus on the design and development of business logic. Temporary table technology can temporarily save data in memory and support query and operation with the syntax of ordinary database tables. In the face of certain business scenarios with large amounts of data, involving many tables, and containing many DML operations, using temporary tables can greatly improve efficiency. Temporary tables are mainly divided into global temporary tables and session temporary tables. Currently, mainstream database types support temporary tables, but different types of databases support slightly different types of temporary tables.

[0003] When using session temporary tables in the connection pool, problems arise: since the connection that the application obtains from the connection pool each time is uncertain, and the session is bound to the physical connection, the connection obtained multiple times in the connection pool may not be the same, and the temporary table resources in the same session cannot be used.

[0004] Therefore, there is an urgent need for a method that can use session-level temporary tables while using connection pool technology, so as to avoid the tediousness of having to manage connections by oneself due to temporary table requirements. Summary of the invention

[0005] In order to solve the above problem, the present application proposes a method for using a session temporary table in a connection pool, including:

[0006] Determine that the business session is started, and execute the start method in the context class, the context class uses the thread private class to store data; obtain the corresponding connection object in the connection pool through the first method of the context class, and store the connection object in the thread private class; receive a connection application, and obtain the connection object from the thread private class through the calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object.

[0007] In one example, after the application establishes a connection with the session temporary table through the connection object, the method further includes: receiving a return application, returning the connection object to the context class through a return method in the context class, and storing it in the thread private class; confirming that the business session is ended, clearing the data stored in the thread private class through a close method in the context class, and returning the connection object to the connection pool.

[0008] In one example, before obtaining the corresponding connection object in the connection pool through the first method of the context class, the method further includes: confirming the connection object contained in the connection pool; and storing the connection pool in a core interface under the Spring system in the form of a Bean.

[0009] In one example, before receiving the connection application, the method further includes: determining a basic method based on the Spring class, and encapsulating the connection object based on the basic method.

[0010] In one example, before returning the connection object to the context class, the method further includes: determining a preset cleanup strategy, determining table information of the session temporary table, and clearing the session temporary table that complies with the cleanup strategy according to the table information; recording a user name, and storing the deleted content in the thread private class according to the user name.

[0011] In an example, the table information includes the table name, structure, remaining storage space, and usage status of the session temporary table.

[0012] In one example, the method further includes: determining usage request information of a session temporary table from a database, matching a suitable session temporary table according to the request information, and modifying the usage status of the session temporary table to in use.

[0013] In one example, the method further includes: confirming the number of tenants, obtaining the tenant identifier through a service provider mechanism or a reflection mechanism, and obtaining the connection object from a connection pool according to the tenant identifier.

[0014] The present application also proposes a device for using a session temporary table in a connection pool, comprising:

[0015] at least one processor; and a memory in communication with the at least one processor; wherein,

[0016] The memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to execute:

[0017] Determine that the business session is started, and execute the start method in the context class, the context class uses the thread private class to store data; obtain the corresponding connection object in the connection pool through the first method of the context class, and store the connection object in the thread private class; receive a connection application, and obtain the connection object from the thread private class through the calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object.

[0018] The present application also proposes a non-volatile computer storage medium storing computer executable instructions, wherein the computer executable instructions are configured as follows:

[0019] Determine that the business session is started, and execute the start method in the context class, the context class uses the thread private class to store data; obtain the corresponding connection object in the connection pool through the first method of the context class, and store the connection object in the thread private class; receive a connection application, and obtain the connection object from the thread private class through the calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object.

[0020] This application constructs a context class and binds the context class to a connection object, so that users can obtain a fixed connection from a connection pool. This allows users to use temporary table resources in the same session while using connection pool technology. This saves memory, improves server service efficiency, supports more customer services, simplifies programming models, and further improves work efficiency in business scenarios with large amounts of data, many tables, and many DML operations. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0022] Figure 1 A flow chart of a method for using a session temporary table in a connection pool in an embodiment of the present application;

[0023] Figure 2A schematic diagram of a device using a temporary session table in a connection pool in an embodiment of the present application. DETAILED DESCRIPTION

[0024] In order to make the purpose, technical solution and advantages of the present application clearer, the technical solution of the present application will be clearly and completely described below in combination with the specific embodiments of the present application and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.

[0025] The following is a detailed description of the technical solutions provided by the embodiments of the present application in conjunction with the accompanying drawings. The example of the present application is implemented based on SpringBoot+JPA+Hibernate+HikariCP.

[0026] like Figure 1 As shown, the embodiment of the present application provides a method, comprising:

[0027] S101: Determine that a business session is started by executing a start method in a context class, wherein the context class uses a thread-private class to store data.

[0028] When a user starts a business session, the user first needs to call the begin method of the context class to start the preset context class. Here, the context class uses the thread-private class, that is, its own threadlocal object to store data.

[0029] S102: Obtain a corresponding connection object in a connection pool through a first method of the context class, and store the connection object in the thread-private class.

[0030] After the context class is opened, a connection object needs to be obtained from the connection pool. When the application is initialized, the connection pool has created and contains a certain number of connection instances for the application to obtain and use. The user can obtain the corresponding connection object from the connection pool by calling the getConnection method of the context class, and then store the connection object in its own thread private class, that is, the threadlocal object.

[0031] S103: receiving a connection request, and obtaining the connection object from the thread-private class through a calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object.

[0032] When the user needs to use the connection, the context's getDatabase method is called to retrieve the stored connection from the thread-private class, i.e., the threadlocal object, for the user to use. This allows the user to use the connection pool technology and store fixed connection objects through the context, thereby using session-based temporary tables.

[0033] In one embodiment, if other users also want to use the current connection, it is necessary to ensure that the current user returns the connection object. Based on this, when the user ends the occupation of the connection object, the user's application for returning the connection object is received, and the connection object is returned to the context class for processing by calling the requiteDatabase method in the context class, ending the current user's occupation of the connection so that other users can use the current connection object. After the use is over, when the user wants to end the service session, it is necessary to call the end method of the context class, which clears the thread private storage and returns the fixed connection to the connection pool.

[0034] In one embodiment, before obtaining the corresponding connection object in the connection pool through the first method of the context class, it is also necessary to determine the number of connection objects contained in the connection pool and the storage location of the connection pool. In the example of this application, since the Hikari database is applied under the Spring system, the default connection pool is stored in the core interface of Spring in the form of a Bean, so the Bean of the DataSource type can be directly injected, and then the first method of the context class, namely the getConnection method, can be called to obtain the connection object.

[0035] In one embodiment, in order to protect the connection object and facilitate the user to call the relevant methods of the temporary table, and better operate the database, the connection object can be encapsulated with some basic methods. Based on this, before receiving the user's connection application, the basic method based on the spring class can be determined, and the connection object can be encapsulated according to the basic method. In the example of the present application, the basic methods based on JdbcTemplate and NamedParameterJdbcTemplate can be encapsulated for the user to call. So that the user can call the encapsulated object by the native jdbcTemplate method. Of course, it is also possible to consider expanding more types of encapsulation methods, such as based on EntityManager or generating Entity class agents, to facilitate user development. By encapsulating the connection object and the basic operation method, it is more convenient for the user to call the relevant methods of the temporary table.

[0036] In one embodiment, since the data inserted into the session temporary table is only valid in the current session, the temporary table is equivalent to having no data in other sessions, and the data in the table will be cleared only when the session ends. Therefore, before the current user ends the occupation of the connection object and returns the connection object to the context class, the current user can be reminded to delete the data that does not need to be saved, record the user name, and store the table structure and metadata in the user's data dictionary. Before returning the connection object to the context class or connection pool, different cleanup strategies can be defined, such as user-defined deletion, or agreeing on a table name rule, deleting the table that meets the rule in the return action, or other custom strategies. In order to determine whether the temporary table meets the deletion rule, it is necessary to determine the table information of the temporary table.

[0037] Furthermore, the table information may include the table name, structure, remaining storage space, usage status and other related contents of the session temporary table to determine whether the temporary table satisfies the deletion rule.

[0038] Furthermore, when receiving a user's request to use a temporary table, it is necessary to consider the table information of the temporary table, including whether the table structure meets the user's needs, whether the table storage space is sufficient for the user, the current usage status of the table, etc. Based on this, after receiving the user's request to use a temporary table, it is first necessary to match a suitable session temporary table according to the request information sent by the user, and after determining that the session temporary table is allocated to the user, the usage status of the session temporary table is modified to being used, where the usage status at least includes being idle and being used.

[0039] In one embodiment, if it is necessary to support Hibernate's custom extension function for multiple tenants, the process needs to be further improved. Based on this, it is necessary to obtain the tenant identifier through the service provider mechanism, that is, the ServiceProvider mechanism or the reflection mechanism, and then obtain the connection object from the connection pool through the tenant identifier. In the example of the present application, it is necessary to obtain the instance objects of spring.jpa.properties.hibernate.multi_tenant_connection_provider and spring.jpa.properties.hibernate.tenant_identifier_resolver through the ServiceProvider mechanism or the reflection mechanism, and then call the resolveCurrentTenantIdentifier() method of TenantIdentifierResolver to obtain the tenant identifier, and then call the MultiTenantConnectionProvider.getConnection(identifier) ​​method to obtain the Connection object. This method can be abstracted into an interface to facilitate the implementation in more scenarios.

[0040] It should be noted that the examples in this application specification are all implemented based on SpringBoot+JPA+Hibernate+HikariCP, but the methods or ideas of this application can be implemented through other architectures. Only adaptive modifications need to be made, which will not be repeated here.

[0041] like Figure 2 As shown, the embodiment of the present application also provides a device for using a session temporary table in a connection pool, including:

[0042] at least one processor; and,

[0043] a memory communicatively connected to the at least one processor; wherein,

[0044] The memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to:

[0045] Determine that the business session is started by executing a start method in a context class, wherein the context class uses a thread-private class to store data;

[0046] Obtaining a corresponding connection object from a connection pool through a first method of the context class, and storing the connection object in the thread private class;

[0047] A connection application is received, and the connection object is obtained from the thread private class through a calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object.

[0048] The embodiment of the present application further provides a non-volatile computer storage medium storing computer executable instructions, wherein the computer executable instructions are configured as follows:

[0049] Determine that the business session is started by executing a start method in a context class, wherein the context class uses a thread-private class to store data;

[0050] Obtaining a corresponding connection object from a connection pool through a first method of the context class, and storing the connection object in the thread private class;

[0051] A connection application is received, and the connection object is obtained from the thread private class through a calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object.

[0052] Each embodiment in this application is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device and medium embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.

[0053] The devices and media provided in the embodiments of the present application correspond one-to-one to the methods. Therefore, the devices and media also have similar beneficial technical effects as the corresponding methods. Since the beneficial technical effects of the methods have been described in detail above, the beneficial technical effects of the devices and media will not be repeated here.

[0054] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0055] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0056] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0057] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0058] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0059] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0060] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0061] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0062] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.

Claims

1. A method for using session temporary tables in a connection pool. It is characterized in that include: Determine that the business session is started by executing a start method in a context class, wherein the context class uses a thread-private class to store data; Obtaining a corresponding connection object from a connection pool through a first method of the context class, and storing the connection object in the thread private class; Receive a connection request, and obtain the connection object from the thread private class through a calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object; After the application establishes a connection with the session temporary table through the connection object, the method further includes: Receive a return application, return the connection object to the context class through a return method in the context class, and store it in the thread private class; Confirm that the business session is ended, clear the data stored in the thread private class through the close method in the context class, and return the connection object to the connection pool; Before returning the connection object to the context class, the method further includes: Determine a preset cleanup strategy, determine table information of the temporary session table, and clear the temporary session table that complies with the cleanup strategy according to the table information; The user name is recorded, and the deleted content is stored in the thread private class according to the user name.

2. The method according to claim 1, It is characterized in that Before obtaining the corresponding connection object in the connection pool through the first method of the context class, the method further includes: Confirming the connection object contained in the connection pool; The connection pool is stored in a core interface under the Spring system in the form of a Bean.

3. The method according to claim 1, It is characterized in that Before receiving the connection application, the method further includes: A basic method based on the Spring class is determined, and the connection object is encapsulated based on the basic method.

4. The method according to claim 1, It is characterized in that The table information includes the table name, structure, remaining storage space, and usage status of the session temporary table.

5. The method according to claim 4, It is characterized in that The method further comprises: Determine the use request information of the session temporary table from the database, match the appropriate session temporary table according to the request information, and modify the use state of the session temporary table to use.

6. The method according to claim 1, It is characterized in that The method further comprises: The number of tenants is confirmed, a tenant identifier is obtained through a service provider mechanism or a reflection mechanism, and the connection object is obtained from a connection pool according to the tenant identifier.

7. A device for using a session temporary table in a connection pool, It is characterized in that include: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to execute: Determine that the business session is started by executing a start method in a context class, wherein the context class uses a thread-private class to store data; Obtaining a corresponding connection object from a connection pool through a first method of the context class, and storing the connection object in the thread private class; Receive a connection request, and obtain the connection object from the thread private class through a calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object; After the application establishes a connection with the session temporary table through the connection object, a return application is received, and the connection object is returned to the context class through a return method in the context class, and stored in the thread private class; Confirm that the business session is ended, clear the data stored in the thread private class through the close method in the context class, and return the connection object to the connection pool; Before returning the connection object to the context class, determining a preset cleanup strategy, determining table information of the session temporary table, and clearing the session temporary table that complies with the cleanup strategy according to the table information; The user name is recorded, and the deleted content is stored in the thread private class according to the user name.

8. A non-volatile computer storage medium storing computer executable instructions, It is characterized in that The computer executable instructions are configured to: Determine that the business session is started by executing a start method in a context class, wherein the context class uses a thread-private class to store data; Obtaining a corresponding connection object from a connection pool through a first method of the context class, and storing the connection object in the thread private class; Receive a connection request, and obtain the connection object from the thread private class through a calling method in the context class, so that the application establishes a connection with the session temporary table through the connection object; After the application establishes a connection with the session temporary table through the connection object, a return application is received, and the connection object is returned to the context class through a return method in the context class, and stored in the thread private class; Confirm that the business session is ended, clear the data stored in the thread private class through the close method in the context class, and return the connection object to the connection pool; Before returning the connection object to the context class, determining a preset cleanup strategy, determining table information of the session temporary table, and clearing the session temporary table that complies with the cleanup strategy according to the table information; The user name is recorded, and the deleted content is stored in the thread private class according to the user name.