当前位置:网站首页>Database connection pool and bdutils tool
Database connection pool and bdutils tool
2022-06-11 07:46:00 【Nagisa siku】
One 、 Database connection pool
1、 What is a database connection pool
The database connection pool is responsible for allocation 、 Manage and release database connections , It allows applications to reuse an existing database connection , Instead of building a new ; Release database connection with idle time exceeding the maximum idle time to avoid missing database connection caused by no free database connection . This technology can obviously improve the performance of database operation .
principle
The basic idea of connection pool is to initialize the system , Store database connections as objects in memory , When the user needs to access the database , It's not about building a new connection , Instead, take an established free connection object out of the connection pool . After use , The user is not closing the connection , Instead, put the connection back in the connection pool , For next request access . And the establishment of connections 、 Disconnection is managed by the connection pool itself . meanwhile , You can also control the initial number of connections in the connection pool by setting the parameters of the connection pool 、 The number of connections and the maximum number of uses per connection 、 Maximum free time, etc . You can also monitor the number of database connections through its own management mechanism 、 Usage, etc .
2、DataSource Interface
As DriverManager An alternative to a tool ,DataSource An object is the preferred method to obtain a connection . Realization DataSource The object of the interface is usually based on JavaTM Naming and Directory Interface (JNDI) API Is registered in the naming service .
DataSource The interface is implemented by the driver vendor . There are three types of implementations :
Basic implementation - Generating standard Connection object
Connection pool implementation - Generates a connection that automatically participates in the connection pool Connection object . This implementation is used with the mid-tier connection pool manager .
Distributed transaction implementation - Generate a Connection object , This object can be used for distributed transactions , Always participate in connection pooling most of the time . This implementation is used with the mid-tier transaction manager , In most cases it is always used with the connection pool manager .
3.BDCP data source
DBCP rely on commons-pool Object pool , So we need to commons-dbcp.jar , commons-pool.jar
DBCP Of BasicDataSource Provides close() Method , So again XML The configuration file needs to formulate destory-method=“close”, In order to Spring Close the data source normally .
Two 、BDUtile Tools
1.DButils Introduction to
To make it easier to use JDBC,Apache The organization provides DButils Tools , It is a database operation component , Realized with JDBC Simple encapsulation , Simplify without compromising performance JDBC The operation of . The latest version should be 1.7 Version of
2.QueryRunner An introduction to the
QueryRunner The use of classes is greatly simplified SQL Statement execution code , And ResultSetHandler Combination can complete various operations of the database ,QueryRunner Class provides With parameters Construction method of , This method uses javax.sql.DataSource Pass parameters to QueryRunner Get from the constructor of Connection object , also For different database operations , There are different ways .
query(String sql ,ResultSetHandler rsh, Object … params)
Query methods , The second one here Parameter is ResultSetHandler Interface , What you query is the result information , The request to return is JavaBean
, Generics are Bean class , Here, you need to establish an anonymous implementation class of the interface , rewrite handle And back to Bean object , It's OK to know that there is such a thing first , I'll explain it in detail when I look at the code ,
there params It's a variable parameter , according to SQL Statement to set parameters .
update(String sql, Object … params)
Use and delete , Insert , Update database information , alike params It's a variable parameter .
3.ResultSetHandler Interface
ResultSetHandler The interface is used to handle RestultSet Result set , It can transform the data in the result set into different forms , Depending on the data type of the result ,ResultSetHandler Several common implementation classes are provided , As follows :
BeanHander: The result set is distributed to JavaBean in , Save a piece of data .
BeanListHandler: Load each piece of data of the result into JavaBean In the example , Store in List, That is to save multiple pieces of data
4.QueryRunner Basic use of
Add data
public void testinsert() throws SQLException {
ComboPooledDataSource
datasource = new ComboPooledDataSource(); QueryRunner queryrunner =
new QueryRunner(datasource); queryrunner.update(“insert into balance
values(?,?)”, “Bob”,192); }
Update data
public void testupdate() throws SQLException {
QueryRunner
queryrunner = new QueryRunner(new ComboPooledDataSource());
queryrunner.update(“update balance set balance = ? where name = ?”,
8160, “Bob”); }
Delete data
public void testdelete() throws SQLException {
QueryRunner
queryrunner = new QueryRunner(new ComboPooledDataSource());
queryrunner.update(“delete from balance where name=?”, “Bob”); }
边栏推荐
- MFC auxiliary CString string splicing
- Use of wordcloud
- [cluster] lvs+keepalived high availability cluster
- 【AtCoder2000】Leftmost Ball (DP+组合数)
- [atcoder1980] mystious light (mathematical simulation)
- [software testing] 90% of the interviewers have been brushed out of such resumes
- wordcloud的使用
- 排序——交换排序
- Modular linear equations (Chinese remainder theorem + general solution)
- Wc2020 guessing game
猜你喜欢

Black Qunhui dsm7.0.1 physical machine installation tutorial

【CodeForces1019E】Raining season(边分治+斜率优化)

C language lesson 2
![[untitled] Weng_ C lesson 1](/img/4e/41876093ef6b6a38909832f89e1495.jpg)
[untitled] Weng_ C lesson 1
![[atcoder2306] rearranging (topology)](/img/b3/38589a07a7c26bea8ed154ab794760.png)
[atcoder2306] rearranging (topology)

RTMP protocol

Summary of classic interview questions
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]

QT picture adaptive display control

C language inherits memory management mechanism (unfinished)
随机推荐
JVM tuning
3年功能测试拿8K,被新来的反超,其实你在假装努力
【AtCoder1984】Wide Swap (拓扑排序转化)
134. 加油站
2021-10-24
MFC debugger OutputDebugString override
Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本
C language function stack frame
放大镜子效果图
multi-sig SC
About static keyword
.NET C#基础(6):命名空间 - 有名字的作用域
【NOIP2016 D1T3】换教室(期望DP+Floyd)(究极思维陷阱!)
[cluster] lvs+keepalived high availability cluster
[cluster] haproxy load balancing
Summary of classic interview questions
Aiop introduction
[software testing] 90% of the interviewers have been brushed out of such resumes
【AtCoder2304】Cleaning
QObject usage skills -- control function class