CN109977096A - A kind of database connection management method and device - Google Patents
A kind of database connection management method and device Download PDFInfo
- Publication number
- CN109977096A CN109977096A CN201910163071.6A CN201910163071A CN109977096A CN 109977096 A CN109977096 A CN 109977096A CN 201910163071 A CN201910163071 A CN 201910163071A CN 109977096 A CN109977096 A CN 109977096A
- Authority
- CN
- China
- Prior art keywords
- database connection
- database
- duration
- application
- occupied
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/21—Design, administration or maintenance of databases
Landscapes
- Engineering & Computer Science (AREA)
- Databases & Information Systems (AREA)
- Theoretical Computer Science (AREA)
- Data Mining & Analysis (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
针对现有技术中,不能自动感知数据库连接池泄露现象,且数据库连接池泄露发生后导致生产服务中断的问题。本发明公开了一种数据库连接管理方法及装置。该方法包括:当数据库连接被应用占用时,确定所述数据库连接被所述应用占用的持续时长;若确定所述持续时长大于或等于第一预设时长,且未检验到所述数据库连接有传输内容,则解除所述应用对所述数据库连接的占用。
In view of the problem in the prior art that the leakage of the database connection pool cannot be automatically sensed, and the production service is interrupted after the leakage of the database connection pool occurs. The invention discloses a database connection management method and device. The method includes: when a database connection is occupied by an application, determining the duration that the database connection is occupied by the application; if it is determined that the duration is greater than or equal to a first preset duration, and it is not checked that the database connection has After the content is transmitted, the application's occupation of the database connection is released.
Description
技术领域technical field
本发明涉及数据库领域,尤其涉及一种数据库连接管理方法及装置。The invention relates to the field of databases, and in particular, to a database connection management method and device.
背景技术Background technique
应用在执行时,常常需要与数据库进行连接,以获取需要的数据,完成相应业务。现有技术中一种手段是,先将数据库连接放在数据库连接池中,应用需要使用数据库连接时,直接从数据库连接池中获取。When the application is executed, it often needs to connect with the database to obtain the required data and complete the corresponding business. One method in the prior art is to first put the database connection in the database connection pool, and directly obtain the database connection from the database connection pool when the application needs to use the database connection.
然而由于应用执行异常等原因,当应用使用数据库连接完毕后,也不将数据库连接归还至数据库连接池,这种现象称为数据库连接池泄露,当数据库连接池泄露频繁出现时,会造成数据库连接池中的数据库连接使用殆尽,造成后续应用连接数据库的请求无法得到服务。现有技术中,不能自动感知数据库连接池泄露现象,需要人力投入,查看应用状态与后台日志,当发生数据库连接池泄露后,采用重启服务器的方法,导致生产服务中断。However, due to abnormal application execution and other reasons, when the application uses the database connection, it does not return the database connection to the database connection pool. This phenomenon is called database connection pool leakage. When database connection pool leakage occurs frequently, it will cause database connection. The database connections in the pool are exhausted, causing subsequent application requests to connect to the database to fail to be serviced. In the prior art, the leakage of database connection pool cannot be automatically sensed, and human input is required to check application status and background logs. When database connection pool leakage occurs, the server is restarted, resulting in interruption of production services.
因此,现有技术中,不能自动感知数据库连接池泄露现象,且数据库连接池泄露发生后导致生产服务中断,是一个亟待解决的问题。Therefore, in the prior art, the phenomenon of database connection pool leakage cannot be automatically sensed, and production services are interrupted after the database connection pool leakage occurs, which is an urgent problem to be solved.
发明内容SUMMARY OF THE INVENTION
本申请实施例提供了一种数据库连接管理方法及装置,解决了现有技术中,不能自动感知数据库连接池泄露现象,且数据库连接池泄露发生后导致生产服务中断的问题。The embodiments of the present application provide a database connection management method and device, which solve the problem in the prior art that the database connection pool leakage phenomenon cannot be automatically sensed, and the production service is interrupted after the database connection pool leakage occurs.
本发明实施例提供一种数据库连接管理方法,该方法包括:The embodiment of the present invention provides a database connection management method, the method includes:
当数据库连接被应用占用时,确定所述数据库连接被所述应用占用的持续时长;When the database connection is occupied by the application, determining the duration that the database connection is occupied by the application;
若确定所述持续时长大于或等于第一预设时长,且未检验到所述数据库连接有传输内容,则解除所述应用对所述数据库连接的占用。If it is determined that the duration is greater than or equal to the first preset duration, and it is not checked that the database connection has transmission content, the application will release the occupation of the database connection.
可选的,所述确定所述数据库连接被所述应用占用的持续时长,包括:Optionally, the determining the duration that the database connection is occupied by the application includes:
所述数据库连接被所述应用占用时,被记录了占用的起始时刻;When the database connection is occupied by the application, the start time of the occupancy is recorded;
按照第一预设周期获取当前时刻,并将当前时刻减去所述起始时刻的时长,作为所述持续时长;所述第一预设周期小于所述第一预设时长。The current time is acquired according to a first preset period, and the duration of the starting time is subtracted from the current time as the duration; the first preset period is less than the first preset time period.
可选的,所述未检验到所述数据库连接有传输内容,还包括:Optionally, the failure to verify that there is transmission content connected to the database further includes:
若检验到所述数据库连接有传输内容,则用当前时刻替换所述起始时刻。If it is checked that there is transmission content connected to the database, the starting time is replaced with the current time.
可选的,按照以下方式检验所述数据库连接是否有传输内容:Optionally, check whether the database connection has transmission content in the following manner:
对所述数据库连接进行切面拦截,并将切面拦截的内容存储至第一变量;所述第一变量的默认值为空;Aspect interception is performed on the database connection, and the content intercepted by the aspect is stored in a first variable; the default value of the first variable is empty;
若在第二预设时长内,所述第一变量的值为空,则确定所述数据库连接无传输内容;否则,确定所述数据库连接有传输内容。If the value of the first variable is empty within the second preset time period, it is determined that the database connection has no transmission content; otherwise, it is determined that the database connection has transmission content.
可选的,在确定所述持续时长小于所述第一预设时长的时间段内,或者检验到所述数据库连接有传输内容,则保持所述数据库连接不变。Optionally, within a time period where the duration is determined to be less than the first preset duration, or it is verified that the database connection has transmission content, the database connection is kept unchanged.
可选的,若所述持续时长大于或等于所述第一预设时长,检验所述数据库连接是否有传输内容。Optionally, if the duration is greater than or equal to the first preset duration, check whether the database connection has transmission content.
本发明实施例中,数据库连接被应用占用时记录了被占用的持续时长,当持续时长大于或等于第一预设时长时,且未检验到数据库连接有传输内容,则解除应用对数据库连接的占用,将该数据块连接归还至数据库连接池,避免应用已无传输内容却仍然占用数据库连接,导致其他应用不能使用该连接。In the embodiment of the present invention, when the database connection is occupied by the application, the occupied duration is recorded, and when the duration is greater than or equal to the first preset duration, and no transmission content is detected in the database connection, the application will release the database connection. Occupy, return the data block connection to the database connection pool, so as to avoid the application still occupying the database connection when there is no transmission content, so that other applications cannot use the connection.
本发明实施例提供一种数据库连接管理装置,该装置包括:An embodiment of the present invention provides a database connection management device, the device comprising:
确定模块,用于当数据库连接被应用占用时,确定所述数据库连接被所述应用占用的持续时长;a determining module, configured to determine the duration that the database connection is occupied by the application when the database connection is occupied by the application;
处理模块,用于若确定所述持续时长大于或等于第一预设时长,且未检验到所述数据库连接有传输内容,则解除所述应用对所述数据库连接的占用。A processing module, configured to release the application from occupying the database connection if it is determined that the duration is greater than or equal to the first preset duration and no transmission content is detected in the database connection.
可选的,所述确定模块,具体用于:Optionally, the determining module is specifically used for:
记录所述数据库连接被所述应用占用时的起始时刻;record the start time when the database connection is occupied by the application;
按照第一预设周期获取当前时刻,并将当前时刻减去所述起始时刻的时长,作为所述持续时长;所述第一预设周期小于所述第一预设时长。The current time is acquired according to a first preset period, and the duration of the starting time is subtracted from the current time as the duration; the first preset period is less than the first preset time period.
可选的,所述处理模块,还用于:Optionally, the processing module is also used for:
若检验到所述数据库连接有传输内容,则用当前时刻替换所述起始时刻。If it is checked that there is transmission content connected to the database, the starting time is replaced with the current time.
可选的,所述处理模块,具体用于:Optionally, the processing module is specifically used for:
对所述数据库连接进行切面拦截,并将切面拦截的内容存储至第一变量;所述第一变量的默认值为空;Aspect interception is performed on the database connection, and the content intercepted by the aspect is stored in a first variable; the default value of the first variable is empty;
若在第二预设时长内,所述第一变量的值为空,则确定所述数据库连接无传输内容;否则,确定所述数据库连接有传输内容。If the value of the first variable is empty within the second preset time period, it is determined that the database connection has no transmission content; otherwise, it is determined that the database connection has transmission content.
可选的,所述处理模块,还用于:Optionally, the processing module is also used for:
在确定所述持续时长小于所述第一预设时长的时间段内,或者检验到所述数据库连接有传输内容,则保持所述数据库连接不变。In the time period when it is determined that the duration is less than the first preset duration, or it is verified that the database connection has transmission content, the database connection is kept unchanged.
可选的,所述处理模块,具体用于:Optionally, the processing module is specifically used for:
若所述持续时长大于或等于所述第一预设时长,检验所述数据库连接是否有传输内容。If the duration is greater than or equal to the first preset duration, it is checked whether the database connection has transmission content.
附图说明Description of drawings
图1为本发明实施例提出的一种数据库连接管理方法对应的步骤流程图;FIG. 1 is a flowchart of steps corresponding to a database connection management method proposed by an embodiment of the present invention;
图2为本发明实施例提出的一种数据库连接管理方法对应的模块交互示意图;2 is a schematic diagram of module interaction corresponding to a database connection management method proposed in an embodiment of the present invention;
图3为本发明实施例提出的一种数据库连接管理方法对应的应用获取数据库连接的示意图;3 is a schematic diagram of an application corresponding to a database connection management method proposed in an embodiment of the present invention acquiring a database connection;
图4为本发明实施例提出的一种数据库连接管理方法对应的应用归还数据库连接的示意图;4 is a schematic diagram of an application returning a database connection corresponding to a database connection management method proposed in an embodiment of the present invention;
图5为本发明实施例提出的一种数据库连接管理装置对应的结构示意图。FIG. 5 is a schematic structural diagram corresponding to a database connection management apparatus according to an embodiment of the present invention.
具体实施方式Detailed ways
为了更好的理解上述技术方案,下面将结合说明书附图及具体的实施方式对上述技术方案进行详细的说明,应当理解本申请实施例以及实施例中的具体特征是对本申请技术方案的详细的说明,而不是对本申请技术方案的限定,在不冲突的情况下,本申请实施例以及实施例中的技术特征可以相互结合。In order to better understand the above technical solutions, the above technical solutions will be described in detail below with reference to the accompanying drawings and specific embodiments of the description. The description is not intended to limit the technical solutions of the present application, and the embodiments of the present application and the technical features in the embodiments may be combined with each other if there is no conflict.
应用在执行时,常常需要与数据库进行连接,以获取需要的数据,完成相应业务。现有技术中一种手段是,先将数据库连接放在数据库连接池中,应用需要使用数据库连接时,直接从数据库连接池中获取。When the application is executed, it often needs to connect with the database to obtain the required data and complete the corresponding business. One method in the prior art is to first put the database connection in the database connection pool, and directly obtain the database connection from the database connection pool when the application needs to use the database connection.
然而由于应用执行异常等原因,当应用使用数据库连接完毕后,也不将数据库连接归还至数据库连接池,这种现象称为数据库连接池泄露,当数据库连接池泄露频繁出现时,会造成数据库连接池中的数据库连接使用殆尽,造成后续应用连接数据库的请求无法得到服务。现有技术中,第一,没有自动感知数据库连接池泄露现象的方法,需要人力投入,查看应用状态与后台日志,当发生数据库连接池泄露后,采用重启服务器的方法,应用修改代码发布新的版本。第二、应用碰到数据库连接泄漏,只能不停的重启解决,该处理时间长,影响性大,导致生产服务中断,造成生产中断服务。一般来说,一个企业的应用系统会有成百上千个,在无数据库连接泄漏自动解决模型的情况下,一旦几个系统一起出现该问题,会需要大量的人力,且需要运维人员全身心观察应用,该模式由于大量的人工介入,出错的概率会放大到一定程度。上述问题的一种典型场景为Java服务器,Java服务器是一种容器,应用完成业务需要运行在Java服务器上。举例来说,Java服务器有JAVA应用服务器,weblogic和开源的tomcat、jboss服务器,Java服务器中封装了大量底层功能,应用通过调用Java服务器中的底层功能完成业务。However, due to abnormal application execution and other reasons, when the application uses the database connection, it does not return the database connection to the database connection pool. This phenomenon is called database connection pool leakage. When database connection pool leakage occurs frequently, it will cause database connection. The database connections in the pool are exhausted, causing subsequent application requests to connect to the database to fail to be serviced. In the prior art, first, there is no method for automatically sensing the leakage of the database connection pool, which requires human input to check the application status and background logs. When the database connection pool leaks, the method of restarting the server is adopted, and the application modifies the code and publishes a new one. Version. Second, when the application encounters database connection leaks, it can only be solved by restarting continuously. The processing time is long and the impact is large, resulting in interruption of production services and interruption of production services. Generally speaking, there will be hundreds of application systems in an enterprise. If there is no database connection leakage and the automatic solution model, once the problem occurs in several systems together, a lot of manpower will be required, and the entire operation and maintenance personnel will be required. In the application of mind observation, due to a large number of manual interventions, the probability of errors will be amplified to a certain extent. A typical scenario of the above problem is a Java server. The Java server is a kind of container, and the application needs to run on the Java server to complete the business. For example, the Java server includes JAVA application server, weblogic and open source tomcat and jboss servers. The Java server encapsulates a large number of underlying functions, and the application completes the business by calling the underlying functions in the Java server.
本发明实施例提出了一种解决数据库连接泄漏的方法,在发生数据库连接泄露时,自动回收数据库连接,该方法可通过解决模型具体实现。其中,解决模型为一个代码模块,这个代码模块中定义了具体场景下各种异常事件处理的函数。举例来说,对应用于任何使用Java技术的数据库连接池,使用Java语言编写解决模型。具体地,解决模型可概述为:首先需要自动解决数据库连接泄漏的中间件开启连接泄漏,然后,应用需要数据库连接的时候,会从容器中的数据库连接池中获取,容器会与数据库去建立连接,放入容器中的数据库连接池中,每次返回给应用的连接,都记录拿取得时间,然后如果应用超过一定的时间仍然没有归还,就去查看连接是否有大量的数据交互,若无,则表明显示连接,主动把连接归还给容器中数据库连接池,则解决数据库泄漏问题。The embodiment of the present invention proposes a method for solving database connection leakage. When database connection leakage occurs, the database connection is automatically recovered, and the method can be specifically realized by solving the model. Among them, the solution model is a code module, which defines functions for processing various abnormal events in specific scenarios. For example, for any database connection pool using Java technology, use the Java language to write the solution model. Specifically, the solution model can be summarized as: first, the middleware that automatically solves the database connection leak needs to open the connection leak, and then, when the application needs a database connection, it will obtain it from the database connection pool in the container, and the container will establish a connection with the database , put it into the database connection pool in the container, and record the acquisition time every time the connection is returned to the application. It indicates that the connection is displayed, and the connection is actively returned to the database connection pool in the container to solve the database leak problem.
如图1所示,为本发明实施例提出的一种数据库连接管理方法对应的步骤流程图。As shown in FIG. 1 , it is a flowchart corresponding to the steps of a database connection management method provided by an embodiment of the present invention.
步骤101:当数据库连接被应用占用时,确定所述数据库连接被所述应用占用的持续时长。Step 101: When the database connection is occupied by the application, determine the duration for which the database connection is occupied by the application.
步骤102:若确定所述持续时长大于或等于第一预设时长,且未检验到所述数据库连接有传输内容,则解除所述应用对所述数据库连接的占用。Step 102: If it is determined that the duration is greater than or equal to a first preset duration, and it is not verified that the database connection has transmission content, release the application from occupying the database connection.
步骤101之前,需要开启解决模型,开启解决模型的方式为热开启,即时生效,不需要再重启机器。举例来说,通过在命令行输入命令以开启该解决模型。如图2所示,为本发明实施例提出的一种数据库连接管理方法对应的模块交互示意图。需要说明的是,图2所示的容器仅以JAVA应用服务器为例说明,应用运行在容器JAVA应用服务器上,数据库先与JAVA应用服务器中运行的数据库连接池建立连接,应用需要与数据库连接时,直接从数据库连接池获取数据库连接。Before step 101, the solution model needs to be opened, and the method of opening the solution model is hot opening, which takes effect immediately and does not need to restart the machine. For example, the solution model is started by entering a command at the command line. As shown in FIG. 2 , it is a schematic diagram of module interaction corresponding to a database connection management method proposed by an embodiment of the present invention. It should be noted that the container shown in Figure 2 only takes the JAVA application server as an example. The application runs on the container JAVA application server, and the database first establishes a connection with the database connection pool running in the JAVA application server. When the application needs to connect to the database , to obtain database connections directly from the database connection pool.
步骤101中,如图3所示,为本发明实施例提出的一种数据库连接管理方法对应的应用获取数据库连接的示意图。应用从数据库连接池获取数据库连接,也就是数据库连接被占用时,解决模型记录了获取数据库连接的时间戳,也就是数据库连接被应用占用的时间戳,被占用的起始时刻,从这个起始时刻开始计时,计算持续时长。In step 101 , as shown in FIG. 3 , it is a schematic diagram of acquiring a database connection by an application corresponding to a database connection management method proposed in an embodiment of the present invention. The application obtains the database connection from the database connection pool, that is, when the database connection is occupied, the solution model records the timestamp when the database connection is obtained, that is, the timestamp when the database connection is occupied by the application, and the starting time of the occupied, from this start The time starts to count, and the duration is calculated.
确定持续时长的方式有多种,一种可选的实施方式为,在数据库连接被应用占用时,记录下被占用的起始时刻,按照第一预设周期获取当前时刻,并将当前时刻减去起始时刻的时长,作为持续时长;所述第一预设周期小于所述第一预设时长。举例来说,在数据库连接被应用占用的起始时刻,设定一个定时器来对第一预设周期进行计时,定时器的时长可根据具体场景设定,举例来说,定时器时长为1秒。当定时器到达对应时长后,将当前时刻减去起始时刻,作为持续时长。There are many ways to determine the duration. An optional implementation is: when the database connection is occupied by the application, record the occupied starting time, obtain the current time according to the first preset cycle, and subtract the current time from the current time. The duration from the start time is taken as the duration; the first preset period is less than the first preset duration. For example, at the starting moment when the database connection is occupied by the application, a timer is set to time the first preset period. The duration of the timer can be set according to the specific scenario. For example, the timer duration is 1. second. When the timer reaches the corresponding duration, subtract the starting time from the current time as the duration.
步骤102中,如图4所示,为本发明实施例提出的一种数据库连接管理方法对应的应用归还数据库连接的示意图。由于步骤101中,应用在获取数据库连接池中的数据库连接时,已经记录了获取数据库连接的时间戳并开始计时,当数据库连接被占用的持续时长超过第一预设时长时,检验数据库连接是否已经归还至数据库连接池,也就是说应用是否解除了对数据库连接的占用,如果没有归还数据库连接,则检测模块会检测数据库连接上是否有数据传输内容,如果也没有传输内容,则强制解除应用对数据库连接的占用。In step 102, as shown in FIG. 4, it is a schematic diagram of an application returning a database connection corresponding to a database connection management method proposed in an embodiment of the present invention. Since in step 101, when the application obtains the database connection in the database connection pool, it has already recorded the time stamp of obtaining the database connection and started timing. It has been returned to the database connection pool, that is to say whether the application has released the occupation of the database connection. If the database connection is not returned, the detection module will detect whether there is data transmission content on the database connection. If there is no transmission content, the application will be forced to release. Occupation of database connections.
需要说明的是,计算持续时长以及检验数据库连接是否有传输内容即可同时进行,也可以异步进行。一种可选的实施方式为,在步骤101中计时的持续时长大于或等于第一预设时长,检验数据库连接是否有传输内容,在第二预设时长内,检验数据库连接是否有传输内容,所述第二预设时长的开始时刻在所述第一预设时长的开始时刻之后。It should be noted that the calculation of the duration and the checking of whether the database connection has transmission content can be performed simultaneously or asynchronously. An optional implementation manner is, in step 101, the duration of timing is greater than or equal to the first preset time length, checking whether the database connection has transmission content, and within the second preset time length, checking whether the database connection has transmission content, The start time of the second preset duration is after the start time of the first preset duration.
另外,举例来说,根据持续时长以及第一预设时长的开始时刻或结束时刻确定启动检验数据库连接的时长,设定一个启动检验数据库连接传输内容的触发检验时长。需要说明的是,以下出现的时刻从数据库连接被占用的开始计时的时刻。举例来说,第一预设时长为10秒,持续时长大于8秒,触发检验时长为8秒,则在持续时长的第8秒末开启对数据库连接是否有传输内容的检验。In addition, for example, the duration for starting the verification database connection is determined according to the duration and the start time or the end time of the first preset duration, and a trigger verification duration for starting the verification database connection transmission content is set. It should be noted that the following times are counted from the time when the database connection is occupied. For example, if the first preset duration is 10 seconds, the duration is greater than 8 seconds, and the triggering check duration is 8 seconds, then at the end of the 8th second of the duration, the check on whether the database connection has transmitted content is enabled.
按照以下方式检验所述数据库连接是否有传输内容:Verify that the database connection has transfer content as follows:
(1)对所述数据库连接进行切面拦截,并将切面拦截的内容存储至第一变量;所述第一变量的默认值为空。其中,切面拦截为,对变量、数据库连接中传输数据进行获取的一种方法,通过该方法可对变量值、数据库连接中传输内容进行追踪,以便运维人员对数据变化进行分析,进而对故障原因进行排查定位,具体可通过编译环境中的数据拦截器实现。举例来说,通过Java拦截器对传输内容进行拦截。(2)从检验数据库连接开始计时,到第二预设时长为止,再次检验第一变量的值,如果第一变量的值仍为空,则确定数据库连接无传输内容;否则,确定数据库连接有传输内容。(1) Aspect interception is performed on the database connection, and the content intercepted by the aspect is stored in a first variable; the default value of the first variable is empty. Among them, aspect interception is a method of acquiring data transmitted in variables and database connections. Through this method, variable values and transmission content in database connections can be tracked, so that operation and maintenance personnel can analyze data changes and then troubleshoot faults. The cause can be checked and located, which can be realized through the data interceptor in the compilation environment. For example, the transmission content is intercepted by a Java interceptor. (2) Start timing from checking the database connection, until the second preset duration, check the value of the first variable again, if the value of the first variable is still empty, then determine that the database connection has no transmission content; otherwise, determine that the database connection has transfer content.
当持续时长大于第一预设时长时,若检验到所述数据库连接有传输内容,若检验到所述数据库连接有传输内容,则用当前时刻替换所述起始时刻,以重新确定所述持续时长。由于数据库连接有传输内容,代表这个应用仍然在正常运行正常传输数据,因此需要重新对被占用的时长计时,在持续时长大于或等于第一预设时长时,仍然要再行检验数据库连接是否有传输内容。When the duration is longer than the first preset duration, if it is checked that the database is connected with transmission content, and if it is checked that the database is connected with transmission content, the starting time is replaced with the current time to re-determine the duration duration. Since the database connection has transmission content, it means that the application is still running and transmitting data normally. Therefore, it is necessary to re-time the occupied duration. When the duration is greater than or equal to the first preset duration, it is still necessary to check whether the database connection has transfer content.
步骤102中,需要说明的是,数据库连接状态包括已被占用或者未被占用。数据库连接在数据库连接池中的默认状态为未被占用。当一个数据库连接从数据库连接池中被获取并被应用占用后,这个数据库连接状态会由未被占用修改为已被占用,而且占用该数据库连接的应用也会被记录,被一个应用占用的数据库连接不能被其它应用获取并使用。当应用归还了被占用的数据库连接时,该数据库连接的状态又会由已被占用状态修改为未被占用状态,从而可再被应用占用,之前占用该数据库连接是应用都会被存储在该数据库连接对应的一个历史记录库中。In step 102, it should be noted that the database connection status includes occupied or not occupied. The default state of a database connection in the database connection pool is not occupied. When a database connection is acquired from the database connection pool and occupied by an application, the status of the database connection will be changed from unoccupied to occupied, and the application occupying the database connection will also be recorded, and the database occupied by an application will be recorded. Connections cannot be acquired and used by other applications. When the application returns the occupied database connection, the state of the database connection will be changed from the occupied state to the unoccupied state, so that it can be occupied by the application again. The application that occupied the database connection before will be stored in the database. Connect to a corresponding history library.
上述数据库连接状态是否为被占用,以及修改数据库连接的状态,一种可行的实施为,在配置文件中配置数据库连接状态变量,以标注数据库连接是否被占用。举例来说,用变量T的值表示数据库连接状态,T取值包括0和1,T为0表示数据库连接未被占用,T为1表示数据库连接已被占用。数据库连接C1从数据库连接池中被获取并被应用APP1占用后,T的值由0修改为1,并且此时会记录C1。当T为1时,如果另一个应用APP2试图获取C1,会首先读取T的值,当T的值为1时,则返回获取C1失败。另外一方面,当APP1归还了C1时,该T的值又由1被修改为0,APP1被存储在C1对应的操作日志库,从而如果应用APP3再试图占用C1时,由于此时T为0,APP3发送数据库连接请求时,成功返回数据库连接C1。Whether the above database connection state is occupied, and modifying the state of the database connection, a feasible implementation is to configure a database connection state variable in the configuration file to mark whether the database connection is occupied. For example, the value of the variable T is used to represent the database connection status. The value of T includes 0 and 1. T is 0, indicating that the database connection is not occupied, and T is 1, indicating that the database connection has been occupied. After the database connection C1 is obtained from the database connection pool and occupied by the application APP1, the value of T is changed from 0 to 1, and C1 is recorded at this time. When T is 1, if another application APP2 tries to obtain C1, it will first read the value of T, and when the value of T is 1, it will return the failure to obtain C1. On the other hand, when APP1 returns C1, the value of T is changed from 1 to 0, and APP1 is stored in the operation log library corresponding to C1, so if the application APP3 tries to occupy C1 again, since T is 0 at this time , when APP3 sends a database connection request, it successfully returns the database connection C1.
另外,一种可选的实施方式为,在确定所述持续时长小于所述第一预设时长的时间段内,或者检验到所述数据库连接有传输内容,则保持所述数据库连接不变。In addition, in an optional implementation manner, within a time period where the duration is determined to be less than the first preset duration, or it is verified that the database connection has transmission content, the database connection is kept unchanged.
该模型的优势在于:第一、对应用使用透明,逻辑处理在中间件数据库连接池中完成。第二、可热开启,热关闭,无需重启服务器。第三、对于数据库连接池泄漏的应用可以使应用无需中断服务,继续正常服务。第四、该模式误杀概率低。该模型核心在于每一此连接拿取都加上时间属性,然后侦测是否超时,再次检测数据流量。The advantages of this model are: First, it is transparent to the application, and the logic processing is completed in the middleware database connection pool. Second, it can be hot-opened and hot-closed without restarting the server. Third, for the application of database connection pool leakage, the application can continue the normal service without interrupting the service. Fourth, this mode has a low probability of manslaughter. The core of this model is to add a time attribute to each connection acquisition, and then detect whether it times out, and then detect the data traffic again.
本发明实施例具有较强的通用性和可扩展性。在数据库连接池的场景下,应用获取连接后,根据设定的归还时间和连接检测技术,使应用可以透明的使用,当出现数据库连接泄漏后,应用也可以透明无感知,自动归还到了数据库连接池。本发明实施例提出的方法适用于任何数据库连接池,举例来说,适用于java技术的数据库连接池。The embodiments of the present invention have strong versatility and expansibility. In the database connection pool scenario, after the application obtains the connection, the application can be used transparently according to the set return time and connection detection technology. When a database connection leak occurs, the application can also be transparent and imperceptible, and automatically return to the database connection. pool. The method proposed by the embodiment of the present invention is applicable to any database connection pool, for example, to the database connection pool of java technology.
本发明实施例中,数据库连接被应用占用时记录了被占用的持续时长,当持续时长大于或等于第一预设时长时,且未检验到数据库连接有传输内容,则解除应用对数据库连接的占用,将该数据块连接归还至数据库连接池,避免应用已无传输内容却仍然占用数据库连接,导致其他应用不能使用该连接。In the embodiment of the present invention, when the database connection is occupied by the application, the occupied duration is recorded, and when the duration is greater than or equal to the first preset duration, and no transmission content is detected in the database connection, the application will release the database connection. Occupy, return the data block connection to the database connection pool, so as to avoid the application still occupying the database connection when there is no transmission content, so that other applications cannot use the connection.
如图5所示,为本发明实施例提出的一种数据库连接管理装置对应的结构示意图。As shown in FIG. 5 , it is a schematic structural diagram corresponding to a database connection management apparatus according to an embodiment of the present invention.
本发明实施例提供一种数据库连接管理装置,该装置包括:An embodiment of the present invention provides a database connection management device, the device comprising:
确定模块501,用于当数据库连接被应用占用时,确定所述数据库连接被所述应用占用的持续时长;A determining module 501, configured to determine the duration that the database connection is occupied by the application when the database connection is occupied by the application;
处理模块502,用于若确定所述持续时长大于或等于第一预设时长,且未检验到所述数据库连接有传输内容,则解除所述应用对所述数据库连接的占用。The processing module 502 is configured to release the application from occupying the database connection if it is determined that the duration is greater than or equal to a first preset duration and no transmission content is detected in the database connection.
可选的,所述确定模块501,具体用于:Optionally, the determining module 501 is specifically used for:
记录所述数据库连接被所述应用占用时的起始时刻;record the start time when the database connection is occupied by the application;
按照第一预设周期获取当前时刻,并将当前时刻减去所述起始时刻的时长,作为所述持续时长;所述第一预设周期小于所述第一预设时长。The current time is acquired according to a first preset period, and the duration of the starting time is subtracted from the current time as the duration; the first preset period is less than the first preset time period.
可选的,所述处理模块502,还用于:Optionally, the processing module 502 is further configured to:
若检验到所述数据库连接有传输内容,则用当前时刻替换所述起始时刻。If it is checked that there is transmission content connected to the database, the starting time is replaced with the current time.
可选的,所述处理模块502,具体用于:Optionally, the processing module 502 is specifically used for:
对所述数据库连接进行切面拦截,并将切面拦截的内容存储至第一变量;所述第一变量的默认值为空;Aspect interception is performed on the database connection, and the content intercepted by the aspect is stored in a first variable; the default value of the first variable is empty;
若在第二预设时长内,所述第一变量的值为空,则确定所述数据库连接无传输内容;否则,确定所述数据库连接有传输内容。If the value of the first variable is empty within the second preset time period, it is determined that the database connection has no transmission content; otherwise, it is determined that the database connection has transmission content.
可选的,所述处理模块502,还用于:Optionally, the processing module 502 is further configured to:
在确定所述持续时长小于所述第一预设时长的时间段内,或者检验到所述数据库连接有传输内容,则保持所述数据库连接不变。In the time period when it is determined that the duration is less than the first preset duration, or it is verified that the database connection has transmission content, the database connection is kept unchanged.
可选的,所述处理模块502,具体用于:Optionally, the processing module 502 is specifically used for:
若所述持续时长大于或等于所述第一预设时长,检验所述数据库连接是否有传输内容。If the duration is greater than or equal to the first preset duration, it is checked whether the database connection has transmission content.
本领域内的技术人员应明白,本发明的实施例可提供为方法、系统、或计算机程序产品。因此,本发明可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本发明可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
本发明是参照根据本发明实施例的方法、装置(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block in the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to the processor of a general purpose computer, special purpose computer, embedded processor or other programmable data processing device to produce a machine such that the instructions executed by the processor of the computer or other programmable data processing device produce Means for implementing the functions specified in a flow or flow of a flowchart and/or a block or blocks of a block diagram.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory result in an article of manufacture comprising instruction means, the instructions The apparatus implements the functions specified in the flow or flow of the flowcharts and/or the block or blocks of the block diagrams.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions can also be loaded on a computer or other programmable data processing device to cause a series of operational steps to be performed on the computer or other programmable device to produce a computer-implemented process such that The instructions provide steps for implementing the functions specified in the flow or blocks of the flowcharts and/or the block or blocks of the block diagrams.
尽管已描述了本发明的优选实施例,但本领域内的技术人员一旦得知了基本创造性概念,则可对这些实施例作出另外的变更和修改。所以,所附权利要求意欲解释为包括优选实施例以及落入本发明范围的所有变更和修改。Although preferred embodiments of the present invention have been described, additional changes and modifications to these embodiments may occur to those skilled in the art once the basic inventive concepts are known. Therefore, the appended claims are intended to be construed to include the preferred embodiment and all changes and modifications that fall within the scope of the present invention.
显然,本领域的技术人员可以对本发明进行各种改动和变型而不脱离本发明的精神和范围。这样,倘若本发明的这些修改和变型属于本发明权利要求及其等同技术的范围之内,则本发明也意图包含这些改动和变型在内。It will be apparent to those skilled in the art that various modifications and variations can be made in the present invention without departing from the spirit and scope of the invention. Thus, provided that these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims (12)
Priority Applications (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201910163071.6A CN109977096A (en) | 2019-03-05 | 2019-03-05 | A kind of database connection management method and device |
| PCT/CN2020/073053 WO2020177495A1 (en) | 2019-03-05 | 2020-01-19 | Database connection management method and apparatus, and device |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201910163071.6A CN109977096A (en) | 2019-03-05 | 2019-03-05 | A kind of database connection management method and device |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN109977096A true CN109977096A (en) | 2019-07-05 |
Family
ID=67077879
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN201910163071.6A Pending CN109977096A (en) | 2019-03-05 | 2019-03-05 | A kind of database connection management method and device |
Country Status (2)
| Country | Link |
|---|---|
| CN (1) | CN109977096A (en) |
| WO (1) | WO2020177495A1 (en) |
Cited By (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| WO2020177495A1 (en) * | 2019-03-05 | 2020-09-10 | 中国银联股份有限公司 | Database connection management method and apparatus, and device |
Citations (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN104281587A (en) * | 2013-07-03 | 2015-01-14 | 阿里巴巴集团控股有限公司 | Connection establishing method and device |
| CN105373420A (en) * | 2014-08-28 | 2016-03-02 | 北京奇虎科技有限公司 | Data transmission method and apparatus |
| CN105956138A (en) * | 2016-05-11 | 2016-09-21 | 北京百度网讯科技有限公司 | Database connection control method and apparatus |
| CN106250500A (en) * | 2016-07-29 | 2016-12-21 | 广州唯品会信息科技有限公司 | The dynamic management approach of database connection pool and system |
| US20180196836A1 (en) * | 2017-01-09 | 2018-07-12 | Sap Se | Database management system with dynamic allocation of database requests |
| CN108549592A (en) * | 2018-03-19 | 2018-09-18 | 联动优势电子商务有限公司 | A kind of monitoring method and monitoring device, application server of database connection pool |
Family Cites Families (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN105808556B (en) * | 2014-12-29 | 2020-02-11 | 深圳云之家网络有限公司 | Database operation method and database engine |
| CN106095878B (en) * | 2016-06-07 | 2019-11-19 | 中国建设银行股份有限公司 | Divide the database manipulation device and method of table based on point library |
| CN109977096A (en) * | 2019-03-05 | 2019-07-05 | 中国银联股份有限公司 | A kind of database connection management method and device |
-
2019
- 2019-03-05 CN CN201910163071.6A patent/CN109977096A/en active Pending
-
2020
- 2020-01-19 WO PCT/CN2020/073053 patent/WO2020177495A1/en not_active Ceased
Patent Citations (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN104281587A (en) * | 2013-07-03 | 2015-01-14 | 阿里巴巴集团控股有限公司 | Connection establishing method and device |
| CN105373420A (en) * | 2014-08-28 | 2016-03-02 | 北京奇虎科技有限公司 | Data transmission method and apparatus |
| CN105956138A (en) * | 2016-05-11 | 2016-09-21 | 北京百度网讯科技有限公司 | Database connection control method and apparatus |
| CN106250500A (en) * | 2016-07-29 | 2016-12-21 | 广州唯品会信息科技有限公司 | The dynamic management approach of database connection pool and system |
| US20180196836A1 (en) * | 2017-01-09 | 2018-07-12 | Sap Se | Database management system with dynamic allocation of database requests |
| CN108549592A (en) * | 2018-03-19 | 2018-09-18 | 联动优势电子商务有限公司 | A kind of monitoring method and monitoring device, application server of database connection pool |
Cited By (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| WO2020177495A1 (en) * | 2019-03-05 | 2020-09-10 | 中国银联股份有限公司 | Database connection management method and apparatus, and device |
Also Published As
| Publication number | Publication date |
|---|---|
| WO2020177495A1 (en) | 2020-09-10 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| CN112434043B (en) | Data synchronization method, device, electronic equipment and medium | |
| US9311214B2 (en) | System and methods for tracing individual transactions across a mainframe computing environment | |
| US9355003B2 (en) | Capturing trace information using annotated trace output | |
| WO2018161622A1 (en) | Method and device for acquiring lag information of application | |
| CN108255620B (en) | Service logic processing method, device, service server and system | |
| US10108474B2 (en) | Trace capture of successfully completed transactions for trace debugging of failed transactions | |
| WO2017044069A1 (en) | Automatic regression identification | |
| CN108170552A (en) | A kind of method, apparatus and equipment for capturing Dump files | |
| CN111339118A (en) | Kubernetes-based resource change history recording method and device | |
| US20180143897A1 (en) | Determining idle testing periods | |
| WO2020098311A1 (en) | Method and apparatus for resource leak detection | |
| EP3131041A1 (en) | Testing of enterprise resource planning systems | |
| CN106802864A (en) | A kind of method of testing and device based on financial sector | |
| US20150378799A1 (en) | Automatic memory leak detection | |
| CN109977096A (en) | A kind of database connection management method and device | |
| CN111078418B (en) | Operation synchronization method, device, electronic equipment and computer readable storage medium | |
| CN113613063B (en) | Application anomaly reduction method, device and storage medium | |
| CN119046050A (en) | Fault reporting method, electronic equipment, medium and computer program product | |
| CN116049245A (en) | A method and device for data call relationship query processing | |
| EP2960798B1 (en) | Automatic memory leak detection | |
| CN108920683A (en) | A method, device and storage medium for downloading external resources on a cloud computing platform | |
| CN115599402A (en) | Mirror image packaging method and device and electronic equipment | |
| US8321844B2 (en) | Providing registration of a communication | |
| CN114048488A (en) | Vulnerability detection method and system | |
| CN114625596A (en) | Database state service method and device |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination |