当前位置:网站首页>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();
}
}边栏推荐
- Principle of screen printing adjustment of EDA (cadence and AD) software
- Where is the Gcov symbol- Where are the gcov symbols?
- Ctrip launched the "3+2" office mode. Are you sour?
- The generation of leetcode simple questions each character is an odd number of strings
- Rich material libraries make modeling easy and efficient for developers
- Programming specification and variables of shell script
- 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
- Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
- Common methods for describing 3D models of objects and their advantages and disadvantages
- Parsing rshub document auto generation API
猜你喜欢

Creation of Arduino uno development environment

Easy to understand TCP four waves (multi picture explanation)

2022.02.15 - SX10-31. House raiding III

Programming specification and variables of shell script

Clickhouse data type

Maximum ascending subarray sum of leetcode simple problem

How to insert pseudo code into word documents simply and quickly?

Parsing rshub document auto generation API

Failure: unable to log in to "taxpayer equity platform"

Rich material libraries make modeling easy and efficient for developers
随机推荐
Venn diagram proportional and color shading with semi transparency
Implementation of queue
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
Introduction to Ceres Quartet
P5 DS - component and document Association
What has urbanization brought to our mental health and behavior?
Overlay histogram with density curve
2022.02.14
Why can't the article be posted?
Fault: NetBt log for id4321
Establishing the development environment of esp8266
Est - ce que l'ouverture d'un compte de titres est sécurisée? Y a - t - il un danger?
Openfpga wishes you a happy Lantern Festival!
Sum of digits under k-ary representation of leetcode simple problem
Leetcode simple question: judging the color of a grid on a chess board
Parsing rshub document auto generation API
What is the "danksharding" of V God Kop on Valentine's day?
Monitor employee turnover dynamics. This system makes employees tremble!
β- Tetraphenyl nickel porphyrin with all chlorine substitution| β- Thiocyano tetraphenyl porphyrin copper| β- Dihydroxy tetraphenyl porphyrin 𞓜 2-nitroporphyrin | supplied by Qiyue
Segment in Lucene