我正确地使用Java PooledConnections吗? [英] Am I using Java PooledConnections correctly?

查看:236
本文介绍了我正确地使用Java PooledConnections吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用池连接Java(因为每个线程创建一个连接是昂贵的)所以我使用 MysqlConnectionPoolDataSource()对象。我坚持我的数据源跨线程。因此,我在整个应用程序中只使用一个数据源,如下所示:

I want to use pooled connections with Java (because it is costly to create one connection per thread) so I'm using the MysqlConnectionPoolDataSource() object. I'm persisting my data source across threads. So, I'm only using one datasource throughout the application like this:

  startRegistry();    // creates an RMI registry for MySQL
  MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
  dataSource.setUser("username");
  dataSource.setPassword("password");
  dataSource.setServerName("serverIP");
  dataSource.setPort(3306);
  dataSource.setDatabaseName("dbname");

  InitialContext context = createContext();   // Creates a context
  context.rebind("MySQLDS", dataSource);

现在我创建了我的数据源,我在每个单独的线程中执行以下操作:

Now that I have my datasource created, I'm doing the following in each separate thread:

  PooledConnection connect = dataSource.getPooledConnection();
  Connection sqlConnection = connect.getConnection();

  Statement state = sqlConnection.createStatement();

  ResultSet result = state.executeQuery("select * from someTable");
  // Continue processing results

我想我困惑的是调用 dataSource.getPooledConnection();

这真的是获取池连接吗?
我注意到PooledConnection有方法像notify()和wait()...意思,我不认为它在做我认为它正在做...

I guess what I'm confused on is the call to dataSource.getPooledConnection();
Is this really fetching a pooled connection? And is this thread safe? I noticed that PooledConnection has methods like notify() and wait()... meaning that I don't think it is doing what I think it is doing...

此外,什么时候和如何释放连接?

Also, when and how should I release the connection?

我想知道如果滚动我自己会更有利,更熟悉一切,但我不想在这种情况下重新发明轮盘。)

I'm wondering if it would be more beneficial to roll my own because then I'd be more familiar with everything, but I don't really want to reinvent the wheel in this case :).

谢谢SO

推荐答案

这不是正确的方法。数据源需要由运行应用程序的任何容器管理。 MysqlConnectionPoolDataSource 连接池。它只是 <$ c $的具体实现c> javax.sql.DataSource 界面。通常在JNDI上下文中定义它并从中获取它。此外,MySQL本身明确地在其文档

This is not the right way. The datasource needs to be managed by whatever container you're running the application in. The MysqlConnectionPoolDataSource is not a connection pool. It is just a concrete implementation of the javax.sql.DataSource interface. You normally define it in the JNDI context and obtain it from there. Also MySQL itself states it all explicitly in their documentation.

现在,如何使用它取决于应用程序的用途。如果它是一个Web应用程序,那么您需要参考有问题的servletcontainer / appserver的JNDI资源文档。如果它是例如Tomcat,那么你可以找到它这里。如果你正在运行一个客户端应用程序 - 我会高度质疑连接池的价值 - ,那么你需要寻找一个连接池框架,可以利用MySQL提供的连接池数据源,如 C3P0

Now, how to use it depends on the purpose of the application. If it is a web application, then you need to refer the JNDI resources documentation of the servletcontainer/appserver in question. If it is for example Tomcat, then you can find it here. If you're running a client application --for which I would highly question the value of a connection pool--, then you need to look for a connection pooling framework which can make use of the MySQL-provided connection pooled datasource, such as C3P0.

另一个问题您发布的代码是 PooledConnection#getConnection()将返回底层连接,因此 a池连接。调用关闭它不会返回连接到池,但只是真正关闭它。池必须每次都创建一个新的连接。

The other problem with the code which you posted is that the PooledConnection#getConnection() will return the underlying connection which is thus not a pooled connection. Calling close on it won't return the connection to the pool, but just really close it. The pool has to create a new connection everytime.

然后是threadsafety故事,这取决于真正的连接池框架。 C3P0已经证明了其稳健性,只要您根据标准习语编写JDBC代码,即使用 JDBC接口并获取关闭所有资源(连接语句 ResultSet )最短可能范围。

Then the threadsafety story, that depends on the real connection pooling framework in question. C3P0 has proven its robustness in years, you don't worry about it as long as you write JDBC code according the standard idiom, i.e. use only the JDBC interfaces and acquire and close all resources (Connection, Statement and ResultSet) in shortest possible scope.

这篇关于我正确地使用Java PooledConnections吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆