CN106445976B - Database connection control method and device - Google Patents

Database connection control method and device Download PDF

Info

Publication number
CN106445976B
CN106445976B CN201510494001.0A CN201510494001A CN106445976B CN 106445976 B CN106445976 B CN 106445976B CN 201510494001 A CN201510494001 A CN 201510494001A CN 106445976 B CN106445976 B CN 106445976B
Authority
CN
China
Prior art keywords
time
database connection
database
monitoring
preset
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.)
Active
Application number
CN201510494001.0A
Other languages
Chinese (zh)
Other versions
CN106445976A (en
Inventor
相利
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Gridsum Technology Co Ltd
Original Assignee
Beijing Gridsum Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Beijing Gridsum Technology Co Ltd filed Critical Beijing Gridsum Technology Co Ltd
Priority to CN201510494001.0A priority Critical patent/CN106445976B/en
Publication of CN106445976A publication Critical patent/CN106445976A/en
Application granted granted Critical
Publication of CN106445976B publication Critical patent/CN106445976B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/21Design, administration or maintenance of databases
    • G06F16/217Database tuning

Abstract

The invention discloses a method and a device for controlling database connection, relates to the technical field of database access, and mainly aims to solve the problem that when an application program does not need to access a database, the database connection is not released due to various reasons, the database connection is maintained all the time, and the database connection is not closed until the database is closed or restarted, so that database resources are wasted. The main technical scheme of the invention comprises the following steps: when a database connection is created, a monitoring thread for monitoring the database connection is created; starting a monitoring thread, and monitoring whether the idle time of database connection exceeds a preset time threshold; and if the idle time of the database connection exceeds a preset time threshold, releasing the database connection and stopping the monitoring thread. The method and the device are mainly applied to the process that the application program accesses the database based on the database connection.

Description

Database connection control method and device
Technical Field
The invention relates to the technical field of database access, in particular to a method and a device for controlling database connection.
Background
During development and use, the application program acquires the required data by accessing the database. When accessing the database, the application program interacts with the database by using the database connection, and the specific implementation mode is as follows: the application program requests and creates database connection, the database is accessed based on the database connection, and after the access is finished, the database connection is closed, and database connection resources are released. When closing the database connection, a function for closing the database connection needs to be called to close the database connection. However, in the actual implementation process, when the application program does not need to access the database, the database connection may not be released for various reasons, and the database connection is maintained until the database is closed or restarted, so that the database resource is wasted.
Disclosure of Invention
In view of the above, the present invention provides a method and an apparatus for controlling a database connection, which mainly aim to solve the problem that when an application program does not need to access a database, the database connection may not be released for various reasons, the database connection is maintained until the database connection is closed or restarted, and the database resource is wasted.
In order to solve the above problems, the present invention mainly provides the following technical solutions:
in one aspect, the present invention provides a method for controlling database connection, including:
when a database connection is created, a monitoring thread for monitoring the database connection is created;
starting the monitoring thread, and monitoring whether the idle time of the database connection exceeds a preset time threshold; the idle time of the database connection is the time interval between the time when the database connection finally executes the database operation instruction and the current time of the system;
and if the idle time of the database connection exceeds the preset time threshold, releasing the database connection and stopping the monitoring thread.
In another aspect, the present invention further provides a database connection control apparatus, including:
the system comprises a creating unit, a monitoring unit and a processing unit, wherein the creating unit is used for creating a monitoring thread for monitoring database connection when the database connection is created;
the starting unit is used for starting the monitoring thread created by the creating unit;
the monitoring unit is used for monitoring whether the idle time of the database connection exceeds a preset time threshold value; the idle time of the database connection is the time interval between the time when the database connection finally executes the database operation instruction and the current time of the system;
the release unit is used for releasing the database connection when the idle time of the database connection monitored by the monitoring unit exceeds the preset time threshold;
and the stopping unit is used for stopping the monitoring thread when the releasing unit releases the database connection.
By the technical scheme, the technical scheme provided by the invention at least has the following advantages:
the invention provides a method and a device for controlling database connection, wherein when the database connection is created, a monitoring thread for monitoring the database connection is created, and after the monitoring thread is started, whether the idle time of the database connection exceeds a preset time threshold value or not is monitored, wherein the idle time of the database connection is the time interval between the time for the database connection to finally execute a database operation instruction and the current time of a system, and when the idle time of the database connection exceeds the preset time threshold value, the database connection is released and the monitoring thread is stopped; compared with the prior art, the method and the device have the advantages that the idle time of the database connection is monitored based on the monitoring thread, namely the application program does not access the database in the idle time, and when the idle time of the database connection exceeds the preset time threshold value, the database connection is released, so that the database resource is saved.
The foregoing description is only an overview of the technical solutions of the present invention, and the embodiments of the present invention are described below in order to make the technical means of the present invention more clearly understood and to make the above and other objects, features, and advantages of the present invention more clearly understandable.
Drawings
Various other advantages and benefits will become apparent to those of ordinary skill in the art upon reading the following detailed description of the preferred embodiments. The drawings are only for purposes of illustrating the preferred embodiments and are not to be construed as limiting the invention. Also, like reference numerals are used to refer to like parts throughout the drawings. In the drawings:
fig. 1 is a flowchart illustrating a method for controlling database connection according to an embodiment of the present invention;
FIG. 2 is a flowchart illustrating a method for monitoring whether idle time of a database connection exceeds a preset time threshold according to an embodiment of the present invention;
FIG. 3 is a block diagram illustrating a database connection control apparatus according to an embodiment of the present invention;
fig. 4 is a block diagram illustrating another database connection control apparatus according to an embodiment of the present invention.
Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
An embodiment of the present invention provides a method for controlling database connection, as shown in fig. 1, the method includes:
101. when creating a database connection, a monitoring thread is created that monitors the database connection.
Before creating a database connection, an application installed in a terminal device transmits request information for creating the database connection to a database, and when the database allows the application to create the database connection, the application creates the database connection, and creates a monitoring thread for monitoring the database connection based on the created database connection. In the embodiment of the present invention, the terminal device includes but is not limited to: personal Computers (PCs), laptops, tablets, smart phones, and the like.
In the embodiment of the invention, when the monitoring thread is created, the monitoring thread can be created after the database connection is created; alternatively, the monitoring thread may be created when the database allows the application to establish a database connection. It should be noted that the purpose of creating the monitoring thread is to monitor the database connection, and in the embodiment of the present invention, when the database allows the application to establish the database connection, the monitoring thread is created, which can perform a better monitoring function on the database connection. The embodiment of the invention does not limit the specific time for creating the monitoring thread.
102. And starting a monitoring thread, and monitoring whether the idle time of the database connection exceeds a preset time threshold.
The monitoring thread created in step 101 is in an inactive state, i.e. the monitoring thread is not started. In order to enable the monitoring thread to monitor the database connection, the monitoring thread needs to be started, and the started monitoring thread can monitor the idle time of the database connection, where the idle time is a time interval between the time when the database connection finally executes the database operation instruction and the current time of the system, and when the idle time exceeds a preset time threshold, step 103 is executed.
In order to better understand the idle time of the database connection, a specific example will be described below. Updating the time corresponding to the execution of the database operation instruction at this time when the database operation instruction is executed based on the database connection, wherein, for example, assuming that the current system time of the application program in creating the database connection and creating the monitoring thread is 08:00, the time 08:00 in creating the database connection is taken as the time for finally executing the database operation instruction by the database connection, namely, the time 08:00 in creating the database connection is used for initializing the time for finally executing the database operation instruction; if the application program executes the database operation instruction on the database at 08:05, updating the time for connecting the database and finally executing the database operation instruction to 08: 05; at 08:15, a monitoring thread monitors database connection, the idle time of the database connection is 10 minutes which is the difference between 08:15 and 08:05, and the monitoring thread monitors the database connection based on the idle time and a preset time threshold. The above are merely exemplary, and the present invention is not limited thereto.
The preset time threshold is an empirical value, and when the preset time threshold is set, the preset time threshold may be determined according to the actual requirement of the application program for accessing the database, for example, if the execution of the application program needs to continuously acquire related data in the database, the preset time threshold may be set to 12 hours or 10 hours; if the application program only needs to obtain a certain calling parameter in the database, the preset time threshold may be set to 10 minutes or 15 minutes. The above is merely an exemplary example, and the specific value set by the preset time threshold is not limited in the embodiment of the present invention.
In the embodiment of the present invention, the following manner may be adopted, but is not limited to, as the timing for starting the monitoring thread, for example, when the monitoring thread is created, the monitoring thread is started; or starting the monitoring thread within a preset time interval after the monitoring thread is established; in the embodiment of the present invention, the database operation instruction may include, but is not limited to, the following: delete database content, view database content, add database content, release database connections, and the like; the embodiment of the invention does not limit the starting time of the monitoring thread and the specific content of the database operation instruction.
103. And if the idle time of the database connection exceeds a preset time threshold, releasing the database connection and stopping the monitoring thread.
When the idle time of the database connection exceeds a preset time threshold, it indicates that the application program does not access the database in the idle time, so that the database connection is released, the monitoring thread is stopped, and the monitoring thread is cancelled. The monitoring thread releases the database connection by calling a preset function for releasing the database connection, and it should be noted that, because there may be differences in database types accessed by the application program, when the preset function is called, there may also be differences in function names used by the preset function and parameters called by the preset function, and therefore, when the preset function is called to release the database connection, the called preset function is determined based on different database types. The database types described in the embodiments of the present invention include, but are not limited to, the following: for example, DB2, SQL Server, Sybase, Informix, MySQL, VF, Access, etc., and the embodiment of the present invention does not specifically limit the type of the database and the preset function for calling and releasing the database connection.
The method for controlling database connection provided by the embodiment of the invention comprises the steps of creating a monitoring thread for monitoring the database connection when the database connection is created, monitoring whether the idle time of the database connection exceeds a preset time threshold value after the monitoring thread is started, wherein the idle time is the time interval between the time for finally executing a database operation instruction by the database connection and the current time of a system, releasing the database connection when the idle time of the database connection exceeds the preset time threshold value, and stopping the monitoring thread; compared with the prior art, the method and the device monitor the idle time of the database connection based on the monitoring thread, namely the application program does not access the database in the idle time, and automatically release the database connection when the idle time exceeds the preset time threshold value, so that the database resource is saved.
Further, when monitoring whether the idle time of the database connection exceeds the preset time threshold, the following method may be adopted, but not limited to, as shown in fig. 2, where the method includes:
201. a first time is obtained.
The first time is the time when the database connection finally executes the database operation instruction. When the first time is obtained, firstly, whether the first time is in a locking state is detected, the first time in the locking state cannot be obtained by two different threads at the same time, namely when a database operation instruction in database connection is executed and the first time is continuously updated, other threads (including a monitoring thread) cannot obtain the first time. If the first time is not in a locking state, calling a preset function to obtain the first time; if the first time is in the locking state, the monitoring thread needs to call a preset function to obtain the first time after waiting for unlocking the first time in a waiting queue for obtaining the first time. When the preset function is called to obtain the first time, any way of obtaining the first time in the prior art is adopted, and the embodiment of the invention does not describe the preset function again here.
It should be noted that, when the embodiment of the present invention is specifically implemented, the monitoring thread executes, based on a preset monitoring period, whether idle time for monitoring database connection exceeds a preset time threshold, and if it is detected that the first time is in a locked state, the monitoring thread waits for unlocking of the first time in a waiting queue corresponding to the first time, and at this time, the monitoring thread executes a next preset monitoring period, and then places both executed monitoring threads in the waiting queue for acquiring the first time. Illustratively, 20, detecting that the first time is in a locking state, adding a monitoring thread into a waiting queue for acquiring the first time, and waiting for unlocking the first time; if the preset monitoring period is 20 minutes, monitoring whether the idle time of the thread monitoring database connection exceeds a preset time threshold value at 09:40, detecting that the first time is still in a locking state, accessing the monitoring thread to a waiting queue for acquiring the first time, and waiting for unlocking the first time; at 09:50, the first time is unlocked and the monitoring thread acquires the first time. In the time interval of 09:20-09:50, two monitoring threads are executed, but in the actual running process, only a first time is returned, wherein the first time is the time when the database connection finally executes the database operation instruction.
202. A second time is obtained.
And the second time is the current time of the system. When the second time is obtained, a method for obtaining the second time correspondingly is adopted according to different application programs, different monitoring thread programming languages and different database types. For example, if the VB programming language is used by the application program and the monitoring thread, the DWORD gettimeckcount () is used to obtain the second time.
203. And calculating the difference value between the first time and the second time to obtain the idle time of the database connection.
And calculating the difference between the first time and the second time based on the first time obtained in the step 201 and the second time obtained in the step 202 to obtain the idle time of the database connection.
204. And judging whether the idle time of the database connection exceeds a time threshold.
And comparing the idle time acquired in the step 203 with a preset time threshold, and judging whether the idle time of the database connection exceeds the time threshold.
Further, after creating the database connection, executing a database operation instruction based on the database connection, specifically including: acquiring a database operation instruction, judging whether the database operation instruction is used for releasing database connection, executing the database operation instruction when the database operation instruction is not used for releasing database connection, and updating the time for finally executing the database operation instruction by the database connection; and when the database operation instruction is to release the database connection, executing the database connection release, and outputting and displaying an operation result corresponding to the execution database operation instruction.
It should be noted that the execution of the idle time for acquiring the database operation instruction and monitoring the connection of the thread monitoring database is not affected or interfered with each other, and in the actual operation process, the execution of the database operation instruction is mainly performed. For example, when the database operation instruction executes to release the database connection, even if the idle time of the database connection monitored by the monitoring thread does not exceed the preset time threshold, the database connection is released.
Further, as a refinement and an extension of the foregoing embodiment, when the step 102 is executed to monitor whether the idle time of the database connection exceeds a preset time threshold, if the idle time of the database connection exceeds the preset time threshold, the database connection is released, and the monitoring thread is stopped; and if the idle time of the database connection does not exceed the preset time threshold, maintaining the database connection and continuously executing the monitoring thread. In the embodiment of the invention, when the monitoring thread monitors the idle time of the database connection, whether the idle time of the monitoring database connection exceeds the preset time threshold value or not can be executed in real time, and whether the idle time of the monitoring database connection exceeds the preset time threshold value or not can also be executed according to the preset monitoring period.
In an implementation manner of the embodiment of the present invention, when monitoring whether the idle time of the database connection exceeds the preset time threshold in real time, if the idle time exceeds the preset time threshold, the database connection can be released in time, but when monitoring the idle time of the database connection in real time, certain processing resources need to be consumed; in another implementation manner of the embodiment of the present invention, the monitoring thread executes, based on the preset monitoring period, whether the idle time for monitoring the database connection exceeds the preset time threshold, and if it is monitored that the idle time exceeds the preset time threshold in the preset monitoring period, the database connection is released. Exemplarily, assuming that the preset monitoring period is 5 minutes, the preset time threshold is 9 minutes, the time for the database connection to finally execute the database operation instruction is 09:00, the monitoring thread executes the idle time for monitoring the database connection at 09:05, the idle time for acquiring the database connection is 5 minutes (09:05-09:00), it is determined that the idle time for the database connection does not exceed the preset time threshold, the database connection is continuously maintained, and the monitoring thread is continuously executed; and the monitoring thread executes idle time for monitoring database connection at 09:10, obtains the idle time for the database connection as 10 minutes (09:10-09:00), determines that the idle time for the database connection is 10 minutes and exceeds a preset time threshold value for 9 minutes, releases the database connection, and stops the monitoring thread. The embodiment of the invention does not specifically limit the preset monitoring period of the monitoring thread.
Further, as a refinement of the monitoring thread started in step 102, in an implementation manner of the embodiment of the present invention, the monitoring thread is started by sending a start operation instruction according to a requirement, and after the monitoring thread is created, the monitoring thread can be started by sending an operation instruction for starting the monitoring thread at the same time; or, the monitoring thread may be started by sending a start operation instruction within a time interval of 2 minutes or 5 minutes after the monitoring thread is created; this way of starting the monitoring thread requires a manual operation by the user of the application program to start the monitoring thread. In another implementation manner of the embodiment of the present invention, when a monitoring thread is created, a start time for starting the monitoring thread is set, and the monitoring thread is started according to the start time; for example, assuming that the time for creating the monitoring thread is 12:00, at this time, the starting time for starting the monitoring thread is set to be 12:08, and the monitoring thread is automatically started at 12: 08; the mode of starting the monitoring thread does not need manual operation of an application program user. The above is merely an exemplary example, and the specific time for starting the monitoring thread is not limited in the embodiment of the present invention.
Further, as a refinement and an extension of the foregoing embodiment, when the monitoring thread is created in step 101, a preset Application Programming Interface (API) is called, and when the monitoring thread is created, a method for creating the monitoring thread corresponding to the Programming language is determined according to a difference between the Programming languages of the Application programs. Illustratively, for the programming language Java, when creating the monitoring Thread, the monitoring Thread can be created by extending the Thread class; alternatively, the monitoring thread may also be created by implementing a Runnable interface. The embodiment of the present invention does not limit the specific implementation manner of creating the monitoring thread.
Optionally, after creating the database connection, an event monitor is set, where the event monitor is configured to monitor whether the application program requesting the database connection exits, and if it is monitored that the application program requesting the database connection exits, a preset function for closing the database connection is called, the database connection is released, and the monitoring thread is stopped. The event monitor captures the instruction information and processes the instruction information by calling a preset function for closing database connection and releasing the database connection when an application program user sends the instruction information for exiting the application program and before the instruction information is not executed for exiting the application program; when the execution is finished and the database connection is released, the execution is followed to exit the application program.
Further, as an implementation of the foregoing method, an embodiment of the present invention further provides a control apparatus for database connection, as shown in fig. 3, where the apparatus includes:
a creating unit 31 configured to create a monitoring thread that monitors a database connection when the database connection is created;
a starting unit 32, configured to start the monitoring thread created by the creating unit 31;
a monitoring unit 33, configured to monitor whether idle time of the database connection exceeds a preset time threshold; the idle time is the time interval between the time when the database connection finally executes the database operation instruction and the current time of the system;
a releasing unit 34, configured to release the database connection when the idle time of the database connection monitored by the monitoring unit 33 exceeds the preset time threshold;
a stopping unit 35, configured to stop the monitoring thread after the releasing unit 34 releases the database connection.
Further, as shown in fig. 4, the monitoring unit 33 includes:
a first obtaining module 331, configured to obtain a first time, where the first time is a time when the database connection finally executes the database operation instruction;
a second obtaining module 332, configured to obtain a second time, where the second time is a current time of the system;
a processing module 333, configured to calculate a difference between the first time obtained by the first obtaining module 331 and the second time obtained by the second obtaining module 332, and obtain an idle time for the database connection;
a determining module 334, configured to determine whether the idle time obtained by the processing module 333 exceeds the preset time threshold.
Further, as shown in fig. 4, the first obtaining module 331 includes:
a detecting submodule 3311, configured to detect whether the first time is in a locked state, where the locked state is a first time at which a thread lock is added to the first time when a database operation instruction in the database connection is being executed, and other threads cannot access the locked state;
the obtaining sub-module 3312 is configured to, when the first time detected by the detecting sub-module 3311 is not in the locked state, call a preset function to obtain the first time;
the processing submodule 3313 is configured to, when the first time detected by the detecting submodule 3311 is in the locked state, add the monitoring thread to a waiting queue corresponding to the obtained first time, and call a preset function to obtain the first time after the first time is unlocked.
Further, as shown in fig. 4, the apparatus further includes:
an acquiring unit 36 for acquiring the database operation instruction after the creating unit 31 creates a database connection;
a determining unit 37, configured to determine whether the database operation instruction obtained by the obtaining unit 36 is to release the database connection;
an executing unit 38, configured to execute the database operation instruction when the determining unit 37 determines that the database operation instruction is not to release the database connection;
an updating unit 39, configured to update the time when the executing unit 38 executes the database connection to finally execute the database operation instruction;
the releasing unit 34 is further configured to, if the determining unit 37 determines that the database operation instruction is to release the database connection, execute releasing the database connection.
Further, as shown in fig. 4, the apparatus further includes:
the processing unit 310 is configured to maintain the database connection and continue to execute the monitoring thread when the idle time of the database connection monitored by the monitoring unit 33 does not exceed the preset time threshold.
Further, the monitoring unit 33 is further configured to perform the monitoring whether the idle time for the database connection exceeds a preset time threshold in real time or according to a preset monitoring period.
Further, as shown in fig. 4, the starting unit 32 includes:
a sending module 321, configured to send a start operation instruction according to a requirement to start the monitoring thread;
a setting module 322, configured to set a start time for starting the monitoring thread when the monitoring thread is created, and start the monitoring thread according to the start time.
Further, the creating unit 31 is further configured to invoke a preset application programming interface to create the monitoring thread.
Further, as shown in fig. 4, the apparatus further includes:
a display unit 311, configured to output and display an operation result corresponding to the execution of the database operation instruction after the execution unit 38 executes the database operation instruction.
The device for controlling database connection provided by the embodiment of the invention is characterized in that when the database connection is created, a monitoring thread for monitoring the database connection is created, and after the monitoring thread is started, whether the idle time of the database connection exceeds a preset time threshold value or not is monitored, wherein the idle time is the time interval between the time for finally executing the database operation instruction by the database connection and the current time of a system; compared with the prior art, the method and the device monitor the idle time of the database connection based on the monitoring thread, namely the application program does not access the database in the idle time, and automatically release the database connection when the idle time exceeds the preset time threshold value, so that the database resource is saved.
In the foregoing embodiments, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
It will be appreciated that the relevant features of the method and apparatus described above are referred to one another. In addition, "first", "second", and the like in the above embodiments are for distinguishing the embodiments, and do not represent merits of the embodiments.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
The algorithms and displays presented herein are not inherently related to any particular computer, virtual machine, or other apparatus. Various general purpose systems may also be used with the teachings herein. The required structure for constructing such a system will be apparent from the description above. Moreover, the present invention is not directed to any particular programming language. It is appreciated that a variety of programming languages may be used to implement the teachings of the present invention as described herein, and any descriptions of specific languages are provided above to disclose the best mode of the invention.
In the description provided herein, numerous specific details are set forth. It is understood, however, that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures and techniques have not been shown in detail in order not to obscure an understanding of this description.
Similarly, it should be appreciated that in the foregoing description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof for the purpose of streamlining the disclosure and aiding in the understanding of one or more of the various inventive aspects. However, the disclosed method should not be interpreted as reflecting an intention that: that the invention as claimed requires more features than are expressly recited in each claim. Rather, as the following claims reflect, inventive aspects lie in less than all features of a single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of this invention.
Those skilled in the art will appreciate that the modules in the device in an embodiment may be adaptively changed and disposed in one or more devices different from the embodiment. The modules or units or components of the embodiments may be combined into one module or unit or component, and furthermore they may be divided into a plurality of sub-modules or sub-units or sub-components. All of the features disclosed in this specification (including any accompanying claims, abstract and drawings), and all of the processes or elements of any method or apparatus so disclosed, may be combined in any combination, except combinations where at least some of such features and/or processes or elements are mutually exclusive. Each feature disclosed in this specification (including any accompanying claims, abstract and drawings) may be replaced by alternative features serving the same, equivalent or similar purpose, unless expressly stated otherwise.
Furthermore, those skilled in the art will appreciate that while some embodiments described herein include some features included in other embodiments, rather than other features, combinations of features of different embodiments are meant to be within the scope of the invention and form different embodiments. For example, in the following claims, any of the claimed embodiments may be used in any combination.
The various component embodiments of the invention may be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. It will be appreciated by those skilled in the art that a microprocessor or Digital Signal Processor (DSP) may be used in practice to implement some or all of the functions of some or all of the components of the database connection control method and apparatus according to embodiments of the present invention. The present invention may also be embodied as apparatus or device programs (e.g., computer programs and computer program products) for performing a portion or all of the methods described herein. Such programs implementing the present invention may be stored on computer-readable media or may be in the form of one or more signals. Such a signal may be downloaded from an internet website or provided on a carrier signal or in any other form.
It should be noted that the above-mentioned embodiments illustrate rather than limit the invention, and that those skilled in the art will be able to design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in a claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The invention may be implemented by means of hardware comprising several distinct elements, and by means of a suitably programmed computer. In the unit claims enumerating several means, several of these means may be embodied by one and the same item of hardware. The usage of the words first, second and third, etcetera do not indicate any ordering. These words may be interpreted as names.

Claims (10)

1. A method for controlling database connection, comprising:
when creating a database connection, calling a preset application programming interface to create a monitoring thread for monitoring the database connection, and further comprising at least: for a programming language Java, when the monitoring Thread is created, the monitoring Thread can be created by extending a Thread class; or, creating a monitoring thread by implementing a Runnable interface;
starting the monitoring thread, and monitoring whether the idle time of the database connection exceeds a preset time threshold, wherein the idle time of the database connection is a time interval between the time when the database connection finally executes the database operation instruction and the current time of the system;
defining the time for the database connection to finally execute the database operation instruction as first time, wherein the acquiring the first time comprises: detecting whether the first time is in a locking state, wherein the locking state is that a thread lock is added to the first time when a database operation instruction in the database connection is executed, and other threads cannot access the first time in the locking state; if the first time is not in the locking state, calling a preset function to obtain the first time;
if the first time is in the locked state, adding the monitoring thread into a waiting queue corresponding to the first time, and after waiting for unlocking the first time, calling a preset function to obtain the first time, wherein the specific steps comprise: according to a preset monitoring period, utilizing the monitoring thread to execute whether the idle time of monitoring database connection exceeds a preset time threshold value, if the first time is detected to be in a locking state, the monitoring thread waits for the unlocking of the first time in a waiting queue corresponding to the first time, executing the next preset monitoring period by the monitoring thread, and putting the two executed monitoring threads into the waiting queue for acquiring the first time so as to execute the first time when the unlocking is executed at the first time;
and if the idle time of the database connection exceeds the preset time threshold, releasing the database connection and stopping the monitoring thread.
2. The method of claim 1, wherein monitoring whether the idle time of the database connection exceeds a preset time threshold comprises:
acquiring first time, wherein the first time is the time when the database connection finally executes the database operation instruction;
acquiring second time, wherein the second time is the current time of the system;
calculating the difference value between the first time and the second time to obtain the idle time of the database connection;
and judging whether the idle time of the database connection exceeds the preset time threshold.
3. The method of claim 1 or 2, wherein after creating the database connection, the method further comprises:
acquiring the database operation instruction;
judging whether the database operation instruction is to release the database connection;
if the database operation instruction is not to release the database connection, executing the database operation instruction, and updating the time for the database connection to finally execute the database operation instruction;
and if the database operation instruction is to release the database connection, executing to release the database connection.
4. The method of claim 1, further comprising:
and if the idle time of the database connection does not exceed the preset time threshold, maintaining the database connection and continuously executing the monitoring thread.
5. The method of claim 1, wherein monitoring whether the idle time of the database connection exceeds a preset time threshold comprises:
and executing the monitoring whether the idle time of the database connection exceeds a preset time threshold in real time or according to a preset monitoring period.
6. The method of claim 1, wherein the initiating the monitoring thread comprises:
sending a starting operation instruction according to the requirement to start the monitoring thread;
or when the monitoring thread is created, setting the starting time for starting the monitoring thread, and starting the monitoring thread according to the starting time.
7. A database-connected control apparatus, comprising:
the creating unit is used for calling a preset application programming interface to create a monitoring thread for monitoring the database connection when the database connection is created, and further comprises at least: for a programming language Java, when the monitoring Thread is created, the monitoring Thread can be created by extending a Thread class; or, creating a monitoring thread by implementing a Runnable interface;
the starting unit is used for starting the monitoring thread created by the creating unit;
the monitoring unit is used for monitoring whether the idle time of the database connection exceeds a preset time threshold value, wherein the idle time of the database connection is a time interval between the time when the database connection finally executes the database operation instruction and the current time of the system;
wherein, the time for the database connection to finally execute the database operation instruction is defined as a first time, and the first obtaining module includes:
a detection submodule, configured to detect whether the first time is in a locked state, where the locked state is a first time when a thread lock is added to the first time when a database operation instruction in the database connection is being executed, and other threads cannot access the locked state;
the obtaining submodule is used for calling a preset function to obtain the first time when the detected first time is not in the locking state;
the processing submodule is configured to, when the detected first time is in the locked state, add the monitoring thread into a waiting queue corresponding to the obtained first time, and call a preset function to obtain the first time after the first time is unlocked, where the specific steps include: according to a preset monitoring period, utilizing the monitoring thread to execute whether the idle time of monitoring database connection exceeds a preset time threshold value, if the first time is detected to be in a locking state, the monitoring thread waits for the unlocking of the first time in a waiting queue corresponding to the first time, executing the next preset monitoring period by the monitoring thread, and putting the two executed monitoring threads into the waiting queue for acquiring the first time so as to execute the first time when the unlocking is executed at the first time;
the release unit is used for releasing the database connection when the idle time of the database connection monitored by the monitoring unit exceeds the preset time threshold;
and the stopping unit is used for stopping the monitoring thread when the releasing unit releases the database connection.
8. The apparatus of claim 7, wherein the monitoring unit comprises:
the first obtaining module is configured to obtain a first time, where the first time is a time when the database connection finally executes the database operation instruction;
the second acquisition module is used for acquiring second time, and the second time is the current time of the system;
the processing module is used for calculating a difference value between the first time acquired by the first acquisition module and the second time acquired by the second acquisition module to acquire the idle time of the database connection;
and the judging module is used for judging whether the idle time acquired by the processing module exceeds the preset time threshold.
9. A storage medium, characterized in that the storage medium includes a stored program, wherein, when the program runs, a device in which the storage medium is located is controlled to execute the database connection control method according to any one of claims 1 to 6.
10. A processor, characterized in that the processor is configured to execute a program, wherein the program executes the control method of the database connection according to any one of claims 1 to 6.
CN201510494001.0A 2015-08-12 2015-08-12 Database connection control method and device Active CN106445976B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201510494001.0A CN106445976B (en) 2015-08-12 2015-08-12 Database connection control method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201510494001.0A CN106445976B (en) 2015-08-12 2015-08-12 Database connection control method and device

Publications (2)

Publication Number Publication Date
CN106445976A CN106445976A (en) 2017-02-22
CN106445976B true CN106445976B (en) 2019-12-24

Family

ID=58092573

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201510494001.0A Active CN106445976B (en) 2015-08-12 2015-08-12 Database connection control method and device

Country Status (1)

Country Link
CN (1) CN106445976B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101334778A (en) * 2007-06-29 2008-12-31 国际商业机器公司 Management database connecting method and system
CN101551771A (en) * 2009-05-08 2009-10-07 中兴通讯股份有限公司 Method and system for monitoring remote database running state
CN102685209A (en) * 2012-04-01 2012-09-19 百度在线网络技术(北京)有限公司 Method, device and facility for assisting user equipment to receive and dispatch instant message
CN103605571A (en) * 2013-11-20 2014-02-26 国家电网公司 Control method of database connection pool

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101334778A (en) * 2007-06-29 2008-12-31 国际商业机器公司 Management database connecting method and system
CN101551771A (en) * 2009-05-08 2009-10-07 中兴通讯股份有限公司 Method and system for monitoring remote database running state
CN102685209A (en) * 2012-04-01 2012-09-19 百度在线网络技术(北京)有限公司 Method, device and facility for assisting user equipment to receive and dispatch instant message
CN103605571A (en) * 2013-11-20 2014-02-26 国家电网公司 Control method of database connection pool

Also Published As

Publication number Publication date
CN106445976A (en) 2017-02-22

Similar Documents

Publication Publication Date Title
JP5861228B2 (en) System, apparatus, program and method for monitoring a virtual partition
US10437578B2 (en) Orchestration of software applications upgrade using automatic hang detection
US10021355B2 (en) Method and device for processing request
KR102076379B1 (en) Persistent and resilient worker processes
US20150242627A1 (en) Apparatus and method for blocking actvity of malware
CN106980544B (en) Thread synchronization method and thread synchronization system
US9819664B2 (en) Method and device for implementing a process under a superuser privilege, and mobile terminal
US20130179554A1 (en) Method, apparatus and electronic device for application display
DE202015009286U1 (en) Short lived applications
US10228963B2 (en) Efficient hypervisor reporting
US8539490B2 (en) System and method for providing dynamic transaction optimizations
CN109918187B (en) Task scheduling method, device, equipment and storage medium
CN108563472B (en) Service plug-in loading method and device based on multi-open application
JP6088531B2 (en) Event service for local client applications through a local server
CN106445976B (en) Database connection control method and device
CN109344634B (en) Permission granting method and device for application, terminal and computer readable storage medium
CN110688203B (en) Task execution method and device
CN111710403A (en) Medical equipment supervision method, equipment and readable storage medium
CN107103099B (en) Browser homepage returning method and device
US9348667B2 (en) Apparatus for managing application program and method therefor
US7784034B1 (en) System, method and computer program product for hooking a COM interface
CN106648911B (en) Key jitter removal method and device
CN108762826B (en) Process hiding method and computer readable storage medium
CN113158149A (en) Method and device for processing operation authority
AU2017408802B2 (en) Network activity indicator presentation method, electronic device, computer-readable storage medium and system

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
CB02 Change of applicant information
CB02 Change of applicant information

Address after: 100083 No. 401, 4th Floor, Haitai Building, 229 North Fourth Ring Road, Haidian District, Beijing

Applicant after: Beijing Guoshuang Technology Co.,Ltd.

Address before: 100086 Cuigong Hotel, 76 Zhichun Road, Shuangyushu District, Haidian District, Beijing

Applicant before: Beijing Guoshuang Technology Co.,Ltd.

GR01 Patent grant
GR01 Patent grant