当前位置:网站首页>JDBC | Chapter 5: closing and releasing JDBC connection resources
JDBC | Chapter 5: closing and releasing JDBC connection resources
2022-06-29 06:23:00 【Mr. dujiu QAQ】
Because it is not like Java Programs that do self - managing memory and garbage collection , So when we use database resources , To manually manage the use and release of these resources . If we use some resources , But did not release , Database resources will be occupied all the time , When there's a lot of data , Will drag our program down . This requires us to write relevant programs , When we finish using a resource , We should take the initiative to release it .
What resources do we need to manually manage in the database ? There are mainly 3 individual ,Connection、Statement、ResultSet, These three types of resources should be closed after they are used , Otherwise it will be reported ORA-00604 And ORA-01000 Such a mistake .
And it's important to note that , Note that code exceptions may cause these resources not to be released , So the release code of these resources needs to be put into finally{ } In the code block .
Question 1 :
Connection、Statement and ResulSet? These three objects are defined inside the method , Then the three objects disappear after the method is executed , Why close them alone ?
answer :
This connection is a connection to the database server , Although your method is over , But the resource still exists and the database connection has not been released
Question two :
Why is it JDBC After accessing the database , To press close first ResultSet, Then close it PreparedStatement, Finally close Connection, Direct closure Connection No, that's fine ?
answer :
It seems as if you just put connection It's closed , The system can run normally . That's in queries or other operations , If only close Connection, Don't make ResultSet and Statement If it's closed , Will it affect the system performance . Or other adverse effects of reality .
If you don't use connection pooling , So there's no problem , once Connection close , The database physical connection is released , All the relevant Java Resources can also be GC Recycled . But if you use connection pooling , So please pay attention ,Connection Shutdown is not a physical shutdown , Just return the connection pool , therefore PreparedStatement and ResultSet All held , And actually occupy the cursor resources of the relevant database , under these circumstances , As long as it runs for a long time , Often report “ The cursor exceeds the maximum allowed in the database ” Error of , Cause the program to fail to correct Often access the databaseBecause you open it in order ,
When open :Connection -> PreparedStatement -> ResultSet
closed :ResultSet-> PreparedStatement -> Connection
This is like a stack , Last in, first out
Resource release code
finally {
// Remember to close the database connection resource after executing the database operation
try {
resultSet.close();
preparedStatement1.close();
preparedStatement2.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}边栏推荐
- Analysis comp122 the Caesar cipher
- Personal blog item: processing of reading number +1 after viewing article details
- Fault: KDC warning log for id29
- Segment in Lucene
- Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
- AIRNET notes 1
- Hyperledger Fabric 2. X custom smart contract
- [high concurrency] deeply analyze the callable interface
- How to insert pseudo code into word documents simply and quickly?
- Week 10 - task 1- fill in the blank: line class inherits point class
猜你喜欢

National Defense University project summary

Why Houdini made the pyside2 plug-in crash

Use of sed in shell script

Meta metauniverse female safety problems occur frequently. How to solve the relevant problems in the metauniverse?

Quickly write MVVM code using source generators

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin

2,5-di (3,4-dicarboxyphenoxy) - 4 '- phenylethynylbiphenyldianhydride (pephqda) / Qiyue custom supply porphyrin modified amphiphilic block copolymer peg113-pcl46-porphyrin

Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately

Hustoj SPJ example

Creation of Arduino uno development environment
随机推荐
Jenkins operation Chapter 6 mail server sending build results
How to insert pseudo code into word documents simply and quickly?
What has urbanization brought to our mental health and behavior?
Week 10 - task 1- fill in the blank: line class inherits point class
Where is the Gcov symbol- Where are the gcov symbols?
Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]
Summary of redis basic knowledge points
AIRNET notes 1
Overlay histogram with density curve
Activiti Designer
Creation of Arduino uno development environment
ICLR is going to have a big discussion on the deep generation model. Max welling and the winner of the AAAI million dollar award are here. Bengio is one of the organizers
Is there any difference between a=a+b and a+=b?
Can I cast int to a variable of type byte? What happens if the value is larger than the range of byte type?
Introduction to Ceres Quartet
Week 10 - task 0- execution process instance resolution of constructors and destructors
Part 63 - interpreter and compiler adaptation (II)
5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
ES6 Modularization: export /import
Why is there a packaging type?