使用JNDI进行数据库连接

问题描述:

这听起来像一个noob问题,但这是我第一次进入数据库领域。

这里我获得了

This might sound like a noob question but this is the first time I'm treading into Database territory.
From here I got the info that


有效的方式来实现
服务器和
数据库之间的通信是建立一个数据库
连接池。为每个客户端请求创建一个新的
连接可能非常耗时,特别是对于连续接收大量请求的
应用程序。

The most efficient way to implement communication between the server and database is to set up a database connection pool. Creating a new connection for each client request can be very time-consuming, especially for applications that continuously receive a large number of requests.

,教程使用JNDI数据源。

and the tutorial uses a JNDI datasource.

我的应用程序也是类似的(但我不会使用Tomcat,只是套接字),我的服务器将从多个客户端获取请求,但我不明白为什么应该使用JNDI数据源,为什么服务器不能与数据库保持一个打开的连接,当客户端请求到达时,它将处理请求并将数据提供给客户端。

My application is also similar(but I won't be using Tomcat, just sockets) and my server will be getting requests from multiple clients but I don't understand why should I use a JNDI datasource, why can't the server maintain one open connection with the Database and when a client request arrives it will process the request and feed the data to the client.

在最坏的情况下,如果我需要一个JNDI如何实现它与我的服务器应用程序?

In the worst case, If I should need a JNDI how can I implement it with my server app?

因此,它是一个客户端应用程序?应用程序和数据库通常使用 DriverManager#getConnection()获得的连接进行交互?如果是这样,那么你不一定需要JNDI来获得连接池工作。单独的连接池框架已经足够了。例如 C3P0 Apache Commons DBCP (我会推荐C3P0; DBCP是单线程)。只需替换 DriverManager#getConnection()即可。

Thus, it is a client application? The application and the database usually talks with each other using a connection obtained by DriverManager#getConnection()? If so, then you don't necessarily need JNDI to get connection pooling to work. Alone the connection pooling framework in question would already suffice. For example C3P0 or Apache Commons DBCP (I would recommend C3P0; DBCP is singlethreaded). Just replace the DriverManager#getConnection() by it.

编辑:您的意见:


服务器将与数据库通信,客户端连接到服务器,因此我不知道是否调用此客户端应用程序。

The server would be talking to the database and the clients connect to the server, so I won't know whether to call this a client application.

我实际上是一个纯Java应用程序, Java EE容器。

I actually mean, a plain vanilla Java application which doesn't run inside a Java EE container. Pascal has worded it better.


实际上,我对连接池如何工作有点困惑,自己的线程?有没有任何文件/书,以帮助我更好地了解这些概念与非池的连接?

要开始,连接池打开一个连接并保持它打开,只要到配置的超时。连接池用其自己的实现包装/ 修饰连接。连接池可以同时打开和保持配置的连接数量。当你调用 getConnection()时,它会立即给你一个已经打开的连接。当在连接上调用 close()时,它会将连接放回池中以备将来请求。这意味着你仍然必须以通常的方式编写JDBC代码:获取并关闭 Connection Statement 最短可能范围内的 ResultSet 。在 finally 块中关闭它们。如果你的JDBC代码已经很好写,事实上只有> DriverManager#getConnection()需要替换。由于你应该在同一个方法块中打开和关闭 Connection ,它通常会在同一个线程中运行。连接池会担心 Connection 不会被另一个线程获取,直到你的代码调用 close() Connection

To start, the connection pool opens a connection and holds it open as long as up to the configured timeout. The connection pool wraps/decorates the connection with its own implementation. The connection pool can open and hold a configured amount of connections simultaneously. When you call getConnection(), it will immediately give you an already opened connection. When you call close() on the connection, it will put the connection back in the pool for future requests. This thus means that you still have to write the JDBC code the usual way: acquire and close the Connection, Statement and ResultSet in the shortest possible scope. Close them all in the finally block. If your JDBC code is already well written, in fact only the DriverManager#getConnection() needs to be replaced. As you ought to open and close the Connection in the very same method block, it will normally run in the same thread. The connection pooling will worry about that the Connection is not acquired by another threads in the meanwhile until your code calls close() on the Connection.

您可以在这里一篇很好的文章,了解连接池的工作原理在引擎盖下(注意:不要使用它的生产和不再homegrow它,它只是得到了整个想法)。对于实际工作,请使用现有彻底开发和健壮的连接池框架。

You can find here a nice article to get the idea how connection pooling works under the hood (take care: don't use it for production and don't homegrow it further, it is just to get the whole idea). For real work, use an existing thoroughly developed and robust connection pooling framework.