从技术上讲,什么是数据库连接?

时间:2022-01-03 13:40:11

When we say we have a "Database Connection" or a "Connection Pool that has several connections open", in technical level, what are we meaning actually?

当我们说我们有一个“数据库连接”或“有几个连接打开的连接池”时,在技术层面,我们究竟意味着什么?

My understanding is:

我的理解是:

A database connection is a link to a thread running in the database process that is blocked and waiting for an input from another thread in another process.

数据库连接是指在数据库进程中运行的线程的链接,该线程被阻塞并等待另一个进程中另一个线程的输入。

Is this the right definition?

这是正确的定义吗?

So when I have mysql running in a computer and a java application running in some other computer (or same computer, does not really matter..) and when I do something like:

因此,当我在计算机中运行mysql和在其他计算机(或同一台计算机上运行,​​并不重要......)的Java应用程序时,当我执行类似的操作时:

conn.open();

to open a database connection..

打开数据库连接..

Will the mysql process create a new thread for me and block that thread and start listening for input?

mysql进程是否会为我创建一个新线程并阻止该线程并开始侦听输入?

What about the client side? What happens if I do not close the connection?

客户端怎么样?如果我不关闭连接会怎么样?

2 个解决方案

#1


1  

Do you know what socket is? This is short, but nice description in tutorial.

你知道插座是什么吗?这很简短,但教程中的描述很好。

You have to distinguish client and server side. I cannot tell for MySQL, but typically server side is implemented so, that for connection new thread is processing the requests.

您必须区分客户端和服务器端。我无法告诉MySQL,但通常服务器端是这样实现的,对于连接新线程正在处理请求。

Connection pool is there to minimize socket opening overhead. Typically you do not care via with connection (assuming all connected as same user) you received result set from DB.

连接池用于最小化套接字开放开销。通常,您不关心通过连接(假设所有连接为同一用户)从DB收到结果集。

You do not want to consume resources, so you want to be nice and when you are finished you close your connection. I believe there every server today ends a connection if there is not activity for some time (timeout).

您不希望消耗资源,因此您希望保持良好状态,并在完成后关闭连接。我相信如果一段时间内没有活动(超时),今天的每个服务器都会结束连接。

#2


1  

What is a connection pool technically ?

什么是连接池技术上?

A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each user, especially requests made to a dynamic database-driven website application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database.

连接池是维护的数据库连接的缓存,以便在将来对数据库的请求需要时可以重用连接。连接池用于增强在数据库上执行命令的性能。为每个用户打开和维护数据库连接,特别是对动态数据库驱动的网站应用程序的请求,是昂贵的并且浪费资源。在连接池中,在创建连接之后,将其放置在池中并再次使用它,以便不必建立新连接。如果正在使用所有连接,则会建立新连接并将其添加到池中。连接池还减少了用户必须等待建立与数据库的连接的时间。

What about the client side? What happens if I do not close the connection?

客户端怎么样?如果我不关闭连接会怎么样?

Technically we don't close a connection obtained from a connection pool - In fact we return it back into the pool when we're done with it.

从技术上讲,我们不会关闭从连接池获得的连接 - 事实上,当我们完成连接时,我们将它返回到池中。

conn.close will return the connection to the pool.

conn.close将返回到池的连接。

You must always call Connection.close() when using a connection pool. Physical database connections will only free up if the java object is destroyed.

使用连接池时,必须始终调用Connection.close()。物理数据库连接只有在java对象被销毁时才会释放。

if you do not close connections, a Connection Leak will occur, and your pool will run out of connections. When this happens, your application will pause indefinitely waiting for a connection to be freed up, and will need to be restarted manually.

如果您不关闭连接,将发生连接泄漏,并且您的池将用尽连接。发生这种情况时,您的应用程序将无限期地暂停等待连接被释放,并且需要手动重新启动。

#1


1  

Do you know what socket is? This is short, but nice description in tutorial.

你知道插座是什么吗?这很简短,但教程中的描述很好。

You have to distinguish client and server side. I cannot tell for MySQL, but typically server side is implemented so, that for connection new thread is processing the requests.

您必须区分客户端和服务器端。我无法告诉MySQL,但通常服务器端是这样实现的,对于连接新线程正在处理请求。

Connection pool is there to minimize socket opening overhead. Typically you do not care via with connection (assuming all connected as same user) you received result set from DB.

连接池用于最小化套接字开放开销。通常,您不关心通过连接(假设所有连接为同一用户)从DB收到结果集。

You do not want to consume resources, so you want to be nice and when you are finished you close your connection. I believe there every server today ends a connection if there is not activity for some time (timeout).

您不希望消耗资源,因此您希望保持良好状态,并在完成后关闭连接。我相信如果一段时间内没有活动(超时),今天的每个服务器都会结束连接。

#2


1  

What is a connection pool technically ?

什么是连接池技术上?

A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each user, especially requests made to a dynamic database-driven website application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database.

连接池是维护的数据库连接的缓存,以便在将来对数据库的请求需要时可以重用连接。连接池用于增强在数据库上执行命令的性能。为每个用户打开和维护数据库连接,特别是对动态数据库驱动的网站应用程序的请求,是昂贵的并且浪费资源。在连接池中,在创建连接之后,将其放置在池中并再次使用它,以便不必建立新连接。如果正在使用所有连接,则会建立新连接并将其添加到池中。连接池还减少了用户必须等待建立与数据库的连接的时间。

What about the client side? What happens if I do not close the connection?

客户端怎么样?如果我不关闭连接会怎么样?

Technically we don't close a connection obtained from a connection pool - In fact we return it back into the pool when we're done with it.

从技术上讲,我们不会关闭从连接池获得的连接 - 事实上,当我们完成连接时,我们将它返回到池中。

conn.close will return the connection to the pool.

conn.close将返回到池的连接。

You must always call Connection.close() when using a connection pool. Physical database connections will only free up if the java object is destroyed.

使用连接池时,必须始终调用Connection.close()。物理数据库连接只有在java对象被销毁时才会释放。

if you do not close connections, a Connection Leak will occur, and your pool will run out of connections. When this happens, your application will pause indefinitely waiting for a connection to be freed up, and will need to be restarted manually.

如果您不关闭连接,将发生连接泄漏,并且您的池将用尽连接。发生这种情况时,您的应用程序将无限期地暂停等待连接被释放,并且需要手动重新启动。