Skip to Content
Java性能权威指南(第2版)
book

Java性能权威指南(第2版)

by Scott Oaks
April 2022
Intermediate to advanced
348 pages
11h 21m
Chinese
Posts & Telecom Press
Content preview from Java性能权威指南(第2版)
数据库性能最佳实践
|
261
快速小结
•
初始化连接对象的成本很高,在
Java
中它们经常被池化—
—无论是在
JDBC
驱动中还是在
JPA
和其他框架中。
•
和其他对象池一样,优化连接池很重要,否则它会对垃圾回收器产生不利
影响。在这种情况下,优化连接池也很必要,这样它就不会对数据库的性
能产生不利影响。
11.2.3
 预处理语句和语句池
在大多数情况下,代码中应该使用
PreparedStatement
而不是
Statement
进行
JDBC
调用。
这有助于提高性能,因为预处理语句允许数据库重用正在执行的
SQL
信息。这为后续数据
库执行预处理语句节省了工作。预处理语句还具有安全性和编程的优势,特别是在为调用
指定参数方面。
重用
是这里的关键词。首次使用预处理语句时,执行数据库需要更多的时间,因为它需要
创建和保存信息。如果语句只使用一次,这些工作就会被浪费。在这种情况下,最好使用
常规语句。
当只有几个数据库调用时,
Statement
接口会让应用程序更快地完成工作。但是,即使是
面向批处理的程序也可能会对同样的几条
SQL
语句执行成百上千次调用
,本章后面有一
个例子将使用一个批处理程序来加载数据库及其
400 896
条
记录。包含很多
JDBC
调用的
批处理程序,以及在其生命周期中会服务大量请求的服务器,最好使用
PreparedStatement
接口(数据库框架会自动使用这个接口)。
当预处理语句被池化,也就是实际情况下重用
PreparedStatement
对象时,它们才有性能
优势。为了更好地进行池化,必须考虑两个因素:
JDBC ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java数据科学实战

Java数据科学实战

Michael R. Brzustowicz, PhD
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton

Publisher Resources

ISBN: 9787115587367